From 537b482cd572556c564080ea7e56f8aee30c3a0a Mon Sep 17 00:00:00 2001 From: Dan Raper Date: Fri, 26 Apr 2024 10:10:55 +0100 Subject: [PATCH] Fixed non unique unique ids and reduce failed API request logging (#4) * Fixed non unique unique ids * Don't report every API fetch error * Bump version --- custom_components/o2/const.py | 2 +- custom_components/o2/coordinator.py | 9 ++++++++- custom_components/o2/sensor.py | 16 ++++------------ 3 files changed, 13 insertions(+), 14 deletions(-) diff --git a/custom_components/o2/const.py b/custom_components/o2/const.py index 394c918..f710350 100644 --- a/custom_components/o2/const.py +++ b/custom_components/o2/const.py @@ -2,7 +2,7 @@ CONFIG_VERSION = 1 ENTITY_TYPES = ["sensor"] USER_AGENT = "dan-r-homeassistant-o2" -INTEGRATION_VERSION = "0.1.1" +INTEGRATION_VERSION = "0.1.3" DATA_COORDINATOR = "coordinator" DATA_APICLIENT = "apiclient" diff --git a/custom_components/o2/coordinator.py b/custom_components/o2/coordinator.py index 6d7f49f..3645b17 100644 --- a/custom_components/o2/coordinator.py +++ b/custom_components/o2/coordinator.py @@ -18,6 +18,7 @@ def __init__(self, hass, config): ) self._hass = hass self._data = None + self._fail_count = 0 async def _async_update_data(self): """Fetch data from API.""" @@ -26,8 +27,14 @@ async def _async_update_data(self): try: data = await self._hass.async_add_executor_job(client.get_allowances) self._data = data + self._fail_count = 0 return data except BaseException: - _LOGGER.warning("Error communicating with API") + self._fail_count += 1 + + # Ignore single failures + if self._fail_count > 1: + _LOGGER.warning("Error communicating with API") + return self._data diff --git a/custom_components/o2/sensor.py b/custom_components/o2/sensor.py index 35f8993..71c87d0 100644 --- a/custom_components/o2/sensor.py +++ b/custom_components/o2/sensor.py @@ -38,17 +38,13 @@ def __init__(self, coordinator, hass, name, data_type, unit_of_measurement, icon self._data_type = data_type self._unit_of_measurement = unit_of_measurement self._icon = icon + self._attr_unique_id = f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}" self.entity_id = generate_entity_id( - "sensor.{}", f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}", hass=hass) + "sensor.{}", self._attr_unique_id, hass=hass) self._attr_device_info = self._client.get_device_info() - @property - def unique_id(self) -> str: - """Return the unique ID of the sensor.""" - return self.entity_id - @property def native_value(self): """Get value from data returned from API by coordinator""" @@ -89,17 +85,13 @@ def __init__(self, coordinator, hass): super().__init__(coordinator=coordinator) self._client = hass.data[DOMAIN][DATA_APICLIENT] + self._attr_unique_id = f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}" self.entity_id = generate_entity_id( - "sensor.{}", f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}", hass=hass) + "sensor.{}", self._attr_unique_id, hass=hass) self._attr_device_info = self._client.get_device_info() - @property - def unique_id(self) -> str: - """Return the unique ID of the sensor.""" - return self.entity_id - @property def state(self): """Return the state."""