Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add attribute with active underlyings for easier tracking and setup #658

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions custom_components/versatile_thermostat/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,14 @@ 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

@property
def extra_state_attributes(self) -> dict:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add the active_device_names into the list of _entity_component_unrecorded_attributes so that it will not be persisted and save place on the hard drive.

See base_thermostat.py at line 32 to have an exemple.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your feedback! Happy to adapt it :)
But I am not sure if I can follow. L32 doesnt look right as an example.

Generally yes: we don't want to persist this on the hard drive.
My code extends the general configuration.
So I was now looking to the list of _entity_component_unrecorded_attributes which is assigned to the central configuration.
But all _entity_component_unrecorded_attributes I could find are assigned to either the base thermostat (which hands down basic attributes and entities to all other climate entities) or the actual thermostats.

(I am not a dev, so chances are pretty high, I don't understand the code ;) ) sorry :/

"""Return additional attributes for the sensor."""
return {
"active_device_names": self._attr_active_device_names,
}

@property
def icon(self) -> str | None:
Expand Down Expand Up @@ -708,22 +716,24 @@ async def listen_vtherms_entities(self):
self.calculate_nb_active_devices,
)
_LOGGER.info(
"%s - the underlyings that could controls the central boiler are %s",
"%s - the underlyings that could control the central boiler are %s",
self,
underlying_entities_id,
)
self.async_on_remove(listener_cancel)
else:
_LOGGER.debug("%s - no VTherm could controls the central boiler", self)
_LOGGER.debug("%s - no VTherm could control the central boiler", self)

await self.calculate_nb_active_devices(None)

async def calculate_nb_active_devices(self, _):
"""Calculate the number of active VTherm that have an
influence on central boiler"""
influence on the central boiler and update the list of active device names."""

_LOGGER.debug("%s - calculating the number of active VTherm", self)
nb_active = 0
active_device_names = []

for entity in self._entities:
_LOGGER.debug(
"Examining the hvac_action of %s",
Expand All @@ -734,10 +744,14 @@ async def calculate_nb_active_devices(self, _):
and entity.hvac_action == HVACAction.HEATING
):
for under in entity.underlying_entities:
nb_active += 1 if under.is_device_active else 0
if under.is_device_active:
nb_active += 1
active_device_names.append(under.entity_id)

self._attr_native_value = nb_active
self._attr_active_device_names = active_device_names
self.async_write_ha_state()

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