Skip to content

Commit

Permalink
Issue #698 nb device active for boiler not updating (#728)
Browse files Browse the repository at this point in the history
* With tests and list of active

* Issue #698 - nb nb_device_active_for_boiler don't work for climate without hvac_action

---------

Co-authored-by: Jean-Marc Collin <[email protected]>
  • Loading branch information
jmcollin78 and Jean-Marc Collin authored Dec 21, 2024
1 parent 9839ed4 commit ee42a23
Show file tree
Hide file tree
Showing 6 changed files with 421 additions and 47 deletions.
16 changes: 11 additions & 5 deletions custom_components/versatile_thermostat/base_thermostat.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ class BaseThermostat(ClimateEntity, RestoreEntity, Generic[T]):
"max_power_sensor_entity_id",
"temperature_unit",
"is_device_active",
"nb_device_actives",
"device_actives",
"target_temperature_step",
"is_used_by_central_boiler",
"temperature_slope",
Expand Down Expand Up @@ -1001,14 +1001,19 @@ def is_device_active(self) -> bool:
return False

@property
def nb_device_actives(self) -> int:
"""Calculate the number of active devices"""
ret = 0
def device_actives(self) -> int:
"""Calculate the active devices"""
ret = []
for under in self._underlyings:
if under.is_device_active:
ret += 1
ret.append(under.entity_id)
return ret

@property
def nb_device_actives(self) -> int:
"""Calculate the number of active devices"""
return len(self.device_actives)

@property
def current_temperature(self) -> float | None:
"""Return the sensor temperature."""
Expand Down Expand Up @@ -2680,6 +2685,7 @@ def update_custom_attributes(self):
"timezone": str(self._current_tz),
"temperature_unit": self.temperature_unit,
"is_device_active": self.is_device_active,
"device_actives": self.device_actives,
"nb_device_actives": self.nb_device_actives,
"ema_temp": self._ema_temp,
"is_used_by_central_boiler": self.is_used_by_central_boiler,
Expand Down
38 changes: 21 additions & 17 deletions custom_components/versatile_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,10 @@ class NbActiveDeviceForBoilerSensor(SensorEntity):
"""Representation of the threshold of the number of VTherm
which should be active to activate the boiler"""

_entity_component_unrecorded_attributes = SensorEntity._entity_component_unrecorded_attributes.union( # pylint: disable=protected-access
frozenset({"active_device_ids"})
)

def __init__(self, hass: HomeAssistant, unique_id, name, entry_infos) -> None:
"""Initialize the energy sensor"""
self._hass = hass
Expand All @@ -653,13 +657,13 @@ def __init__(self, hass: HomeAssistant, unique_id, name, entry_infos) -> None:
self._attr_unique_id = "nb_device_active_boiler"
self._attr_value = self._attr_native_value = None # default value
self._entities = []
self._attr_active_device_names = [] # Holds the names of active devices
self._attr_active_device_ids = [] # Holds the entity ids of active devices

@property
def extra_state_attributes(self) -> dict:
"""Return additional attributes for the sensor."""
return {
"active_device_names": self._attr_active_device_names,
"active_device_ids": self._attr_active_device_ids,
}

@property
Expand Down Expand Up @@ -765,6 +769,8 @@ async def calculate_nb_active_devices(self, event: Event):
old_state is not None
and new_state.state == old_state.state
and new_hvac_action == old_hvac_action
# issue 698 - force recalculation when underlying climate doesn't have any hvac_action
and new_hvac_action is not None
):
# A false state change
return
Expand All @@ -782,30 +788,28 @@ async def calculate_nb_active_devices(self, event: Event):
)

nb_active = 0
active_device_names = []
active_device_ids = []

for entity in self._entities:
nb_active += entity.nb_device_actives
device_actives = entity.device_actives
_LOGGER.debug(
"After examining the hvac_action of %s, nb_active is %s",
"After examining the hvac_action of %s, device_actives is %s",
entity.name,
nb_active,
device_actives,
)

if (
entity.hvac_mode in [HVACMode.HEAT, HVACMode.AUTO]
and entity.hvac_action == HVACAction.HEATING
):
for under in entity.underlying_entities:
if under.is_device_active:
nb_active += 1
active_device_names.append(under.entity_id)

nb_active += len(device_actives)
active_device_ids.extend(device_actives)

self._attr_native_value = nb_active
self._attr_active_device_names = active_device_names
self._attr_active_device_ids = active_device_ids

self.async_write_ha_state()

@property
def active_device_ids(self) -> list:
"""Get the list of active device id"""
return self._attr_active_device_ids

def __str__(self):
return f"VersatileThermostat-{self.name}"

Original file line number Diff line number Diff line change
Expand Up @@ -277,12 +277,15 @@ def is_device_active(self) -> bool:
return self.valve_open_percent > 0

@property
def nb_device_actives(self) -> int:
def device_actives(self) -> int:
"""Calculate the number of active devices"""
if self.is_device_active:
return len(self._underlyings_valve_regulation)
return [
under.opening_degree_entity_id
for under in self._underlyings_valve_regulation
]
else:
return 0
return []

@property
def activable_underlying_entities(self) -> list | None:
Expand Down
1 change: 1 addition & 0 deletions tests/commons.py
Original file line number Diff line number Diff line change
Expand Up @@ -579,6 +579,7 @@ def name(self) -> str:
def set_native_value(self, value: float):
"""Change the value"""
self._attr_native_value = value
self.async_write_ha_state()


async def create_thermostat(
Expand Down
Loading

0 comments on commit ee42a23

Please sign in to comment.