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

Code before:
SENSORS[DeviceCategory.CZ] = SENSORS[DeviceCategory.KG]

# Smart Camera - Low power consumption camera (duplicate of `sp`)
SENSORS[DeviceCategory.DGHSXJ] = SENSORS[DeviceCategory.SP]

# Power Socket (duplicate of `kg`)
SENSORS[DeviceCategory.PC] = SENSORS[DeviceCategory.KG]


def _get_dpcode_wrapper(
    device: CustomerDevice,
    description: TuyaSensorEntityDescription,
) -> DPCodeWrapper | None:
    """Get DPCode wrapper for an entity description."""
    dpcode = description.dpcode or description.key
    wrapper: DPCodeWrapper | None

    if description.wrapper_class:
        for cls in description.wrapper_class:
            if wrapper := cls.find_dpcode(device, dpcode):
                return wrapper
        return None

    # Use DPCodeDeltaIntegerWrapper to handle delta report types
    # It will detect if the sensor is a delta report based on report_type
    for cls in (DPCodeDeltaIntegerWrapper, DPCodeEnumWrapper):
        if wrapper := cls.find_dpcode(device, dpcode):
            return wrapper

    return None


async def async_setup_entry(
    hass: HomeAssistant,
    entry: TuyaConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """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))

Review comment (suggestion):
It would be better to have explicit checks early. `DPCodeDeltaIntegerWrapper` should only be used when it is already confirmed that the report_type is `sum`.

```python
    if (type_information:=IntegerTypeInformation.find_dpcode(device, dpcode)):
        if type_information.report_type == "sum":
            return DPCodeDeltaIntegerWrapper(type_information.dpcode, type_information)
        return DPCodeIntegerWrapper(type_information.dpcode, type_information)
    
    return DPCodeEnumWrapper.find_dpcode(device, dpcode)
```

Code after:

def _get_dpcode_wrapper(
    device: CustomerDevice,
    description: TuyaSensorEntityDescription,
) -> DPCodeWrapper | None:
    """Get DPCode wrapper for an entity description."""
    dpcode = description.dpcode or description.key
    wrapper: DPCodeWrapper | None

    if description.wrapper_class:
        for cls in description.wrapper_class:
            if wrapper := cls.find_dpcode(device, dpcode):
                return wrapper
        return None

    # Check for integer type first, using delta wrapper only for sum report_type
    if type_information := IntegerTypeInformation.find_dpcode(device, dpcode):
        if type_information.report_type == "sum":
            return DPCodeDeltaIntegerWrapper(type_information.dpcode, type_information)
        return DPCodeIntegerWrapper(type_information.dpcode, type_information)

    return DPCodeEnumWrapper.find_dpcode(device, dpcode)


async def async_setup_entry(
    hass: HomeAssistant,
    entry: TuyaConfigEntry,
    async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
    """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)