From ef7f7e8a3601a3ec270bee33ea15c47639d7281e Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Sat, 27 Jan 2024 07:39:06 -0500 Subject: [PATCH 1/4] =?UTF-8?q?Optimisation=20cr=C3=A9ation=20energy=20sen?= =?UTF-8?q?sors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les energysensors sont une belle patate chaude mal aimée. Changé _unit_of_measurement pour _attr_unit_of_measurement car natif HA et mieux géré. Lors de la création les sensors restent "unknown" jusqu'à consommation mais ne nécessite pas de "fix issue" d'unité. La méthode fix_utility_sensor dans __init.py__ devient inutile/cause plus de trouble qu'elle en règle car l'unité est mal gérée par celle-ci. Je propose soit de la retirer et que les gens attendent quelques jours que les données se populent seules, ou de comprendre pourquoi elle siphonne mal les unités. --- custom_components/hilo/__init__.py | 2 +- custom_components/hilo/sensor.py | 10 ++++------ 2 files changed, 5 insertions(+), 7 deletions(-) diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index 3a3f932..8084eef 100755 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -570,7 +570,7 @@ def check_tarif(self): pass if not entity.startswith("sensor.hilo_energy") or entity.endswith("_cost"): continue - self.fix_utility_sensor(entity, state) + #self.fix_utility_sensor(entity, state) note ic-dev21: commented this out as this method does not work properly right now if self.track_unknown_sources: total_power = self._hass.states.get(smart_meter) if not total_power: diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index 27a28f2..d8ae9bb 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -241,19 +241,16 @@ class EnergySensor(IntegrationSensor): _attr_device_class = SensorDeviceClass.ENERGY _attr_native_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR _attr_state_class = SensorStateClass.TOTAL_INCREASING + _attr_suggested_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR + _attr_unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR _attr_icon = "mdi:lightning-bolt" def __init__(self, device): self._device = device self._attr_name = f"Hilo Energy {slugify(device.name)}" self._attr_unique_id = f"hilo_energy_{slugify(device.name)}" - self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR - self._unit_prefix = None if device.type == "Meter": self._attr_name = HILO_ENERGY_TOTAL - self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR - if device.type == "Thermostat" or device.type == "FloorThermostat": - self._unit_of_measurement = UnitOfEnergy.KILO_WATT_HOUR self._source = f"sensor.{slugify(device.name)}_power" super().__init__( @@ -273,7 +270,7 @@ def __init__(self, device): @property def unit_of_measurement(self): - return self._unit_of_measurement + return self._attr_unit_of_measurement async def async_added_to_hass(self) -> None: """Handle entity which will be added.""" @@ -499,6 +496,7 @@ class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity): _attr_device_class = SensorDeviceClass.MONETARY _attr_state_class = SensorStateClass.TOTAL_INCREASING + _attr_suggested_unit_of_measurement = "CAD" def __init__(self, hilo, device, scan_interval): self._attr_name = "Recompenses Hilo" From 4f8c957ba3a88ea371e2a9fceff9f44cc14c402c Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Sun, 28 Jan 2024 16:24:06 -0500 Subject: [PATCH 2/4] Fix fix_utility_sensor function MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Les attributs étaient mal gérés et la fonction finissait par nous mettre des "W" là où on devrait être en "kWh" Permet à la fonction de faire ce qu'elle devait faire. --- custom_components/hilo/__init__.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/custom_components/hilo/__init__.py b/custom_components/hilo/__init__.py index 8084eef..df60260 100755 --- a/custom_components/hilo/__init__.py +++ b/custom_components/hilo/__init__.py @@ -570,7 +570,7 @@ def check_tarif(self): pass if not entity.startswith("sensor.hilo_energy") or entity.endswith("_cost"): continue - #self.fix_utility_sensor(entity, state) note ic-dev21: commented this out as this method does not work properly right now + self.fix_utility_sensor(entity, state) if self.track_unknown_sources: total_power = self._hass.states.get(smart_meter) if not total_power: @@ -600,14 +600,19 @@ def fix_utility_sensor(self, entity, state): if not attrs.get("source"): LOG.debug(f"No source entity defined on {entity}: {current_state}") return - parent_unit = self._hass.states.get(attrs.get("source")) + + parent_unit_state = self._hass.states.get(attrs.get("source")) + parent_unit = ( + "kWh" + if parent_unit_state is None + else parent_unit_state.attributes.get("unit_of_measurement") + ) if not parent_unit: LOG.warning(f"Unable to find state for parent unit: {current_state}") return + new_attrs = { - ATTR_UNIT_OF_MEASUREMENT: parent_unit.as_dict() - .get("attributes", {}) - .get(ATTR_UNIT_OF_MEASUREMENT), + ATTR_UNIT_OF_MEASUREMENT: parent_unit, # note ic-dev21: now uses parent_unit directly ATTR_DEVICE_CLASS: SensorDeviceClass.ENERGY, } if not all(a in attrs.keys() for a in new_attrs.keys()): From aec62382ca369a0e458304d13082888611aa265c Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Sun, 28 Jan 2024 20:37:26 -0500 Subject: [PATCH 3/4] Handle currency MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Fix pour changement d'unité du HiloRewardSensor dans le log. --- custom_components/hilo/sensor.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index d8ae9bb..3645de9 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -500,11 +500,19 @@ class HiloRewardSensor(HiloEntity, RestoreEntity, SensorEntity): def __init__(self, hilo, device, scan_interval): self._attr_name = "Recompenses Hilo" + + # Check if currency is configured, set a default if not + currency = hilo._hass.config.currency + if currency: + self._attr_native_unit_of_measurement = currency + else: + # Set a default currency or handle the case where currency is not configured + self._attr_native_unit_of_measurement = "CAD" + super().__init__(hilo, name=self._attr_name, device=device) self._attr_unique_id = slugify(self._attr_name) LOG.debug(f"Setting up RewardSensor entity: {self._attr_name}") self.scan_interval = timedelta(seconds=REWARD_SCAN_INTERVAL) - self._attr_native_unit_of_measurement = hilo._hass.config.currency self._state = 0 self._history = [] self.async_update = Throttle(self.scan_interval)(self._async_update) From dd8bb18009e7ef6611add0d72d1a9cdbe4456c83 Mon Sep 17 00:00:00 2001 From: snaky69 <108159253+ic-dev21@users.noreply.github.com> Date: Mon, 29 Jan 2024 21:11:49 -0500 Subject: [PATCH 4/4] Prevent unavailable state on boot MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit En ajoutant un last_valid_state ça permet d'avoir un unknown plutôt qu'un unavailable. Évite des fuck lors de reboot. --- custom_components/hilo/sensor.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/custom_components/hilo/sensor.py b/custom_components/hilo/sensor.py index 3645de9..7e85f64 100755 --- a/custom_components/hilo/sensor.py +++ b/custom_components/hilo/sensor.py @@ -252,6 +252,11 @@ def __init__(self, device): if device.type == "Meter": self._attr_name = HILO_ENERGY_TOTAL self._source = f"sensor.{slugify(device.name)}_power" + # ic-dev21: Set initial state and last_valid_state, removes log errors and unavailable states + initial_state = 0 + self._attr_native_value = initial_state + self._attr_last_valid_state = initial_state + self._attr_last_reset = dt_util.utcnow() super().__init__( integration_method=METHOD_LEFT, @@ -262,8 +267,6 @@ def __init__(self, device): unit_prefix="k", unit_time="h", ) - self._state = 0 - self._last_period = 0 LOG.debug( f"Setting up EnergySensor entity: {self._attr_name} with source {self._source}" )