Repository: home-assistant/core
File: homeassistant/components/sunricher_dali/sensor.py (Python)

Code before:
async def async_setup_entry(
    hass: HomeAssistant,
    entry: DaliCenterConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Sunricher DALI sensor entities from config entry."""
    devices = entry.runtime_data.devices

    sensors: list[SensorEntity] = []
    for device in devices:
        if is_motion_sensor(device.dev_type):
            sensors.append(SunricherDaliMotionSensor(device))
        elif is_illuminance_sensor(device.dev_type):
            sensors.append(SunricherDaliIlluminanceSensor(device))

    if sensors:
        async_add_entities(sensors)


class SunricherDaliMotionSensor(DaliDeviceEntity, SensorEntity):
    """Representation of a Sunricher DALI Motion Sensor."""

    _attr_device_class = SensorDeviceClass.ENUM
    _attr_options = ["no_motion", "motion", "vacant", "presence", "occupancy"]
    _attr_icon = "mdi:motion-sensor"
    _attr_name = "State"

    def __init__(self, device: Device) -> None:
        """Initialize the motion sensor."""
        super().__init__(device)
        self._device = device
        self._attr_native_value = "no_motion"
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, device.dev_id)},
            name=device.name,
            manufacturer=MANUFACTURER,
            model=device.model,
            via_device=(DOMAIN, device.gw_sn),
        )

    async def async_added_to_hass(self) -> None:
        """Handle entity addition to Home Assistant."""
        await super().async_added_to_hass()

        self.async_on_remove(
            self._device.register_listener(
                CallbackEventType.MOTION_STATUS, self._handle_motion_status
            )
        )

        self._device.read_status()

Review comment (suggestion):
For both entities, if the functionality of the entity is the main purpose of the device. we can set the device name to None, and thus make the entity name use the device name

Code after:
async def async_setup_entry(
    hass: HomeAssistant,
    entry: DaliCenterConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """Set up Sunricher DALI sensor entities from config entry."""
    devices = entry.runtime_data.devices

    entities: list[SensorEntity] = [
        SunricherDaliIlluminanceSensor(device)
        for device in devices
        if is_illuminance_sensor(device.dev_type)
    ]

    if entities:
        async_add_entities(entities)


class SunricherDaliIlluminanceSensor(DaliDeviceEntity, SensorEntity):
    """Representation of a Sunricher DALI Illuminance Sensor."""

    _attr_device_class = SensorDeviceClass.ILLUMINANCE
    _attr_state_class = SensorStateClass.MEASUREMENT
    _attr_native_unit_of_measurement = LIGHT_LUX
    _attr_name = None

    def __init__(self, device: Device) -> None:
        """Initialize the illuminance sensor."""
        super().__init__(device)
        self._device = device
        self._illuminance_value: float | None = None
        self._sensor_enabled: bool = True
        self._attr_device_info = DeviceInfo(
            identifiers={(DOMAIN, device.dev_id)},
            name=device.name,
            manufacturer=MANUFACTURER,
            model=device.model,
            via_device=(DOMAIN, device.gw_sn),
        )

    @property
    def native_value(self) -> float | None:
        """Return the native value, or None if sensor is disabled."""
        if not self._sensor_enabled:
            return None
        return self._illuminance_value

    async def async_added_to_hass(self) -> None:
        """Handle entity addition to Home Assistant."""
        await super().async_added_to_hass()