diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index ea8cb0e..507dae2 100755 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -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: @@ -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( diff --git a/custom_components/hilo/managers.py b/custom_components/hilo/managers.py index 15f9b39..ef24dab 100644 --- a/custom_components/hilo/managers.py +++ b/custom_components/hilo/managers.py @@ -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, ) @@ -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() @@ -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), @@ -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 diff --git a/custom_components/hilo/manifest.json b/custom_components/hilo/manifest.json index ec4cb07..d27a404 100755 --- a/custom_components/hilo/manifest.json +++ b/custom_components/hilo/manifest.json @@ -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" } diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index 574b760..6bfe686 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -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)