Skip to content

Commit

Permalink
fix: fixed issue where sensor attributes were always strings when res…
Browse files Browse the repository at this point in the history
…tored after reload/restart
  • Loading branch information
BottlecapDave committed Nov 17, 2023
1 parent 7f6712f commit 53964ae
Show file tree
Hide file tree
Showing 52 changed files with 305 additions and 178 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from homeassistant.util.dt import (now)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from . import calculate_electricity_consumption_and_cost

Expand Down Expand Up @@ -124,5 +125,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeElectricityConsumption state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -124,11 +125,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityConsumptionOffPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -123,11 +124,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeElectricityConsumptionPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -137,11 +138,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeElectricityCost state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -120,11 +121,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeElectricityCostOffPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -120,11 +121,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentAccumulativeElectricityCostPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from ..utils.consumption import (get_current_consumption_delta, get_total_consumption)

Expand Down Expand Up @@ -106,5 +107,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentElectricityConsumption state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,5 +86,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyCurrentElectricityDemand state: {self._state}')
5 changes: 2 additions & 3 deletions custom_components/octopus_energy/electricity/current_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from ..utils.rate_information import (get_current_rate_information)

Expand Down Expand Up @@ -150,8 +151,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyElectricityCurrentRate state: {self._state}')
5 changes: 2 additions & 3 deletions custom_components/octopus_energy/electricity/next_rate.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict
from ..utils.rate_information import (get_next_rate_information)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -124,8 +125,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyElectricityNextRate state: {self._state}')
2 changes: 2 additions & 0 deletions custom_components/octopus_energy/electricity/off_peak.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from ..utils import is_off_peak

from .base import OctopusEnergyElectricitySensor
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -76,6 +77,7 @@ async def async_added_to_hass(self):

if state is not None:
self._state = state.state
self._attributes = dict_to_typed_dict(state.attributes)

if (self._state is None):
self._state = False
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from ..statistics.consumption import async_import_external_statistics_from_consumption, get_electricity_consumption_statistic_unique_id
from ..statistics.refresh import async_refresh_previous_electricity_consumption_data
Expand Down Expand Up @@ -158,12 +159,7 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -121,11 +122,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityConsumptionOffPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -121,11 +122,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityConsumptionPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from ..statistics.cost import async_import_external_statistics_from_cost, get_electricity_cost_statistic_unique_id

Expand Down Expand Up @@ -158,11 +159,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCost state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -119,11 +120,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCostOffPeak state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

from ..api_client import (OctopusEnergyApiClient)

Expand Down Expand Up @@ -174,11 +175,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCostOverride state: {self._state}')
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from ..api_client import OctopusEnergyApiClient

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -82,8 +83,6 @@ async def async_added_to_hass(self):
self._attr_state = state.state
self._hass.data[DOMAIN][get_electricity_tariff_override_key(self._serial_number, self._mpan)] = self._attr_native_value

self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]
self._attributes = dict_to_typed_dict(state.attributes)

_LOGGER.debug(f'Restored OctopusEnergyPreviousAccumulativeElectricityCostTariffOverride state: {self._attr_state}')
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
)

from .base import (OctopusEnergyElectricitySensor)
from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -119,11 +120,6 @@ async def async_added_to_hass(self):

if state is not None and self._state is None:
self._state = state.state
self._attributes = {}
for x in state.attributes.keys():
self._attributes[x] = state.attributes[x]

if x == "last_reset":
self._last_reset = datetime.strptime(state.attributes[x], "%Y-%m-%dT%H:%M:%S%z")
self._attributes = dict_to_typed_dict(state.attributes)

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

0 comments on commit 53964ae

Please sign in to comment.