Repository: home-assistant/core
File: homeassistant/components/growatt_server/coordinator.py (Python)

Code before:
        # The Growatt API sometimes delivers stale yesterday values after a midnight
        # reset (0 → stale → 0), causing TOTAL_INCREASING double-counting.
        if (
            entity_description.state_class is SensorStateClass.TOTAL_INCREASING
            and not entity_description.never_resets
            and return_value is not None
            and previous_value is not None
        ):
            current_val = float(return_value)
            prev_val = float(previous_value)
            if prev_val > 0 and current_val == 0:
                # Value dropped to 0 from a positive level — track it.
                self._pre_reset_values[variable] = prev_val
            elif variable in self._pre_reset_values:
                pre_reset = self._pre_reset_values[variable]
                if current_val == pre_reset:
                    # Value equals yesterday's final value — the API is
                    # serving a stale cached response (bounce)
                    return_value = previous_value
                elif current_val > 0:
                    # Genuine new-day production — clear tracking
                    del self._pre_reset_values[variable]

        # Note: previous_values stores the *output* value (after suppression),
        # not the raw API value. This is intentional — after a suppressed bounce,
        # previous_value will be 0, which is what downstream comparisons need.
        self.previous_values[variable] = return_value

        return return_value

    async def update_time_segment(
        self, segment_id: int, batt_mode: int, start_time, end_time, enabled: bool
    ) -> None:
        """Update an inverter time segment.

        Args:
            segment_id: Time segment ID (1-9)
            batt_mode: Battery mode (0=load first, 1=battery first, 2=grid first)
            start_time: Start time (datetime.time object)
            end_time: End time (datetime.time object)
            enabled: Whether the segment is enabled
        """
        _LOGGER.debug(
            "Updating time segment %d for device %s (mode=%d, %s-%s, enabled=%s)",
            segment_id,
            self.device_id,
            batt_mode,
            start_time,
            end_time,
            enabled,
        )

Review comment (bug):
```suggestion
                    _LOGGER.debug(
                        "Suppressing midnight bounce for %s: stale value %s matches "
                        "pre-reset value, keeping %s",
                        variable,
                        current_val,
                        previous_value,
                    )
                    return_value = previous_value
```
Could be useful to log this

Code after:
        # The Growatt API sometimes delivers stale yesterday values after a midnight
        # reset (0 → stale → 0), causing TOTAL_INCREASING double-counting.
        if (
            entity_description.state_class is SensorStateClass.TOTAL_INCREASING
            and not entity_description.never_resets
            and return_value is not None
            and previous_value is not None
        ):
            current_val = float(return_value)
            prev_val = float(previous_value)
            if prev_val > 0 and current_val == 0:
                # Value dropped to 0 from a positive level — track it.
                self._pre_reset_values[variable] = prev_val
            elif variable in self._pre_reset_values:
                pre_reset = self._pre_reset_values[variable]
                if current_val == pre_reset:
                    # Value equals yesterday's final value — the API is
                    # serving a stale cached response (bounce)
                    _LOGGER.debug(
                        "Suppressing midnight bounce for %s: stale value %s matches "
                        "pre-reset value, keeping %s",
                        variable,
                        current_val,
                        previous_value,
                    )
                    return_value = previous_value
                elif current_val > 0:
                    # Genuine new-day production — clear tracking
                    del self._pre_reset_values[variable]

        # Note: previous_values stores the *output* value (after suppression),
        # not the raw API value. This is intentional — after a suppressed bounce,
        # previous_value will be 0, which is what downstream comparisons need.
        self.previous_values[variable] = return_value

        return return_value

    async def update_time_segment(
        self, segment_id: int, batt_mode: int, start_time, end_time, enabled: bool
    ) -> None:
        """Update an inverter time segment.

        Args:
            segment_id: Time segment ID (1-9)
            batt_mode: Battery mode (0=load first, 1=battery first, 2=grid first)
            start_time: Start time (datetime.time object)
            end_time: End time (datetime.time object)
            enabled: Whether the segment is enabled
        """
        _LOGGER.debug(
            "Updating time segment %d for device %s (mode=%d, %s-%s, enabled=%s)",