Skip to content

Commit

Permalink
fix: Fixed issue with restoring sensors where the unit of measurement…
Browse files Browse the repository at this point in the history
… has been changed (1 hour dev time)
  • Loading branch information
BottlecapDave committed Jan 2, 2025
1 parent 1d81993 commit 0c61292
Show file tree
Hide file tree
Showing 47 changed files with 146 additions and 99 deletions.
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/cost_tracker/cost_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)
# Make sure our attributes don't override any changed settings
self._attributes.update(self._config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)
# Make sure our attributes don't override any changed settings
self._attributes.update(self._config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -126,9 +126,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)
# Make sure our attributes don't override any changed settings
self._attributes.update(self._config)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,9 +153,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

if "last_updated_timestamp" in self._attributes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

if "last_updated_timestamp" in self._attributes:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentElectricityIntervalAccumulativeConsumption state: {self._state}')
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/electricity/current_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,9 +156,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['all_rates', 'applicable_rates'])
_LOGGER.debug(f'Restored OctopusEnergyElectricityCurrentRate state: {self._state}')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/electricity/next_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['all_rates', 'applicable_rates'])

_LOGGER.debug(f'Restored OctopusEnergyElectricityNextRate state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -183,9 +183,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCostOverride state: {self._state}')
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/electricity/previous_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['all_rates', 'applicable_rates'])

_LOGGER.debug(f'Restored OctopusEnergyElectricityPreviousRate state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['valid_from', 'valid_to'])

_LOGGER.debug(f'Restored OctopusEnergyElectricityCurrentStandingCharge state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeGasConsumption state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ["total"])

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeGasConsumption state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -138,9 +138,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeGasCost state: {self._state}')
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/gas/current_consumption.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

if "last_updated_timestamp" in self._attributes:
Expand Down
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/gas/current_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['all_rates', 'applicable_rates'])

_LOGGER.debug(f'Restored OctopusEnergyGasCurrentRate state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored state: {self._state}')
5 changes: 3 additions & 2 deletions custom_components/octopus_energy/gas/next_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes, ['all_rates', 'applicable_rates'])

_LOGGER.debug(f'Restored OctopusEnergyGasNextRate state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeGasConsumption state: {self._state}')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ async def async_added_to_hass(self):
# If not None, we got an initial value.
await super().async_added_to_hass()
state = await self.async_get_last_state()
last_sensor_state = await self.async_get_last_sensor_data()

if state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state
if state is not None and last_sensor_state is not None and self._state is None:
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else last_sensor_state.native_value
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeGasConsumptionKwh state: {self._state}')
Loading

0 comments on commit 0c61292

Please sign in to comment.