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

Changed TOTAL_INCREASING to TOTAL for MONETARY device #340

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
17 changes: 10 additions & 7 deletions custom_components/hildebrandglow_dcc/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async def should_update() -> bool:
return False


async def daily_data(hass: HomeAssistant, resource) -> float:
async def daily_data(hass: HomeAssistant, resource) -> (float, str):
"""Get daily usage from the API."""
# 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
Expand Down Expand Up @@ -189,7 +189,7 @@ async def daily_data(hass: HomeAssistant, resource) -> float:
v = readings[0][1].value
if len(readings) > 1:
v += readings[1][1].value
return v
return (v, t_from)
except requests.Timeout as ex:
_LOGGER.error("Timeout: %s", ex)
except requests.exceptions.ConnectionError as ex:
Expand Down Expand Up @@ -275,14 +275,14 @@ async def async_update(self) -> None:
"""Fetch new data for the sensor."""
# Get data on initial startup
if not self.initialised:
value = await daily_data(self.hass, self.resource)
(value, t_from) = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value, 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)
(value, t_from) = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value, 2)

Expand All @@ -294,7 +294,8 @@ 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
_attr_last_reset = None

def __init__(self, hass: HomeAssistant, resource, virtual_entity) -> None:
"""Initialize the sensor."""
Expand All @@ -320,16 +321,18 @@ def device_info(self) -> DeviceInfo:
async def async_update(self) -> None:
"""Fetch new data for the sensor."""
if not self.initialised:
value = await daily_data(self.hass, self.resource)
(value, t_from) = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value / 100, 2)
self._attr_last_reset = t_from
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)
(value, t_from) = await daily_data(self.hass, self.resource)
if value:
self._attr_native_value = round(value / 100, 2)
self._attr_last_reset = t_from


class TariffCoordinator(DataUpdateCoordinator):
Expand Down