Skip to content

Commit

Permalink
Add ability to set groups as data sources.
Browse files Browse the repository at this point in the history
  • Loading branch information
Limych committed Sep 7, 2020
1 parent a613d27 commit 0c0bfc5
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ I put a lot of work into making this repo and component available and updated to
> **_Note_**:\
> You can use weather provider, climate and water heater entities as a data source. For that entities sensor use values of current temperature.
> **_Note_**:\
> You can use groups of entities as a data source. These groups will be automatically expanded to individual entities.
**name**:\
_(string) (Optional)_\
Name to use in the frontend.\
Expand Down Expand Up @@ -132,8 +135,11 @@ I put a lot of work into making this repo and component available and updated to
**end**:\
Timestamp of the end of the calculation period (if period was set).
**sources**:\
Total expanded list of source sensors.
**count_sources**:\
Total count of set source sensors.
Total count of source sensors.
**available_sources**:\
Count of available source sensors (for current calculation period).
Expand Down
2 changes: 2 additions & 0 deletions custom_components/average/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

ATTR_START = "start"
ATTR_END = "end"
ATTR_SOURCES = "sources"
ATTR_COUNT_SOURCES = "count_sources"
ATTR_AVAILABLE_SOURCES = "available_sources"
ATTR_COUNT = "count"
Expand All @@ -26,6 +27,7 @@
ATTR_TO_PROPERTY = [
ATTR_START,
ATTR_END,
ATTR_SOURCES,
ATTR_COUNT_SOURCES,
ATTR_AVAILABLE_SOURCES,
ATTR_COUNT,
Expand Down
10 changes: 6 additions & 4 deletions custom_components/average/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import voluptuous as vol
from homeassistant.components import history
from homeassistant.components.climate import ClimateDevice
from homeassistant.components.group import expand_entity_ids
from homeassistant.components.water_heater import WaterHeaterDevice
from homeassistant.components.weather import WeatherEntity
from homeassistant.const import (
Expand Down Expand Up @@ -138,14 +139,15 @@ def __init__(
self._end_template = end
self._duration = duration
self._period = self.start = self.end = None
self._entity_ids = entity_ids
self._precision = precision
self._undef = undef
self._state = None
self._unit_of_measurement = None
self._icon = None
self._temperature_mode = None
self.count_sources = len(self._entity_ids)

self.sources = expand_entity_ids(hass, entity_ids)
self.count_sources = len(self.sources)
self.available_sources = 0
self.count = 0
self.min_value = self.max_value = None
Expand Down Expand Up @@ -218,7 +220,7 @@ def sensor_startup(event):
self.async_schedule_update_ha_state(True)
else:
async_track_state_change(
self._hass, self._entity_ids, sensor_state_listener
self._hass, self.sources, sensor_state_listener
)
sensor_state_listener(None, None, None)

Expand Down Expand Up @@ -419,7 +421,7 @@ def _update_state(self): # pylint: disable=r0914,r0912,r0915
self.min_value = self.max_value = None

# pylint: disable=too-many-nested-blocks
for entity_id in self._entity_ids:
for entity_id in self.sources:
_LOGGER.debug('Processing entity "%s"', entity_id)

entity = self._hass.states.get(entity_id)
Expand Down

0 comments on commit 0c0bfc5

Please sign in to comment.