From f67b714080f2fd99dccb3b0a548ede0222e1a5ad Mon Sep 17 00:00:00 2001 From: mampfes Date: Sun, 11 Feb 2024 10:20:37 +0100 Subject: [PATCH] improve debug output if wrong sensor selected --- custom_components/epex_spot_sensor/binary_sensor.py | 8 +++++++- custom_components/epex_spot_sensor/util.py | 7 +++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/custom_components/epex_spot_sensor/binary_sensor.py b/custom_components/epex_spot_sensor/binary_sensor.py index e48cec9..2b7a95d 100644 --- a/custom_components/epex_spot_sensor/binary_sensor.py +++ b/custom_components/epex_spot_sensor/binary_sensor.py @@ -349,7 +349,13 @@ def _update_state_for_contigous( ) def _get_marketdata(self): - marketdata = get_marketdata_from_sensor_attrs(self._sensor_attributes) + try: + marketdata = get_marketdata_from_sensor_attrs(self._sensor_attributes) + except KeyError as error: + _LOGGER.error( + f'Invalid price sensor "{self._entity_id}" selected for EPEX Spot Sensor "{self._attr_name}": {error}' # noqa:E501 + ) + return [] # now merge it with the cached info marketdata = [*marketdata, *self._cached_marketdata] diff --git a/custom_components/epex_spot_sensor/util.py b/custom_components/epex_spot_sensor/util.py index 8a11558..ae3e516 100644 --- a/custom_components/epex_spot_sensor/util.py +++ b/custom_components/epex_spot_sensor/util.py @@ -24,7 +24,7 @@ def __init__(self, entry): self._price = x self._price_uom = "pence/kWh" else: - raise KeyError("No valid price field found. Check price sensor.") + raise KeyError("No valid price field found.") def __repr__(self): return f"{self.__class__.__name__}(start: {self._start_time.isoformat()}, end: {self._end_time.isoformat()}, marketprice: {self._price} {self._price_uom})" # noqa: E501 @@ -50,8 +50,7 @@ def get_marketdata_from_sensor_attrs(attributes): """Convert sensor attributes to market price list.""" try: data = attributes["data"] - except (KeyError, TypeError): - _LOGGER.error("price sensor attributes invalid") - return [] + except KeyError: + raise KeyError("'data' missing in sensor attributes") return [Marketprice(e) for e in data]