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
1 changed files with 12 additions and 3 deletions

View File

@ -53,6 +53,8 @@ from homeassistant.util import Throttle, dt as dt_util
from .const import DOMAIN as TIBBER_DOMAIN, MANUFACTURER
FIVE_YEARS = 5 * 365 * 24
_LOGGER = logging.getLogger(__name__)
ICON = "mdi:currency-usd"
@ -724,9 +726,16 @@ class TibberDataCoordinator(DataUpdateCoordinator[None]): # pylint: disable=has
None,
{"sum"},
)
first_stat = stat[statistic_id][0]
_sum = cast(float, first_stat["sum"])
last_stats_time = first_stat["start"]
if statistic_id in stat:
first_stat = stat[statistic_id][0]
_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 = []