Skip to content

Commit

Permalink
Relying on device type instead of name for finding SmartMeter
Browse files Browse the repository at this point in the history
Apparently, some people are having their smartmeter named as meter00. We
shouldn't rely on device.name for detecting but on device.type.

Also, creating HILO_ENERGY_TOTAL constant moving some blocks around.

Fixes #15
  • Loading branch information
valleedelisle committed Dec 25, 2021
1 parent af7a24e commit 3cf83c3
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 31 deletions.
3 changes: 2 additions & 1 deletion custom_components/hilo/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
DEFAULT_HQ_PLAN_NAME,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
HILO_ENERGY_TOTAL,
LOG,
)

Expand Down Expand Up @@ -370,7 +371,7 @@ def high_times(self):

def check_tarif(self):
tarif = "low"
base_sensor = "sensor.hilo_energy_total_daily_low"
base_sensor = f"sensor.{HILO_ENERGY_TOTAL}_daily_low"
energy_used = self._hass.states.get(base_sensor)
if not energy_used:
LOG.warning(f"check_tarif: Unable to find state for {base_sensor}")
Expand Down
2 changes: 2 additions & 0 deletions custom_components/hilo/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
DEFAULT_ENERGY_METER_PERIOD = DAILY
TARIFF_LIST = ["high", "medium", "low"]

HILO_ENERGY_TOTAL = "hilo_energy_total"

DEFAULT_SCAN_INTERVAL = 60
MIN_SCAN_INTERVAL = 15

Expand Down
4 changes: 2 additions & 2 deletions custom_components/hilo/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
async_setup_platform as utility_setup_platform,
)

from .const import LOG, TARIFF_LIST
from .const import HILO_ENERGY_TOTAL, LOG, TARIFF_LIST


class UtilityManager:
Expand Down Expand Up @@ -116,7 +116,7 @@ def add_device(self, sensor):
def add_to_dashboard(self, entity):
for tarif in TARIFF_LIST:
name = f"{entity}_{self.period}"
if entity == "hilo_energy_total":
if entity == HILO_ENERGY_TOTAL:
self.add_flow_from(f"{name}_{tarif}", f"hilo_rate_{tarif}")
else:
self.add_device(f"{name}_{tarif}")
Expand Down
55 changes: 27 additions & 28 deletions custom_components/hilo/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
DEFAULT_HQ_PLAN_NAME,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
HILO_ENERGY_TOTAL,
LOG,
)
from .managers import EnergyManager, UtilityManager
Expand All @@ -46,12 +47,9 @@
async def async_setup_entry(
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
) -> None:
"""Set up Hilo power sensors based on a config entry."""
"""Set up Hilo sensors based on a config entry."""
hilo = hass.data[DOMAIN][entry.entry_id]
power_entities = []
temperature_entities = []
misc_entities = []
energy_entities = []
new_entities = []
cost_entities = []
hq_plan_name = entry.options.get(CONF_HQ_PLAN_NAME, DEFAULT_HQ_PLAN_NAME)
energy_meter_period = entry.options.get(
Expand All @@ -65,45 +63,44 @@ async def async_setup_entry(
energy_manager = await EnergyManager().init(hass, energy_meter_period)
utility_manager = UtilityManager(energy_meter_period)

def create_energy_entity(d):
d._energy_entity = EnergySensor(d)
energy_entities.append(d._energy_entity)
energy_entity = f"hilo_energy_{slugify(d.name)}"
if energy_entity == "hilo_energy_total":
def create_energy_entity(device):
device._energy_entity = EnergySensor(device)
new_entities.append(device._energy_entity)
energy_entity = f"hilo_energy_{slugify(device.name)}"
if energy_entity == HILO_ENERGY_TOTAL:
LOG.error(
"An hilo entity can't be named 'total' because it conflicts with the generate name for the smart energy meter"
"An hilo entity can't be named 'total' because it conflicts "
"with the generated name for the smart energy meter"
)
return
if d.name == "SmartEnergyMeter":
energy_entity = "hilo_energy_total"
if device.type == "Meter":
energy_entity = HILO_ENERGY_TOTAL
utility_manager.add_meter(energy_entity)
energy_manager.add_to_dashboard(energy_entity)

for d in hilo.devices.all:
LOG.debug(f"Adding device {d}")
if d.name == "Hilo Gateway":
misc_entities.extend(
if d.type == "Gateway":
new_entities.extend(
[
HiloChallengeSensor(hilo, d, scan_interval),
DeviceSensor(hilo, d),
]
)
elif d.type == "Thermostat":
d._temperature_entity = TemperatureSensor(hilo, d)
new_entities.append(d._temperature_entity)
elif d.type in ["SmokeDetector", "IndoorWeatherStation"]:
d._device_sensor_entity = DeviceSensor(hilo, d)
new_entities.append(d._device_sensor_entity)
if d.has_attribute("power"):
d._power_entity = PowerSensor(hilo, d)
power_entities.append(d._power_entity)
new_entities.append(d._power_entity)
# If we opt out the geneneration of meters we just create the power sensors
if generate_energy_meters:
create_energy_entity(d)
if d.has_attribute("current_temperature"):
d._temperature_entity = TemperatureSensor(hilo, d)
temperature_entities.append(d._temperature_entity)
if d.type in ["SmokeDetector", "IndoorWeatherStation"]:
d._device_sensor_entity = DeviceSensor(hilo, d)
misc_entities.append(d._device_sensor_entity)

async_add_entities(
power_entities + energy_entities + temperature_entities + misc_entities
)
async_add_entities(new_entities)
if not generate_energy_meters:
return
# Creating cost sensors based on plan
Expand Down Expand Up @@ -252,8 +249,8 @@ def __init__(self, device):
self._attr_name = f"hilo_energy_{slugify(device.name)}"
self._unit_of_measurement = ENERGY_WATT_HOUR
self._unit_prefix = None
if device.name == "SmartEnergyMeter":
self._attr_name = "hilo_energy_total"
if device.type == "Meter":
self._attr_name = HILO_ENERGY_TOTAL
self._unit_of_measurement = ENERGY_KILO_WATT_HOUR
self._unit_prefix = "k"
if device.type == "Thermostat":
Expand All @@ -272,7 +269,9 @@ def __init__(self, device):
)
self._state = 0
self._last_period = 0
LOG.debug(f"Setting up EnergySensor entity: {self._attr_name}")
LOG.debug(
f"Setting up EnergySensor entity: {self._attr_name} with source {self._source}"
)

@property
def unit_of_measurement(self):
Expand Down

0 comments on commit 3cf83c3

Please sign in to comment.