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

Code before:
    """Set up Tuya sensor dynamically through Tuya discovery."""
    manager = entry.runtime_data.manager

    @callback
    def async_discover_device(device_ids: list[str]) -> None:
        """Discover and add a discovered Tuya sensor."""
        entities: list[TuyaSensorEntity] = []
        for device_id in device_ids:
            device = manager.device_map[device_id]
            if descriptions := SENSORS.get(device.category):
                entities.extend(
                    TuyaSensorEntity(device, manager, description, dpcode_wrapper)
                    for description in descriptions
                    if (dpcode_wrapper := _get_dpcode_wrapper(device, description))
                )

        async_add_entities(entities)

    async_discover_device([*manager.device_map])

    entry.async_on_unload(
        async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
    )


class TuyaSensorEntity(TuyaEntity, RestoreSensor):
    """Tuya Sensor Entity."""

    entity_description: TuyaSensorEntityDescription
    _dpcode_wrapper: DPCodeWrapper
    _accumulated_value: float | None = None
    _is_delta_report: bool = False
    _last_dp_timestamp: int | None = None

    def __init__(
        self,
        device: CustomerDevice,
        device_manager: Manager,
        description: TuyaSensorEntityDescription,
        dpcode_wrapper: DPCodeWrapper,
    ) -> None:
        """Init Tuya sensor."""
        super().__init__(device, device_manager)
        self.entity_description = description
        self._attr_unique_id = f"{super().unique_id}{description.key}"
        self._dpcode_wrapper = dpcode_wrapper

        if description.native_unit_of_measurement is None:
            self._attr_native_unit_of_measurement = dpcode_wrapper.native_unit
        if description.suggested_unit_of_measurement is None:
            self._attr_suggested_unit_of_measurement = dpcode_wrapper.suggested_unit

Review comment (suggestion):
I am not sure that using a RestoreSensor is useful.
It makes the code a lot more complicated than it needs to be - with (in my opinion) very little added value.

What is the advantage of
`0...10...15...30...100...<disconnect/restart>0...5...31...54`
vs
`0...10...15...30...100...<disconnect/restart>100...105...131...154`

Code after:
        entities: list[TuyaSensorEntity] = []
        for device_id in device_ids:
            device = manager.device_map[device_id]
            if descriptions := SENSORS.get(device.category):
                entities.extend(
                    TuyaSensorEntity(device, manager, description, dpcode_wrapper)
                    for description in descriptions
                    if (dpcode_wrapper := _get_dpcode_wrapper(device, description))
                )

        async_add_entities(entities)

    async_discover_device([*manager.device_map])

    entry.async_on_unload(
        async_dispatcher_connect(hass, TUYA_DISCOVERY_NEW, async_discover_device)
    )


class TuyaSensorEntity(TuyaEntity, SensorEntity):
    """Tuya Sensor Entity."""

    entity_description: TuyaSensorEntityDescription

    def __init__(
        self,
        device: CustomerDevice,
        device_manager: Manager,
        description: TuyaSensorEntityDescription,
        dpcode_wrapper: DeviceWrapper[StateType],
    ) -> None:
        """Init Tuya sensor."""
        super().__init__(device, device_manager)
        self.entity_description = description
        self._attr_unique_id = f"{super().unique_id}{description.key}"
        self._dpcode_wrapper = dpcode_wrapper

        if description.native_unit_of_measurement is None:
            self._attr_native_unit_of_measurement = dpcode_wrapper.native_unit
        if description.suggested_unit_of_measurement is None:
            self._attr_suggested_unit_of_measurement = dpcode_wrapper.suggested_unit
        if description.state_class is None:
            self._attr_state_class = dpcode_wrapper.state_class

        self._validate_device_class_unit()

    def _validate_device_class_unit(self) -> None:
        """Validate device class unit compatibility."""

        # Logic to ensure the set device class and API received Unit Of Measurement
        # match Home Assistants requirements.