-
-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added sensor to show the current raw intelligent state (1 hour …
…dev time) (#1142)
- Loading branch information
Showing
12 changed files
with
225 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
custom_components/octopus_energy/intelligent/current_state.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import logging | ||
|
||
from homeassistant.const import ( | ||
STATE_UNAVAILABLE, | ||
STATE_UNKNOWN, | ||
) | ||
from homeassistant.core import HomeAssistant, callback | ||
from homeassistant.helpers.entity import generate_entity_id | ||
|
||
from homeassistant.util.dt import (now) | ||
from homeassistant.helpers.update_coordinator import ( | ||
CoordinatorEntity | ||
) | ||
from homeassistant.components.sensor import ( | ||
RestoreSensor, | ||
) | ||
|
||
from .base import OctopusEnergyIntelligentSensor | ||
from ..coordinators.intelligent_dispatches import IntelligentDispatchesCoordinatorResult | ||
from ..utils.attributes import dict_to_typed_dict | ||
from ..api_client.intelligent_device import IntelligentDevice | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class OctopusEnergyIntelligentCurrentState(CoordinatorEntity, OctopusEnergyIntelligentSensor, RestoreSensor): | ||
"""Sensor for determining the current intelligent state.""" | ||
|
||
def __init__(self, hass: HomeAssistant, coordinator, device: IntelligentDevice, account_id: str): | ||
"""Init sensor.""" | ||
|
||
CoordinatorEntity.__init__(self, coordinator) | ||
OctopusEnergyIntelligentSensor.__init__(self, device) | ||
|
||
self._account_id = account_id | ||
self._state = None | ||
|
||
self.entity_id = generate_entity_id("sensor.{}", self.unique_id, hass=hass) | ||
|
||
@property | ||
def unique_id(self): | ||
"""The id of the sensor.""" | ||
return f"octopus_energy_{self._account_id}_intelligent_state" | ||
|
||
@property | ||
def name(self): | ||
"""Name of the sensor.""" | ||
return f"Intelligent State ({self._account_id})" | ||
|
||
@property | ||
def icon(self): | ||
"""Icon of the sensor.""" | ||
return "mdi:power-plug-battery" | ||
|
||
@property | ||
def extra_state_attributes(self): | ||
"""Attributes of the sensor.""" | ||
return self._attributes | ||
|
||
@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 = now() | ||
result: IntelligentDispatchesCoordinatorResult = self.coordinator.data if self.coordinator is not None and self.coordinator.data is not None else None | ||
if (result is not None and result.dispatches is not None): | ||
_LOGGER.debug(f"Updating OctopusEnergyIntelligentCurrentState for '{self._device.id}'") | ||
|
||
self._state = result.dispatches.current_state | ||
else: | ||
self._state = None | ||
|
||
self._attributes = dict_to_typed_dict(self._attributes) | ||
super()._handle_coordinator_update() | ||
|
||
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 and self._state is None: | ||
self._state = None if state.state in (STATE_UNAVAILABLE, STATE_UNKNOWN) else state.state | ||
self._attributes = dict_to_typed_dict(state.attributes, []) | ||
_LOGGER.debug(f'Restored OctopusEnergyIntelligentCurrentState state: {self._state}') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.