Skip to content

Commit

Permalink
Fixed unique ids of device data sensors not being unique, skip API up…
Browse files Browse the repository at this point in the history
…dates for disabled energy sensors to reduce quota exceeded issues, added an entity for the number of API requests made by this integration, removed version debug logging on setup
  • Loading branch information
signalkraft committed Oct 31, 2024
1 parent 7e54902 commit cd4b6b8
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 15 deletions.
11 changes: 0 additions & 11 deletions custom_components/mypyllant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,6 @@ def update_unique_id(entity_entry: RegistryEntry):


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
if _LOGGER.isEnabledFor(logging.DEBUG):
from importlib.metadata import version

_LOGGER.debug(
"Starting mypyllant component %s (library %s) with homeassistant %s, pydantic %s, and aiohttp %s",
hass.data["integrations"][DOMAIN].version,
version("myPyllant"),
version("homeassistant"),
version("pydantic"),
version("aiohttp"),
)
username: str = entry.data.get("username") # type: ignore
password: str = entry.data.get("password") # type: ignore
update_interval = entry.options.get(OPTION_UPDATE_INTERVAL, DEFAULT_UPDATE_INTERVAL)
Expand Down
18 changes: 17 additions & 1 deletion custom_components/mypyllant/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.helpers import entity_registry as er

from custom_components.mypyllant.const import (
DOMAIN,
Expand Down Expand Up @@ -204,6 +205,17 @@ class SystemWithDeviceData(TypedDict):
class DailyDataCoordinator(MyPyllantCoordinator):
data: dict[str, SystemWithDeviceData]

async def is_sensor_disabled(self, unique_id: str) -> bool:
"""
Check if a sensor is disabled to be able to skip its API update
"""
entity_registry = er.async_get(self.hass)
entity_id = entity_registry.async_get_entity_id("sensor", DOMAIN, unique_id)
if entity_id:
entity_entry = entity_registry.async_get(entity_id)
return entity_entry and entity_entry.disabled
return False

async def _async_update_data(self) -> dict[str, SystemWithDeviceData]:
self._raise_if_quota_hit()
_LOGGER.debug("Starting async update data for DailyDataCoordinator")
Expand All @@ -227,7 +239,11 @@ async def _async_update_data(self) -> dict[str, SystemWithDeviceData]:
"home_name": system.home.home_name or system.home.nomenclature,
"devices_data": [],
}
for device in system.devices:
for de_index, device in enumerate(system.devices):
for da_index, dd in enumerate(device.data):
sensor_id = f"{DOMAIN}_{device.system_id}_{device.device_uuid}_{da_index}_{de_index}"
if await self.is_sensor_disabled(sensor_id):
device.data[da_index].skip_data_update = True
device_data = self.api.get_data_by_device(
device, DeviceDataBucketResolution.HOUR, start, end
)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/mypyllant/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"iot_class": "cloud_polling",
"issue_tracker": "https://github.com/signalkraft/mypyllant-component/issues",
"requirements": [
"myPyllant==0.8.33"
"myPyllant==0.8.36"
],
"version": "v0.8.19"
}
19 changes: 18 additions & 1 deletion custom_components/mypyllant/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ async def create_system_sensors(

sensors: EntityList[SensorEntity] = EntityList()
_LOGGER.debug("Creating system sensors for %s", system_coordinator.data)
sensors.append(lambda: SystemAPIRequestCount(system_coordinator))
for index, system in enumerate(system_coordinator.data):
if system.outdoor_temperature is not None:
sensors.append(
Expand Down Expand Up @@ -858,7 +859,7 @@ def total_consumption(self) -> float | None:
def unique_id(self) -> str | None:
if self.device is None:
return None
return f"{DOMAIN}_{self.system_id}_{self.device.device_uuid}_{self.da_index}"
return f"{DOMAIN}_{self.system_id}_{self.device.device_uuid}_{self.da_index}_{self.de_index}"

@property
def name_prefix(self) -> str:
Expand Down Expand Up @@ -1109,3 +1110,19 @@ def unique_id(self) -> str:
@property
def name(self):
return f"{self.name_prefix} Current Power"


class SystemAPIRequestCount(SensorEntity, CoordinatorEntity):
_attr_entity_category = EntityCategory.DIAGNOSTIC

@property
def native_value(self):
return self.coordinator.api.aiohttp_session.request_count

@property
def unique_id(self) -> str:
return f"{DOMAIN}_api_request_count"

@property
def name(self):
return "Vaillant API Request Count"
2 changes: 1 addition & 1 deletion dev-requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ types-PyYAML~=6.0.12.20240311

# Need specific versions
pytest-homeassistant-custom-component==0.13.142
myPyllant==0.8.33
myPyllant==0.8.36

# Versions handled by pytest-homeassistant-custom-component
freezegun
Expand Down

0 comments on commit cd4b6b8

Please sign in to comment.