From 5c224f423931c6d54a40cf4978711d48fb4890c0 Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Thu, 8 Feb 2024 19:41:33 -0500 Subject: [PATCH 1/2] Update managers.py MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C'est un début --- custom_components/hilo/managers.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/custom_components/hilo/managers.py b/custom_components/hilo/managers.py index 15f9b39..7f13c5a 100644 --- a/custom_components/hilo/managers.py +++ b/custom_components/hilo/managers.py @@ -9,7 +9,7 @@ ) from .const import HILO_ENERGY_TOTAL, LOG - +from homeassistant.helpers.entity_registry import async_get as async_get_registry class UtilityManager: """Class that maps to the utility_meters""" @@ -21,23 +21,28 @@ def __init__(self, hass, period): self.meter_entities = {} self.new_entities = 0 - def add_meter(self, entity, tariff_list, net_consumption=False): - self.add_meter_entity(entity, tariff_list) + async def entity_exists(self, entity): + """Check if the entity already exists.""" + registry = await async_get_registry(self.hass) + return registry.async_is_registered(entity) + + async def add_meter(self, entity, tariff_list, net_consumption=False): + await self.add_meter_entity(entity, tariff_list) self.add_meter_config(entity, tariff_list, net_consumption) - def add_meter_entity(self, entity, tariff_list): - if entity in self.hass.data.get("utility_meter_data", {}): + async def add_meter_entity(self, entity, tariff_list): + if await self.entity_exists(entity): LOG.debug(f"Entity {entity} is already in the utility meters") return self.new_entities += 1 - for tarif in tariff_list: + for tariff in tariff_list: name = f"{entity}_{self.period}" - meter_name = f"{name} {tarif}" + meter_name = f"{name} {tariff}" LOG.debug(f"Creating UtilityMeter entity for {entity}: {meter_name}") self.meter_entities[meter_name] = { "meter": entity, "name": meter_name, - "tariff": tarif, + "tariff": tariff, } def add_meter_config(self, entity, tariff_list, net_consumption): From 8589e9a03e14d0b279107a5a59b10ff8750bb48a Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Thu, 8 Feb 2024 19:42:15 -0500 Subject: [PATCH 2/2] Update managers.py linting --- custom_components/hilo/managers.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/custom_components/hilo/managers.py b/custom_components/hilo/managers.py index 7f13c5a..3ddebbe 100644 --- a/custom_components/hilo/managers.py +++ b/custom_components/hilo/managers.py @@ -7,9 +7,10 @@ from homeassistant.components.utility_meter.sensor import ( async_setup_platform as utility_setup_platform, ) +from homeassistant.helpers.entity_registry import async_get as async_get_registry from .const import HILO_ENERGY_TOTAL, LOG -from homeassistant.helpers.entity_registry import async_get as async_get_registry + class UtilityManager: """Class that maps to the utility_meters"""