Skip to content

Commit

Permalink
Merge pull request #392 from elafontaine/main
Browse files Browse the repository at this point in the history
Handle utility_meter Tariffs list to always exist

Finish fixing device tarif utitity meters.
  • Loading branch information
ic-dev21 authored Mar 16, 2024
2 parents e1f5367 + c851ec7 commit dee5b7e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 10 deletions.
17 changes: 16 additions & 1 deletion custom_components/hilo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ def check_tarif(self):
known_power += int(float(state.state))
except ValueError:
pass
if not entity.startswith("sensor.hilo_energy") or entity.endswith("_cost"):
if not entity.endswith("_hilo_energy") or entity.endswith("_cost"):
continue
self.fix_utility_sensor(entity, state)
if self.track_unknown_sources:
Expand Down Expand Up @@ -658,6 +658,21 @@ def set_tarif(self, entity, current, new):
SELECT_DOMAIN, SERVICE_SELECT_OPTION, data, context=context
)
)
if (
entity.startswith("select.")
and entity.endswith("_hilo_energy")
and current != new
):
LOG.debug(
f"check_tarif: Changing tarif of {entity} from {current} to {new}"
)
context = Context()
data = {ATTR_OPTION: new, "entity_id": entity}
self._hass.async_create_task(
self._hass.services.async_call(
SELECT_DOMAIN, SERVICE_SELECT_OPTION, data, context=context
)
)

@callback
def async_migrate_unique_id(
Expand Down
20 changes: 13 additions & 7 deletions custom_components/hilo/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

from homeassistant.components.energy.data import async_get_manager
from homeassistant.components.utility_meter import async_setup as utility_setup
from homeassistant.components.utility_meter.const import DOMAIN as UTILITY_DOMAIN
from homeassistant.components.utility_meter.const import (
CONF_TARIFFS,
DOMAIN as UTILITY_DOMAIN,
)
from homeassistant.components.utility_meter.sensor import (
async_setup_platform as utility_setup_platform,
)
Expand All @@ -14,7 +17,8 @@
class UtilityManager:
"""Class that maps to the utility_meters"""

def __init__(self, hass, period):
def __init__(self, hass, period, tariffs):
self.tariffs = tariffs
self.hass = hass
self.period = period
self.meter_configs = OrderedDict()
Expand Down Expand Up @@ -50,7 +54,7 @@ def add_meter_config(self, entity, tariff_list, net_consumption):
"source": f"sensor.{entity}",
"name": name,
"cycle": self.period,
"tariffs": tariff_list,
CONF_TARIFFS: tariff_list,
"net_consumption": net_consumption,
"utility_meter_sensors": [],
"offset": timedelta(0),
Expand All @@ -65,10 +69,12 @@ async def update(self, async_add_entities):
if self.new_entities == 0:
LOG.debug("No new entities, not setting up again")
return
config = {}
config[UTILITY_DOMAIN] = OrderedDict(
{**self.hass.data.get("utility_meter_data", {}), **self.meter_configs}
)
config = {
UTILITY_DOMAIN: OrderedDict(
{**self.hass.data.get("utility_meter_data", {}), **self.meter_configs}
),
CONF_TARIFFS: self.tariffs,
}
await utility_setup(self.hass, config)
await utility_setup_platform(
self.hass, config, async_add_entities, self.meter_entities
Expand Down
2 changes: 1 addition & 1 deletion custom_components/hilo/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@
"iot_class": "cloud_push",
"issue_tracker": "https://github.com/dvd-dev/hilo/issues",
"requirements": ["python-hilo>=2024.3.1"],
"version": "2024.3.2"
"version": "2024.3.3"
}
2 changes: 1 addition & 1 deletion custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ async def async_setup_entry(
default_tariff_list = validate_tariff_list(tariff_config)
if generate_energy_meters:
energy_manager = await EnergyManager().init(hass, energy_meter_period)
utility_manager = UtilityManager(hass, energy_meter_period)
utility_manager = UtilityManager(hass, energy_meter_period, default_tariff_list)

def create_energy_entity(hilo, device):
device._energy_entity = EnergySensor(hilo, device)
Expand Down

0 comments on commit dee5b7e

Please sign in to comment.