Fix statistic bug in Tibber sensor (#116112)

* Handle keyError in Tibber sensor

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

* Constant

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>

---------

Signed-off-by: Daniel Hjelseth Høyer <github@dahoiv.net>
This commit is contained in:
Daniel Hjelseth Høyer 2024-04-24 20:00:06 +02:00 committed by GitHub
parent 67021be274
commit 830e8d7b94
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -53,6 +53,8 @@ from homeassistant.util import Throttle, dt as dt_util
from .const import DOMAIN as TIBBER_DOMAIN, MANUFACTURER from .const import DOMAIN as TIBBER_DOMAIN, MANUFACTURER
FIVE_YEARS = 5 * 365 * 24
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ICON = "mdi:currency-usd" ICON = "mdi:currency-usd"
@ -724,9 +726,16 @@ class TibberDataCoordinator(DataUpdateCoordinator[None]): # pylint: disable=has
None, None,
{"sum"}, {"sum"},
) )
first_stat = stat[statistic_id][0] if statistic_id in stat:
_sum = cast(float, first_stat["sum"]) first_stat = stat[statistic_id][0]
last_stats_time = first_stat["start"] _sum = cast(float, first_stat["sum"])
last_stats_time = first_stat["start"]
else:
hourly_data = await home.get_historic_data(
FIVE_YEARS, production=is_production
)
_sum = 0.0
last_stats_time = None
statistics = [] statistics = []