Skip to content

Commit

Permalink
Ha update to 2023.12 & HACS updates #14 #16
Browse files Browse the repository at this point in the history
  • Loading branch information
BeardedTinker committed Dec 6, 2023
1 parent 59ff0f3 commit efb3284
Show file tree
Hide file tree
Showing 47 changed files with 4,838 additions and 974 deletions.
2 changes: 1 addition & 1 deletion .HA_VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2023.11.3
2023.12.0
2 changes: 1 addition & 1 deletion custom_components/powercalc/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,5 +22,5 @@
"requirements": [
"numpy>=1.21.1"
],
"version": "v1.9.8"
"version": "v1.9.9"
}
4 changes: 3 additions & 1 deletion custom_components/powercalc/sensors/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,7 +496,9 @@ async def async_added_to_hass(self) -> None:
)
except DecimalException as err:
_LOGGER.warning(
"%s: Could not restore last state: %s", self.entity_id, err,
"%s: Could not restore last state: %s",
self.entity_id,
err,
)

state_listener = Throttle(timedelta(seconds=30))(state_listener)
Expand Down
32 changes: 23 additions & 9 deletions custom_components/powercalc/sensors/power.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,7 @@ def __init__(
) -> None:
"""Initialize the sensor."""
self._calculation_strategy = calculation_strategy
self._calculation_enabled_condition: Template | None = None
self._source_entity = source_entity
self._attr_name = name
self._power: Decimal | None = None
Expand Down Expand Up @@ -380,6 +381,7 @@ async def async_added_to_hass(self) -> None:
await super().async_added_to_hass()
await self.ensure_strategy_instance()
assert self._strategy_instance is not None
self.init_calculation_enabled_condition()

async def appliance_state_listener(event: Event) -> None:
"""Handle for state changes for dependent sensors."""
Expand Down Expand Up @@ -444,6 +446,10 @@ async def initial_update(hass: HomeAssistant) -> None:
if isinstance(self._standby_power, Template):
self._standby_power.hass = self.hass
track_templates.append(TrackTemplate(self._standby_power, None, None))
if self._calculation_enabled_condition:
track_templates.append(
TrackTemplate(self._calculation_enabled_condition, None, None)
)
if track_templates:
async_track_template_result(
self.hass,
Expand All @@ -463,6 +469,21 @@ def async_update(event_time: datetime | None = None) -> None:

async_track_time_interval(self.hass, async_update, self._update_frequency)

def init_calculation_enabled_condition(self) -> None:
if CONF_CALCULATION_ENABLED_CONDITION not in self._sensor_config:
return

template = self._sensor_config.get(CONF_CALCULATION_ENABLED_CONDITION)
if isinstance(template, str):
template = template.replace("[[entity]]", self.source_entity)
template = Template(template)

if not isinstance(template, Template):
_LOGGER.error("Invalid calculation_enabled_condition: %s", template)
return

self._calculation_enabled_condition = template

async def _handle_source_entity_state_change(
self,
trigger_entity_id: str,
Expand Down Expand Up @@ -620,17 +641,10 @@ def _update_sleep_power(*_: Any) -> None: # noqa: ANN401
return standby_power

async def is_calculation_enabled(self) -> bool:
if CONF_CALCULATION_ENABLED_CONDITION not in self._sensor_config:
template = self._calculation_enabled_condition
if not template:
return True

template = self._sensor_config.get(CONF_CALCULATION_ENABLED_CONDITION)
if isinstance(template, str):
template = template.replace("[[entity]]", self.source_entity)
template = Template(template)

if not isinstance(template, Template):
return True # pragma: no cover

template.hass = self.hass
return bool(template.async_render())

Expand Down
Loading

0 comments on commit efb3284

Please sign in to comment.