Skip to content

Commit

Permalink
fix: Fixed restoring of intelligent states
Browse files Browse the repository at this point in the history
  • Loading branch information
BottlecapDave committed Jan 2, 2024
1 parent 8ed5496 commit faa3a32
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 5 deletions.
19 changes: 17 additions & 2 deletions custom_components/octopus_energy/intelligent/bump_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
from ..api_client import OctopusEnergyApiClient
from . import is_in_bump_charge
from ..coordinators.intelligent_dispatches import IntelligentDispatchesCoordinatorResult

from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -85,4 +85,19 @@ async def async_turn_off(self):
)
self._state = False
self._last_updated = utcnow()
self.async_write_ha_state()
self.async_write_ha_state()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
# 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 == "unknown" else state.state
self._attributes = dict_to_typed_dict(state.attributes)

if (self._state is None):
self._state = False

_LOGGER.debug(f'Restored OctopusEnergyIntelligentBumpCharge state: {self._state}')
19 changes: 18 additions & 1 deletion custom_components/octopus_energy/intelligent/ready_time.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
from datetime import time
import time as time_time
from custom_components.octopus_energy.utils.attributes import dict_to_typed_dict

from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import generate_entity_id
Expand Down Expand Up @@ -77,4 +79,19 @@ async def async_set_value(self, value: time) -> None:
)
self._state = value
self._last_updated = utcnow()
self.async_write_ha_state()
self.async_write_ha_state()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
# 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 == "unknown" else time_time.strptime(state.state, "%H:%M:%S")
self._attributes = dict_to_typed_dict(state.attributes)

if (self._state is None):
self._state = False

_LOGGER.debug(f'Restored OctopusEnergyIntelligentReadyTime state: {self._state}')
19 changes: 17 additions & 2 deletions custom_components/octopus_energy/intelligent/smart_charge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from .base import OctopusEnergyIntelligentSensor
from ..api_client import OctopusEnergyApiClient
from ..coordinators.intelligent_settings import IntelligentCoordinatorResult

from ..utils.attributes import dict_to_typed_dict

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -84,4 +84,19 @@ async def async_turn_off(self):
)
self._state = False
self._last_updated = utcnow()
self.async_write_ha_state()
self.async_write_ha_state()

async def async_added_to_hass(self):
"""Call when entity about to be added to hass."""
# 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 == "unknown" else state.state
self._attributes = dict_to_typed_dict(state.attributes)

if (self._state is None):
self._state = False

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

0 comments on commit faa3a32

Please sign in to comment.