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

Code before:
class DPCodeDeltaIntegerWrapper(DPCodeIntegerWrapper):
    """Wrapper for integer values with delta report accumulation.

    This wrapper handles sensors that report incremental (delta) values
    instead of cumulative totals. It accumulates the delta values locally
    to provide a running total.
    """

    _accumulated_value: float | None = None
    _last_dp_timestamp: int | None = None

    # Mapping from Tuya API report_type to Home Assistant SensorStateClass.
    # - "sum": Delta/incremental reports, accumulated locally to form a total
    # - "minux": Cumulative total that only increases
    _REPORT_TYPE_TO_STATE_CLASS: dict[str, SensorStateClass] = {
        "sum": SensorStateClass.TOTAL_INCREASING,
        "minux": SensorStateClass.TOTAL_INCREASING,
    }

    def __init__(self, dpcode: str, type_information: IntegerTypeInformation) -> None:
        """Init DPCodeDeltaIntegerWrapper."""
        super().__init__(dpcode, type_information)

        # Set state_class based on report_type
        if report_type := type_information.report_type:
            self.state_class = self._REPORT_TYPE_TO_STATE_CLASS.get(report_type)

    def initialize(self, device: CustomerDevice) -> None:
        """Initialize wrapper with device data.

        Called when the entity is added to Home Assistant.
        Initializes the accumulated value from the current device state.
        """
        raw_value = super().read_device_status(device)
        if raw_value is not None:
            self._accumulated_value = float(raw_value)
            _LOGGER.debug(
                "Initialized accumulated value from device for %s: %s",
                self.dpcode,
                self._accumulated_value,
            )

    def process_delta_update(
        self, device: CustomerDevice, dp_timestamps: dict | None = None
    ) -> None:
        """Process a delta update by accumulating the value.

        Called during state updates to accumulate incremental values.
        """
        current_timestamp = dp_timestamps.get(self.dpcode) if dp_timestamps else None


Review comment (suggestion):
This should be moved to `DPCodeIntegerWrapper`, as `DPCodeDeltaIntegerWrapper` is only created now for "sum"

Code after:
class DPCodeDeltaIntegerWrapper(DPCodeIntegerWrapper):
    """Wrapper for integer values with delta report accumulation.

    This wrapper handles sensors that report incremental (delta) values
    instead of cumulative totals. It accumulates the delta values locally
    to provide a running total.
    """

    _accumulated_value: float = 0
    _last_dp_timestamp: int | None = None

    def __init__(self, dpcode: str, type_information: IntegerTypeInformation) -> None:
        """Init DPCodeDeltaIntegerWrapper."""
        super().__init__(dpcode, type_information)
        # Delta reports use TOTAL_INCREASING state class
        self.state_class = SensorStateClass.TOTAL_INCREASING

    def skip_update(
        self,
        device: CustomerDevice,
        updated_status_properties: list[str] | None,
        dp_timestamps: dict[str, int] | None,
    ) -> bool:
        """Override skip_update to process delta updates.

        Processes delta accumulation before determining if update should be skipped.
        """
        if (
            super().skip_update(device, updated_status_properties, dp_timestamps)
            or dp_timestamps is None
            or (current_timestamp := dp_timestamps.get(self.dpcode)) is None
            or current_timestamp == self._last_dp_timestamp
            or (raw_value := super().read_device_status(device)) is None
        ):
            return True

        delta = float(raw_value)
        self._accumulated_value += delta
        _LOGGER.debug(
            "Delta update for %s: +%s, total: %s",
            self.dpcode,
            delta,
            self._accumulated_value,
        )

        self._last_dp_timestamp = current_timestamp
        return False

    def read_device_status(self, device: CustomerDevice) -> float | None:
        """Read device status, returning accumulated value for delta reports."""
        return self._accumulated_value