Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Fixed non unique unique ids and reduce failed API request logging (#4)
Browse files Browse the repository at this point in the history
* Fixed non unique unique ids

* Don't report every API fetch error

* Bump version
  • Loading branch information
dan-r authored Apr 26, 2024
1 parent f3ee393 commit 537b482
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
2 changes: 1 addition & 1 deletion custom_components/o2/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 8 additions & 1 deletion custom_components/o2/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""
Expand All @@ -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
16 changes: 4 additions & 12 deletions custom_components/o2/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"""
Expand Down Expand Up @@ -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."""
Expand Down

0 comments on commit 537b482

Please sign in to comment.