Skip to content

Commit

Permalink
fix: Updated various sensors to update their state based on recommend…
Browse files Browse the repository at this point in the history
…ed HA practices
  • Loading branch information
BottlecapDave committed Jan 17, 2024
1 parent 9bd2b9c commit 9885528
Show file tree
Hide file tree
Showing 35 changed files with 227 additions and 96 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import logging
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity
Expand All @@ -17,6 +16,7 @@

from homeassistant.util.dt import (now)

from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult
from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

Expand Down Expand Up @@ -81,6 +81,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the current days accumulative consumption"""
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand Down Expand Up @@ -118,7 +122,7 @@ def native_value(self):
}, consumption_and_cost["charges"]))
}

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
from datetime import datetime
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand All @@ -23,6 +22,7 @@
calculate_electricity_consumption_and_cost,
)

from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult
from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

Expand Down Expand Up @@ -93,7 +93,10 @@ def last_reset(self):

@property
def native_value(self):
"""Retrieve the current days accumulative consumption"""
return self._state

@callback
def _handle_coordinator_update(self) -> None:
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
consumption_data = consumption_result.data if consumption_result is not None else None
Expand All @@ -118,7 +121,7 @@ def native_value(self):
self._attributes["last_evaluated"] = consumption_and_cost["last_evaluated"]
self._attributes["data_last_retrieved"] = consumption_result.last_retrieved if consumption_result is not None else None

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
from datetime import datetime
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand All @@ -22,6 +21,7 @@
calculate_electricity_consumption_and_cost,
)

from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult
from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

Expand Down Expand Up @@ -92,6 +92,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the current days accumulative consumption"""
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand All @@ -117,7 +121,7 @@ def native_value(self):
self._attributes["last_evaluated"] = consumption_and_cost["last_evaluated"]
self._attributes["data_last_retrieved"] = consumption_result.last_retrieved if consumption_result is not None else None

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand Down Expand Up @@ -90,6 +90,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the currently calculated state"""
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand Down Expand Up @@ -131,7 +135,7 @@ def native_value(self):
}, consumption_and_cost["charges"]))
}

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logging
from datetime import datetime
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand All @@ -19,6 +18,7 @@
calculate_electricity_consumption_and_cost,
)

from ..coordinators.current_consumption import CurrentConsumptionCoordinatorResult
from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

Expand Down Expand Up @@ -89,6 +89,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the currently calculated state"""
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand All @@ -114,7 +118,7 @@ def native_value(self):
self._attributes["last_evaluated"] = consumption_and_cost["last_evaluated"]
self._attributes["data_last_retrieved"] = consumption_result.last_retrieved if consumption_result is not None else None

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from datetime import datetime
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand Down Expand Up @@ -89,6 +89,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the currently calculated state"""
current = now()
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand All @@ -114,7 +118,7 @@ def native_value(self):
self._attributes["last_evaluated"] = consumption_and_cost["last_evaluated"]
self._attributes["data_last_retrieved"] = consumption_result.last_retrieved if consumption_result is not None else None

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from custom_components.octopus_energy.coordinators.current_consumption import CurrentConsumptionCoordinatorResult

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.util.dt import (now)

from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -80,8 +80,11 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the latest electricity consumption"""
_LOGGER.debug('Updating OctopusEnergyCurrentElectricityConsumption')
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
current_date = now()

Expand All @@ -99,7 +102,8 @@ def native_value(self):
self._attributes["last_evaluated"] = result.last_evaluated
self._attributes["data_last_retrieved"] = result.data_last_retrieved

return self._state
_LOGGER.debug('Updated OctopusEnergyCurrentElectricityConsumption')
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from homeassistant.util.dt import (now)
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity
Expand Down Expand Up @@ -69,6 +69,10 @@ def extra_state_attributes(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Handle updated data from the coordinator."""
_LOGGER.debug('Updating OctopusEnergyCurrentElectricityConsumption')
consumption_result: CurrentConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
Expand All @@ -79,7 +83,7 @@ def native_value(self):
self._attributes["last_evaluated"] = now()
self._attributes["data_last_retrieved"] = consumption_result.last_retrieved if consumption_result is not None else None

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
9 changes: 6 additions & 3 deletions custom_components/octopus_energy/electricity/current_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.util.dt import (utcnow)
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -87,6 +87,10 @@ def extra_state_attributes(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the current rate for the sensor."""
# Find the current rate. We only need to do this every half an hour
current = utcnow()
Expand Down Expand Up @@ -140,8 +144,7 @@ def native_value(self):
self._attributes["data_last_retrieved"] = rates_result.last_retrieved

self._attributes["last_evaluated"] = current

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
9 changes: 6 additions & 3 deletions custom_components/octopus_energy/electricity/next_rate.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.util.dt import (utcnow)
from homeassistant.helpers.update_coordinator import (
Expand Down Expand Up @@ -78,6 +78,10 @@ def extra_state_attributes(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the next rate for the sensor."""
# Find the next rate. We only need to do this every half an hour
current = utcnow()
Expand Down Expand Up @@ -117,8 +121,7 @@ def native_value(self):
self._attributes["data_last_retrieved"] = rates_result.last_retrieved

self._attributes["last_evaluated"] = current

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
10 changes: 7 additions & 3 deletions custom_components/octopus_energy/electricity/off_peak.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from datetime import timedelta
import logging

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity import generate_entity_id

from homeassistant.util.dt import (now)
Expand Down Expand Up @@ -57,6 +57,10 @@ def extra_state_attributes(self):

@property
def is_on(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Determine if current rate is off peak."""
current = now()
rates = self.coordinator.data.rates if self.coordinator is not None and self.coordinator.data is not None else None
Expand All @@ -66,8 +70,8 @@ def is_on(self):
self._state = is_off_peak(current, rates)

self._last_updated = current
return self._state

super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import logging
from datetime import datetime

from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback

from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
Expand Down Expand Up @@ -90,6 +90,10 @@ def last_reset(self):

@property
def native_value(self):
return self._state

@callback
def _handle_coordinator_update(self) -> None:
"""Retrieve the previous days accumulative consumption"""
result: PreviousConsumptionCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None
consumption_data = result.consumption if result is not None else None
Expand Down Expand Up @@ -117,7 +121,7 @@ def native_value(self):
if result is not None:
self._attributes["data_last_retrieved"] = result.last_retrieved

return self._state
super()._handle_coordinator_update()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
Expand Down

0 comments on commit 9885528

Please sign in to comment.