diff --git a/custom_components/octopus_energy/cost_tracker/cost_tracker.py b/custom_components/octopus_energy/cost_tracker/cost_tracker.py index 8a92b8df..2636ab72 100644 --- a/custom_components/octopus_energy/cost_tracker/cost_tracker.py +++ b/custom_components/octopus_energy/cost_tracker/cost_tracker.py @@ -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) diff --git a/custom_components/octopus_energy/cost_tracker/cost_tracker_month.py b/custom_components/octopus_energy/cost_tracker/cost_tracker_month.py index 1fc81eb3..6a5e4cb4 100644 --- a/custom_components/octopus_energy/cost_tracker/cost_tracker_month.py +++ b/custom_components/octopus_energy/cost_tracker/cost_tracker_month.py @@ -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) diff --git a/custom_components/octopus_energy/cost_tracker/cost_tracker_week.py b/custom_components/octopus_energy/cost_tracker/cost_tracker_week.py index 6e94027d..3b877093 100644 --- a/custom_components/octopus_energy/cost_tracker/cost_tracker_week.py +++ b/custom_components/octopus_energy/cost_tracker/cost_tracker_week.py @@ -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) diff --git a/custom_components/octopus_energy/electricity/current_accumulative_consumption.py b/custom_components/octopus_energy/electricity/current_accumulative_consumption.py index 8dc4e231..7afb33a3 100644 --- a/custom_components/octopus_energy/electricity/current_accumulative_consumption.py +++ b/custom_components/octopus_energy/electricity/current_accumulative_consumption.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/current_accumulative_cost.py b/custom_components/octopus_energy/electricity/current_accumulative_cost.py index 41f69418..0848cd43 100644 --- a/custom_components/octopus_energy/electricity/current_accumulative_cost.py +++ b/custom_components/octopus_energy/electricity/current_accumulative_cost.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/current_consumption.py b/custom_components/octopus_energy/electricity/current_consumption.py index b74986d2..e2880557 100644 --- a/custom_components/octopus_energy/electricity/current_consumption.py +++ b/custom_components/octopus_energy/electricity/current_consumption.py @@ -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: diff --git a/custom_components/octopus_energy/electricity/current_demand.py b/custom_components/octopus_energy/electricity/current_demand.py index c511e7c4..d9758625 100644 --- a/custom_components/octopus_energy/electricity/current_demand.py +++ b/custom_components/octopus_energy/electricity/current_demand.py @@ -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: diff --git a/custom_components/octopus_energy/electricity/current_interval_accumulative_consumption_.py b/custom_components/octopus_energy/electricity/current_interval_accumulative_consumption_.py index def7af26..2accac79 100644 --- a/custom_components/octopus_energy/electricity/current_interval_accumulative_consumption_.py +++ b/custom_components/octopus_energy/electricity/current_interval_accumulative_consumption_.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/current_rate.py b/custom_components/octopus_energy/electricity/current_rate.py index cae19580..e398aeb0 100644 --- a/custom_components/octopus_energy/electricity/current_rate.py +++ b/custom_components/octopus_energy/electricity/current_rate.py @@ -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}') diff --git a/custom_components/octopus_energy/electricity/current_total_consumption.py b/custom_components/octopus_energy/electricity/current_total_consumption.py index 9e896b42..d4bde7f9 100644 --- a/custom_components/octopus_energy/electricity/current_total_consumption.py +++ b/custom_components/octopus_energy/electricity/current_total_consumption.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/next_rate.py b/custom_components/octopus_energy/electricity/next_rate.py index a59cc4cd..0913582b 100644 --- a/custom_components/octopus_energy/electricity/next_rate.py +++ b/custom_components/octopus_energy/electricity/next_rate.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/previous_accumulative_consumption.py b/custom_components/octopus_energy/electricity/previous_accumulative_consumption.py index f1dd5521..6204161f 100644 --- a/custom_components/octopus_energy/electricity/previous_accumulative_consumption.py +++ b/custom_components/octopus_energy/electricity/previous_accumulative_consumption.py @@ -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}') diff --git a/custom_components/octopus_energy/electricity/previous_accumulative_cost.py b/custom_components/octopus_energy/electricity/previous_accumulative_cost.py index cb87c5db..bdc00945 100644 --- a/custom_components/octopus_energy/electricity/previous_accumulative_cost.py +++ b/custom_components/octopus_energy/electricity/previous_accumulative_cost.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py b/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py index 0ea11859..9e4c9e0b 100644 --- a/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py +++ b/custom_components/octopus_energy/electricity/previous_accumulative_cost_override.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/previous_rate.py b/custom_components/octopus_energy/electricity/previous_rate.py index c4d74b16..6641164a 100644 --- a/custom_components/octopus_energy/electricity/previous_rate.py +++ b/custom_components/octopus_energy/electricity/previous_rate.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/electricity/standing_charge.py b/custom_components/octopus_energy/electricity/standing_charge.py index 81a7347c..e0722330 100644 --- a/custom_components/octopus_energy/electricity/standing_charge.py +++ b/custom_components/octopus_energy/electricity/standing_charge.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_accumulative_consumption_cubic_meters.py b/custom_components/octopus_energy/gas/current_accumulative_consumption_cubic_meters.py index ee99e3d8..8b489ffb 100644 --- a/custom_components/octopus_energy/gas/current_accumulative_consumption_cubic_meters.py +++ b/custom_components/octopus_energy/gas/current_accumulative_consumption_cubic_meters.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_accumulative_consumption_kwh.py b/custom_components/octopus_energy/gas/current_accumulative_consumption_kwh.py index e6e58851..3ea2940a 100644 --- a/custom_components/octopus_energy/gas/current_accumulative_consumption_kwh.py +++ b/custom_components/octopus_energy/gas/current_accumulative_consumption_kwh.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_accumulative_cost.py b/custom_components/octopus_energy/gas/current_accumulative_cost.py index e21ae218..5833dcc1 100644 --- a/custom_components/octopus_energy/gas/current_accumulative_cost.py +++ b/custom_components/octopus_energy/gas/current_accumulative_cost.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_consumption.py b/custom_components/octopus_energy/gas/current_consumption.py index fef6c0b9..e988f539 100644 --- a/custom_components/octopus_energy/gas/current_consumption.py +++ b/custom_components/octopus_energy/gas/current_consumption.py @@ -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: diff --git a/custom_components/octopus_energy/gas/current_rate.py b/custom_components/octopus_energy/gas/current_rate.py index 3965a389..6c9c4137 100644 --- a/custom_components/octopus_energy/gas/current_rate.py +++ b/custom_components/octopus_energy/gas/current_rate.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py b/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py index 9be19c9e..897be55d 100644 --- a/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py +++ b/custom_components/octopus_energy/gas/current_total_consumption_cubic_meters.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/current_total_consumption_kwh.py b/custom_components/octopus_energy/gas/current_total_consumption_kwh.py index 6fad52db..16e1c6ec 100644 --- a/custom_components/octopus_energy/gas/current_total_consumption_kwh.py +++ b/custom_components/octopus_energy/gas/current_total_consumption_kwh.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/next_rate.py b/custom_components/octopus_energy/gas/next_rate.py index a98d4d67..99eb6a2d 100644 --- a/custom_components/octopus_energy/gas/next_rate.py +++ b/custom_components/octopus_energy/gas/next_rate.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/previous_accumulative_consumption_cubic_meters.py b/custom_components/octopus_energy/gas/previous_accumulative_consumption_cubic_meters.py index a8363689..983ac360 100644 --- a/custom_components/octopus_energy/gas/previous_accumulative_consumption_cubic_meters.py +++ b/custom_components/octopus_energy/gas/previous_accumulative_consumption_cubic_meters.py @@ -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}') diff --git a/custom_components/octopus_energy/gas/previous_accumulative_consumption_kwh.py b/custom_components/octopus_energy/gas/previous_accumulative_consumption_kwh.py index 4f0f98d9..713df8b2 100644 --- a/custom_components/octopus_energy/gas/previous_accumulative_consumption_kwh.py +++ b/custom_components/octopus_energy/gas/previous_accumulative_consumption_kwh.py @@ -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}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/previous_accumulative_cost.py b/custom_components/octopus_energy/gas/previous_accumulative_cost.py index 06554eb1..1f101e18 100644 --- a/custom_components/octopus_energy/gas/previous_accumulative_cost.py +++ b/custom_components/octopus_energy/gas/previous_accumulative_cost.py @@ -164,9 +164,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 OctopusEnergyPreviousAccumulativeGasCost state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py b/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py index 4901683d..b51fe74a 100644 --- a/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py +++ b/custom_components/octopus_energy/gas/previous_accumulative_cost_override.py @@ -199,9 +199,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 OctopusEnergyPreviousAccumulativeGasCostOverride state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/previous_rate.py b/custom_components/octopus_energy/gas/previous_rate.py index 157ade3c..3ae31c51 100644 --- a/custom_components/octopus_energy/gas/previous_rate.py +++ b/custom_components/octopus_energy/gas/previous_rate.py @@ -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 OctopusEnergyGasPreviousRate state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/gas/standing_charge.py b/custom_components/octopus_energy/gas/standing_charge.py index ae5cfa98..53d519f4 100644 --- a/custom_components/octopus_energy/gas/standing_charge.py +++ b/custom_components/octopus_energy/gas/standing_charge.py @@ -96,8 +96,9 @@ 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 OctopusEnergyGasCurrentStandingCharge state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/greenness_forecast/current_index.py b/custom_components/octopus_energy/greenness_forecast/current_index.py index 113a1ac8..d0c48f26 100644 --- a/custom_components/octopus_energy/greenness_forecast/current_index.py +++ b/custom_components/octopus_energy/greenness_forecast/current_index.py @@ -105,8 +105,9 @@ 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 OctopusEnergyGreennessForecastCurrentIndex state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/greenness_forecast/next_index.py b/custom_components/octopus_energy/greenness_forecast/next_index.py index eb1eba81..dde3e37a 100644 --- a/custom_components/octopus_energy/greenness_forecast/next_index.py +++ b/custom_components/octopus_energy/greenness_forecast/next_index.py @@ -99,8 +99,9 @@ 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 OctopusEnergyGreennessForecastNextIndex state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_humidity.py b/custom_components/octopus_energy/heat_pump/sensor_humidity.py index ea04da7d..bf04675c 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_humidity.py +++ b/custom_components/octopus_energy/heat_pump/sensor_humidity.py @@ -104,9 +104,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, []) self._attributes["type"] = self._sensor.type diff --git a/custom_components/octopus_energy/heat_pump/sensor_lifetime_energy_input.py b/custom_components/octopus_energy/heat_pump/sensor_lifetime_energy_input.py index c2135744..fe5dbcee 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_lifetime_energy_input.py +++ b/custom_components/octopus_energy/heat_pump/sensor_lifetime_energy_input.py @@ -100,9 +100,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 OctopusEnergyHeatPumpSensorLifetimeEnergyInput state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_lifetime_heat_output.py b/custom_components/octopus_energy/heat_pump/sensor_lifetime_heat_output.py index 9a1de051..934060bb 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_lifetime_heat_output.py +++ b/custom_components/octopus_energy/heat_pump/sensor_lifetime_heat_output.py @@ -100,9 +100,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 OctopusEnergyHeatPumpSensorLifetimeHeatOutput state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_lifetime_scop.py b/custom_components/octopus_energy/heat_pump/sensor_lifetime_scop.py index 31cac1e1..c807d2af 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_lifetime_scop.py +++ b/custom_components/octopus_energy/heat_pump/sensor_lifetime_scop.py @@ -85,9 +85,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 OctopusEnergyHeatPumpSensorLifetimeSCoP state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_live_cop.py b/custom_components/octopus_energy/heat_pump/sensor_live_cop.py index d8221255..fca9bae4 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_live_cop.py +++ b/custom_components/octopus_energy/heat_pump/sensor_live_cop.py @@ -89,9 +89,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 OctopusEnergyHeatPumpSensorLiveCoP state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_live_heat_output.py b/custom_components/octopus_energy/heat_pump/sensor_live_heat_output.py index e8121d67..b9a62784 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_live_heat_output.py +++ b/custom_components/octopus_energy/heat_pump/sensor_live_heat_output.py @@ -101,9 +101,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 OctopusEnergyHeatPumpSensorLiveHeatOutput state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_live_outdoor_temperature.py b/custom_components/octopus_energy/heat_pump/sensor_live_outdoor_temperature.py index 361af0b2..c9941354 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_live_outdoor_temperature.py +++ b/custom_components/octopus_energy/heat_pump/sensor_live_outdoor_temperature.py @@ -101,9 +101,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 OctopusEnergyHeatPumpSensorLiveOutdoorTemperature state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_live_power_input.py b/custom_components/octopus_energy/heat_pump/sensor_live_power_input.py index 6d43946f..da47f89c 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_live_power_input.py +++ b/custom_components/octopus_energy/heat_pump/sensor_live_power_input.py @@ -101,9 +101,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 OctopusEnergyHeatPumpSensorLivePowerInput state: {self._state}') \ No newline at end of file diff --git a/custom_components/octopus_energy/heat_pump/sensor_temperature.py b/custom_components/octopus_energy/heat_pump/sensor_temperature.py index 0d71cb22..5cc975b6 100644 --- a/custom_components/octopus_energy/heat_pump/sensor_temperature.py +++ b/custom_components/octopus_energy/heat_pump/sensor_temperature.py @@ -105,9 +105,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, []) self._attributes["type"] = self._sensor.type diff --git a/custom_components/octopus_energy/intelligent/current_state.py b/custom_components/octopus_energy/intelligent/current_state.py index 264bd788..f270413d 100644 --- a/custom_components/octopus_energy/intelligent/current_state.py +++ b/custom_components/octopus_energy/intelligent/current_state.py @@ -82,8 +82,9 @@ 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 OctopusEnergyIntelligentCurrentState state: {self._state}') diff --git a/custom_components/octopus_energy/octoplus/free_electricity_session_baseline.py b/custom_components/octopus_energy/octoplus/free_electricity_session_baseline.py index ad158dc9..f6f71f97 100644 --- a/custom_components/octopus_energy/octoplus/free_electricity_session_baseline.py +++ b/custom_components/octopus_energy/octoplus/free_electricity_session_baseline.py @@ -170,9 +170,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() - - if state is not None: - self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state + last_sensor_state = await self.async_get_last_sensor_data() + + 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}') diff --git a/custom_components/octopus_energy/octoplus/points.py b/custom_components/octopus_energy/octoplus/points.py index 39e2dddf..2d703d61 100644 --- a/custom_components/octopus_energy/octoplus/points.py +++ b/custom_components/octopus_energy/octoplus/points.py @@ -82,9 +82,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() - - if state is not None and self._state is None: - self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state + last_sensor_state = await self.async_get_last_sensor_data() + + 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 OctopusEnergyOctoplusPoints state: {self._state}') diff --git a/custom_components/octopus_energy/octoplus/saving_session_baseline.py b/custom_components/octopus_energy/octoplus/saving_session_baseline.py index 74ce9b11..c014e9c9 100644 --- a/custom_components/octopus_energy/octoplus/saving_session_baseline.py +++ b/custom_components/octopus_energy/octoplus/saving_session_baseline.py @@ -171,9 +171,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() - - if state is not None: - self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state + last_sensor_state = await self.async_get_last_sensor_data() + + 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}') diff --git a/custom_components/octopus_energy/wheel_of_fortune/electricity_spins.py b/custom_components/octopus_energy/wheel_of_fortune/electricity_spins.py index 515af446..d2bd2751 100644 --- a/custom_components/octopus_energy/wheel_of_fortune/electricity_spins.py +++ b/custom_components/octopus_energy/wheel_of_fortune/electricity_spins.py @@ -80,9 +80,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() - - if state is not None and self._state is None: - self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state + last_sensor_state = await self.async_get_last_sensor_data() + + 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 OctopusEnergyWheelOfFortuneElectricitySpins state: {self._state}') diff --git a/custom_components/octopus_energy/wheel_of_fortune/gas_spins.py b/custom_components/octopus_energy/wheel_of_fortune/gas_spins.py index 0b26203a..e4287d51 100644 --- a/custom_components/octopus_energy/wheel_of_fortune/gas_spins.py +++ b/custom_components/octopus_energy/wheel_of_fortune/gas_spins.py @@ -80,9 +80,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() - - if state is not None and self._state is None: - self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state + last_sensor_state = await self.async_get_last_sensor_data() + + 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 OctopusEnergyWheelOfFortuneGasSpins state: {self._state}')