Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Goodwe reset to 0 at midnight #76793

Merged
merged 9 commits into from
Sep 27, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 41 additions & 1 deletion homeassistant/components/goodwe/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from collections.abc import Callable
from dataclasses import dataclass
from datetime import timedelta
from typing import Any, cast

from goodwe import Inverter, Sensor, SensorKind
Expand All @@ -23,19 +24,28 @@
POWER_WATT,
TEMP_CELSIUS,
)
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.event import async_track_point_in_time
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
)
import homeassistant.util.dt as dt_util

from .const import DOMAIN, KEY_COORDINATOR, KEY_DEVICE_INFO, KEY_INVERTER

# Sensor name of battery SoC
BATTERY_SOC = "battery_soc"

# Sensors that are reset to 0 at midnight.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add some background in the comment to why this is needed and why it's safe to do.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, I added a detailed explanation.

# The inverter is only powered by the solar panels and not mains power, so it goes dead when the sun goes down.
# The "_day" sensors are reset to 0 when the inverter wakes up in the morning when the sun comes up and power to the inverter is restored.
# This makes sure daily values are reset at midnight instead of at sunrise.
# When the inverter has a battery connected, HomeAssistant will not reset the values but let the inverter reset them by looking at the unavailable state of the inverter.
DAILY_RESET = ["e_day", "e_load_day"]

_MAIN_SENSORS = (
"ppv",
"house_consumption",
Expand Down Expand Up @@ -167,6 +177,7 @@ def __init__(
self._attr_device_class = SensorDeviceClass.BATTERY
self._sensor = sensor
self._previous_value = None
self._stop_reset = None

@property
def native_value(self):
Expand All @@ -190,3 +201,32 @@ def available(self) -> bool:
return cast(GoodweSensorEntityDescription, self.entity_description).available(
self
)

@callback
def async_reset(self, now):
"""Reset the value back to 0 at midnight."""
if not self.coordinator.last_update_success:
self._previous_value = 0
self.coordinator.data[self._sensor.id_] = 0
self.async_write_ha_state()
next_midnight = dt_util.start_of_local_day(dt_util.utcnow() + timedelta(days=1))
self._stop_reset = async_track_point_in_time(
self.hass, self.async_reset, next_midnight
)

async def async_added_to_hass(self):
"""Schedule reset task at midnight."""
if self._sensor.id_ in DAILY_RESET:
next_midnight = dt_util.start_of_local_day(
dt_util.utcnow() + timedelta(days=1)
)
self._stop_reset = async_track_point_in_time(
self.hass, self.async_reset, next_midnight
)
await super().async_added_to_hass()

async def async_will_remove_from_hass(self):
"""Remove reset task at midnight."""
if self._sensor.id_ in DAILY_RESET and self._stop_reset is not None:
self._stop_reset()
await super().async_will_remove_from_hass()