From d9a7ab067de0bfcaf2f48d2dc834822a6c8b3251 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 20 Apr 2023 10:58:37 +0100 Subject: [PATCH 1/8] Update sensor.py Stopped returning "unknown" value for sensor where the return from the API is zero value of usage, i.e. running of solar and all entries are valid zero's, also helps with the delay in getting gas readings. --- custom_components/hildebrandglow_dcc/sensor.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 4ac22f6..ccff1d5 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -276,14 +276,15 @@ async def async_update(self) -> None: # Get data on initial startup if not self.initialised: value = await daily_data(self.hass, self.resource) - if value: + if type(value) == float: self._attr_native_value = round(value, 2) + self._attr_native_value = value self.initialised = True else: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if value: + if type(value) == float: self._attr_native_value = round(value, 2) @@ -321,14 +322,14 @@ async def async_update(self) -> None: """Fetch new data for the sensor.""" if not self.initialised: value = await daily_data(self.hass, self.resource) - if value: + if type(value) == float: self._attr_native_value = round(value / 100, 2) self.initialised = True else: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if value: + if type(value) == float: self._attr_native_value = round(value / 100, 2) From edec1514028976ff863face0f74ea0cc0cfef9bb Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 20 Apr 2023 11:06:56 +0100 Subject: [PATCH 2/8] Update sensor.py Changes Cost sensor state class to TOTAL to remove HA warning about monetary value --- custom_components/hildebrandglow_dcc/sensor.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index ccff1d5..709b780 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -295,7 +295,7 @@ class Cost(SensorEntity): _attr_has_entity_name = True _attr_name = "Cost (today)" _attr_native_unit_of_measurement = "GBP" - _attr_state_class = SensorStateClass.TOTAL_INCREASING + _attr_state_class = SensorStateClass.TOTAL def __init__(self, hass: HomeAssistant, resource, virtual_entity) -> None: """Initialize the sensor.""" From 756cf12c5b49e1205363c1a0f4dc8c23a7a3e445 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 20 Apr 2023 11:13:55 +0100 Subject: [PATCH 3/8] Update sensor.py added int check also --- custom_components/hildebrandglow_dcc/sensor.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 709b780..dbf8987 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -276,7 +276,7 @@ async def async_update(self) -> None: # Get data on initial startup if not self.initialised: value = await daily_data(self.hass, self.resource) - if type(value) == float: + if type(value) == float or type(value) == int : self._attr_native_value = round(value, 2) self._attr_native_value = value self.initialised = True @@ -284,7 +284,7 @@ async def async_update(self) -> None: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if type(value) == float: + if type(value) == float or type(value) == int : self._attr_native_value = round(value, 2) @@ -322,14 +322,14 @@ async def async_update(self) -> None: """Fetch new data for the sensor.""" if not self.initialised: value = await daily_data(self.hass, self.resource) - if type(value) == float: + if type(value) == float or type(value) == int: self._attr_native_value = round(value / 100, 2) self.initialised = True else: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if type(value) == float: + if type(value) == float or type(value) == int: self._attr_native_value = round(value / 100, 2) From 5efefe50700a2e7c8ef08ed8581bf5dbbf5e4228 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 20 Apr 2023 12:31:38 +0100 Subject: [PATCH 4/8] Update sensor.py Corrected for adding costs up now not total increasing, my bad --- custom_components/hildebrandglow_dcc/sensor.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index dbf8987..124941e 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -184,12 +184,20 @@ async def daily_data(hass: HomeAssistant, resource) -> float: ) _LOGGER.debug("Successfully got daily usage for resource id %s", resource.id) _LOGGER.debug( - "Readings for %s has %s entries", resource.classifier, len(readings) + "Readings for %s has %s entries ", resource.classifier, len(readings), ) v = readings[0][1].value + _LOGGER.debug("reading %f:",v) + if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"): + _LOGGER.debug("reading total %f:",v) + return v + if len(readings) > 1: - v += readings[1][1].value + v += readings[1][1].value + _LOGGER.debug("reading %f:",v) + _LOGGER.debug("reading total %f:",v) return v + except requests.Timeout as ex: _LOGGER.error("Timeout: %s", ex) except requests.exceptions.ConnectionError as ex: From e87eb13f090d7d36d15c877e2f643357ab12db9e Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 20 Apr 2023 13:14:12 +0100 Subject: [PATCH 5/8] Update sensor.py Updated to fix cost to be latest --- custom_components/hildebrandglow_dcc/sensor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 124941e..74b879b 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -189,8 +189,10 @@ async def daily_data(hass: HomeAssistant, resource) -> float: v = readings[0][1].value _LOGGER.debug("reading %f:",v) if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"): - _LOGGER.debug("reading total %f:",v) - return v + if len(readings) > 1: + v = readings[1][1].value + _LOGGER.debug("reading cost %f:",v) + return v if len(readings) > 1: v += readings[1][1].value From 877c63664cb07b6d44203c16c88678cdda93d717 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Sat, 22 Apr 2023 10:48:37 +0100 Subject: [PATCH 6/8] Update sensor.py Tested changes, allow for zero returns for consumption, round to 3 places instead of two for Watts, make cost total and not increasing for HA and only get latest cost --- custom_components/hildebrandglow_dcc/sensor.py | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 74b879b..76861d4 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -186,6 +186,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float: _LOGGER.debug( "Readings for %s has %s entries ", resource.classifier, len(readings), ) + v = 0.0 v = readings[0][1].value _LOGGER.debug("reading %f:",v) if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"): @@ -286,16 +287,13 @@ async def async_update(self) -> None: # Get data on initial startup if not self.initialised: value = await daily_data(self.hass, self.resource) - if type(value) == float or type(value) == int : - self._attr_native_value = round(value, 2) - self._attr_native_value = value - self.initialised = True + self._attr_native_value = round(value, 3) + self.initialised = True else: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if type(value) == float or type(value) == int : - self._attr_native_value = round(value, 2) + self._attr_native_value = round(value, 3) class Cost(SensorEntity): @@ -332,15 +330,13 @@ async def async_update(self) -> None: """Fetch new data for the sensor.""" if not self.initialised: value = await daily_data(self.hass, self.resource) - if type(value) == float or type(value) == int: - self._attr_native_value = round(value / 100, 2) - self.initialised = True + self._attr_native_value = round(value / 100, 2) + self.initialised = True else: # Only update the sensor if it's between 0-5 or 30-35 minutes past the hour if await should_update(): value = await daily_data(self.hass, self.resource) - if type(value) == float or type(value) == int: - self._attr_native_value = round(value / 100, 2) + self._attr_native_value = round(value / 100, 2) class TariffCoordinator(DataUpdateCoordinator): From 21623a6be9117451d9ac9ddcd3e46f5993c6e071 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Thu, 27 Apr 2023 07:41:23 +0100 Subject: [PATCH 7/8] Update sensor.py Trap error if valid cost data not returned --- custom_components/hildebrandglow_dcc/sensor.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 76861d4..37cf10a 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -186,9 +186,15 @@ async def daily_data(hass: HomeAssistant, resource) -> float: _LOGGER.debug( "Readings for %s has %s entries ", resource.classifier, len(readings), ) + v = 0.0 v = readings[0][1].value - _LOGGER.debug("reading %f:",v) + _LOGGER.debug("1st reading %f:",v) + + if (not isinstance(v,float)): + v= 0.0 + return v + if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"): if len(readings) > 1: v = readings[1][1].value @@ -196,8 +202,8 @@ async def daily_data(hass: HomeAssistant, resource) -> float: return v if len(readings) > 1: + _LOGGER.debug("2nd reading %f:",readings[1][1].value) v += readings[1][1].value - _LOGGER.debug("reading %f:",v) _LOGGER.debug("reading total %f:",v) return v From d4b39550e0ac825e4c62d015bf9d61112479d3f0 Mon Sep 17 00:00:00 2001 From: mcc05 <38889743+mcc05@users.noreply.github.com> Date: Tue, 2 May 2023 14:55:15 +0100 Subject: [PATCH 8/8] Added more handling on errors return zero if expection --- custom_components/hildebrandglow_dcc/sensor.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/custom_components/hildebrandglow_dcc/sensor.py b/custom_components/hildebrandglow_dcc/sensor.py index 37cf10a..96d7c3b 100644 --- a/custom_components/hildebrandglow_dcc/sensor.py +++ b/custom_components/hildebrandglow_dcc/sensor.py @@ -143,6 +143,7 @@ async def should_update() -> bool: async def daily_data(hass: HomeAssistant, resource) -> float: """Get daily usage from the API.""" + v = 0.0 # If it's before 01:06, we need to fetch yesterday's data # Should only need to be before 00:36 but gas data can be 30 minutes behind electricity data if datetime.now().time() <= time(1, 5): @@ -187,7 +188,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float: "Readings for %s has %s entries ", resource.classifier, len(readings), ) - v = 0.0 + v = readings[0][1].value _LOGGER.debug("1st reading %f:",v) @@ -197,6 +198,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float: if (resource.classifier == "electricity.consumption.cost" or resource.classifier == "gas.consumption.cost"): if len(readings) > 1: + _LOGGER.debug("2nd cost reading %f:",readings[1][1].value) v = readings[1][1].value _LOGGER.debug("reading cost %f:",v) return v @@ -219,7 +221,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float: ) else: _LOGGER.exception("Unexpected exception: %s. Please open an issue", ex) - return None + return v async def tariff_data(hass: HomeAssistant, resource) -> float: