Skip to content

Commit

Permalink
Making sure the utility_setup is ran only once
Browse files Browse the repository at this point in the history
I suspect that this is what's causing the spikes in energy meters when
the integration is reloaded. We do see the hilo_energy sensors creating
duplicates and there might be a link here. We'll need some time to test
this adequatly.

This is to address issue #35 which can't be closed until we're sure.
  • Loading branch information
valleedelisle committed Jan 15, 2022
1 parent d2df65f commit 1757d85
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
19 changes: 14 additions & 5 deletions custom_components/hilo/managers.py
Expand Up @@ -14,16 +14,22 @@
class UtilityManager:
"""Class that maps to the utility_meters"""

def __init__(self, period):
def __init__(self, hass, period):
self.hass = hass
self.period = period
self.meter_configs = OrderedDict()
self.meter_entities = []
self.new_entities = 0

def add_meter(self, entity, tariff_list):
self.add_meter_entity(entity, tariff_list)
self.add_meter_config(entity, tariff_list)

def add_meter_entity(self, entity, tariff_list):
if entity in self.hass.data.get("utility_meter_data", {}):
LOG.debug(f"Entity {entity} is already in the utility meters")
return
self.new_entities += 1
for tarif in tariff_list:
name = f"{entity}_{self.period}"
LOG.debug(f"Creating UtilityMeter entity: {name} {tarif}")
Expand All @@ -47,15 +53,18 @@ def add_meter_config(self, entity, tariff_list):
}
)

async def update(self, hass, async_add_entities):
async def update(self, async_add_entities):
LOG.debug(f"Setting up UtilityMeter entities {UTILITY_DOMAIN}")
if self.new_entities == 0:
LOG.debug("No new entities, not setting up again")
return
config = {}
config[UTILITY_DOMAIN] = OrderedDict(
{**hass.data.get("utility_meter_data", {}), **self.meter_configs}
{**self.hass.data.get("utility_meter_data", {}), **self.meter_configs}
)
await utility_setup(hass, config)
await utility_setup(self.hass, config)
await utility_setup_platform(
hass, config, async_add_entities, self.meter_entities
self.hass, config, async_add_entities, self.meter_entities
)


Expand Down
4 changes: 2 additions & 2 deletions custom_components/hilo/sensor.py
Expand Up @@ -139,7 +139,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(energy_meter_period)
utility_manager = UtilityManager(hass, energy_meter_period)

def create_energy_entity(device):
device._energy_entity = EnergySensor(device)
Expand Down Expand Up @@ -179,7 +179,7 @@ def create_energy_entity(device):
cost_entities.append(HiloCostSensor("hilo_rate_current", hq_plan_name))
async_add_entities(cost_entities)
# This setups the utility_meter platform
await utility_manager.update(hass, async_add_entities)
await utility_manager.update(async_add_entities)
# This sends the entities to the energy dashboard
await energy_manager.update()

Expand Down

0 comments on commit 1757d85

Please sign in to comment.