Skip to content

Commit

Permalink
multiple utility_meters and tariff sensors
Browse files Browse the repository at this point in the history
  • Loading branch information
dgomes committed Aug 6, 2021
1 parent ce8e163 commit 1b0d634
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 24 deletions.
10 changes: 5 additions & 5 deletions custom_components/erse/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
CONF_PLAN,
CONF_CYCLE,
CONF_POWER_COST,
CONF_UTILITY_METER,
CONF_UTILITY_METERS,
CONF_METER_SUFFIX,
DOMAIN,
)
Expand Down Expand Up @@ -87,7 +87,7 @@ async def async_step_utility_meter(self, user_input=None):
step_id="utility_meter",
data_schema=vol.Schema(
{
vol.Optional(CONF_UTILITY_METER): vol.In(
vol.Optional(CONF_UTILITY_METERS): cv.multi_select(
[
s.entity_id
for s in self.hass.states.async_all()
Expand All @@ -98,8 +98,8 @@ async def async_step_utility_meter(self, user_input=None):
),
)

if CONF_UTILITY_METER in user_input:
self.info[CONF_UTILITY_METER] = user_input[CONF_UTILITY_METER]
if CONF_UTILITY_METERS in user_input:
self.info[CONF_UTILITY_METERS] = user_input[CONF_UTILITY_METERS]
return await self.async_step_costs()

async def async_step_costs(self, user_input=None):
Expand All @@ -115,7 +115,7 @@ async def async_step_costs(self, user_input=None):
for tariff in self.operator.plano.tarifas
},
**{
vol.Required(tariff.name + CONF_METER_SUFFIX): vol.In(
vol.Required(tariff.name + CONF_METER_SUFFIX): cv.multi_select(
[
s.entity_id
for s in self.hass.states.async_all()
Expand Down
38 changes: 19 additions & 19 deletions custom_components/erse/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,14 @@

from .const import (
CONF_METER_SUFFIX,
CONF_UTILITY_METER,
CONF_UTILITY_METERS,
DOMAIN,
)

_LOGGER = logging.getLogger(__name__)

ATTR_TARIFFS = "tariffs"
ATTR_UTILITY_METER = "utility meter"
ATTR_UTILITY_METERS = "utility meters"

ICON = "mdi:transmission-tower"

Expand All @@ -54,17 +54,16 @@ async def async_setup_entry(hass, config_entry, async_add_entities):

entities = list()

if CONF_UTILITY_METER in config_entry.data:
if CONF_UTILITY_METERS in config_entry.data:
entities.append(
EletricityEntity(
hass, config_entry.entry_id, config_entry.data[CONF_UTILITY_METER]
hass, config_entry.entry_id, config_entry.data[CONF_UTILITY_METERS]
)
)

for tariff in hass.data[DOMAIN][config_entry.entry_id].plano.tarifas:
meter_entity = config_entry.data[f"{tariff.name}{CONF_METER_SUFFIX}"]

entities.append(TariffCost(hass, config_entry.entry_id, tariff, meter_entity))
for meter_entity in config_entry.data[f"{tariff.name}{CONF_METER_SUFFIX}"]:
entities.append(TariffCost(hass, config_entry.entry_id, tariff, meter_entity))

entities.append(FixedCost(hass, config_entry.entry_id, meter_entity))

Expand All @@ -84,7 +83,7 @@ def __init__(self, hass, entry_id, tariff, meter_entity):
self._attr_last_reset = dt_util.utc_from_timestamp(0)

self._attr_name = f"{self.operator} {tariff} cost"
self._attr_unique_id = slugify(f"{entry_id} {tariff} cost")
self._attr_unique_id = slugify(f"{entry_id} {meter_entity} {tariff} cost")

self._tariff = tariff
self._meter_entity = meter_entity
Expand Down Expand Up @@ -151,7 +150,7 @@ def __init__(self, hass, entry_id, any_meter) -> None:
self._attr_last_reset = dt_util.utc_from_timestamp(0)

self._attr_name = f"{self.operator} cost"
self._attr_unique_id = slugify(f"{entry_id} fixed cost")
self._attr_unique_id = slugify(f"{entry_id} {any_meter} fixed cost")

self._meter = any_meter

Expand Down Expand Up @@ -188,14 +187,14 @@ async def timer_update(self, now):
class EletricityEntity(Entity):
"""Representation of an Electricity Tariff tracker."""

def __init__(self, hass, entry_id, utility_meter):
def __init__(self, hass, entry_id, utility_meters):
"""Initialize an Electricity Tariff Tracker."""
self.operator = hass.data[DOMAIN][entry_id]
self._attr_name = str(self.operator)
self.utility_meter = utility_meter
self._utility_meters = utility_meters
self._state = None
self._attr_icon = ICON
self._attr_unique_id = slugify(f"{entry_id} {self.utility_meter}")
self._attr_unique_id = slugify(f"{entry_id} utility_meters {len(self._utility_meters)}")

async def async_added_to_hass(self):
"""Setups all required entities and automations."""
Expand Down Expand Up @@ -224,12 +223,13 @@ async def timer_update(self, now):

await self.async_update_ha_state()

_LOGGER.debug("Change %s to %s", self.utility_meter, self._state)
await self.hass.services.async_call(
UTILITY_METER_DOMAIN,
SERVICE_SELECT_TARIFF,
{ATTR_ENTITY_ID: self.utility_meter, ATTR_TARIFF: self._state},
)
for utility_meter in self._utility_meters:
_LOGGER.debug("Change %s to %s", utility_meter, self._state)
await self.hass.services.async_call(
UTILITY_METER_DOMAIN,
SERVICE_SELECT_TARIFF,
{ATTR_ENTITY_ID: utility_meter, ATTR_TARIFF: self._state},
)

@property
def should_poll(self):
Expand All @@ -246,6 +246,6 @@ def device_state_attributes(self):
"""Return the state attributes."""
attr = {
ATTR_TARIFFS: self.operator.plano.tarifas,
ATTR_UTILITY_METER: self.utility_meter,
ATTR_UTILITY_METERS: self._utility_meters,
}
return attr

0 comments on commit 1b0d634

Please sign in to comment.