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

Commit 537b482

Browse files
authored
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
1 parent f3ee393 commit 537b482

File tree

3 files changed

+13
-14
lines changed

3 files changed

+13
-14
lines changed

custom_components/o2/const.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
CONFIG_VERSION = 1
33
ENTITY_TYPES = ["sensor"]
44
USER_AGENT = "dan-r-homeassistant-o2"
5-
INTEGRATION_VERSION = "0.1.1"
5+
INTEGRATION_VERSION = "0.1.3"
66

77
DATA_COORDINATOR = "coordinator"
88
DATA_APICLIENT = "apiclient"

custom_components/o2/coordinator.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ def __init__(self, hass, config):
1818
)
1919
self._hass = hass
2020
self._data = None
21+
self._fail_count = 0
2122

2223
async def _async_update_data(self):
2324
"""Fetch data from API."""
@@ -26,8 +27,14 @@ async def _async_update_data(self):
2627
try:
2728
data = await self._hass.async_add_executor_job(client.get_allowances)
2829
self._data = data
30+
self._fail_count = 0
2931

3032
return data
3133
except BaseException:
32-
_LOGGER.warning("Error communicating with API")
34+
self._fail_count += 1
35+
36+
# Ignore single failures
37+
if self._fail_count > 1:
38+
_LOGGER.warning("Error communicating with API")
39+
3340
return self._data

custom_components/o2/sensor.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,13 @@ def __init__(self, coordinator, hass, name, data_type, unit_of_measurement, icon
3838
self._data_type = data_type
3939
self._unit_of_measurement = unit_of_measurement
4040
self._icon = icon
41+
self._attr_unique_id = f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}"
4142

4243
self.entity_id = generate_entity_id(
43-
"sensor.{}", f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}", hass=hass)
44+
"sensor.{}", self._attr_unique_id, hass=hass)
4445

4546
self._attr_device_info = self._client.get_device_info()
4647

47-
@property
48-
def unique_id(self) -> str:
49-
"""Return the unique ID of the sensor."""
50-
return self.entity_id
51-
5248
@property
5349
def native_value(self):
5450
"""Get value from data returned from API by coordinator"""
@@ -89,17 +85,13 @@ def __init__(self, coordinator, hass):
8985
super().__init__(coordinator=coordinator)
9086

9187
self._client = hass.data[DOMAIN][DATA_APICLIENT]
88+
self._attr_unique_id = f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}"
9289

9390
self.entity_id = generate_entity_id(
94-
"sensor.{}", f"o2_{self._client.number}_{self._attr_name.lower().replace(' ', '_')}", hass=hass)
91+
"sensor.{}", self._attr_unique_id, hass=hass)
9592

9693
self._attr_device_info = self._client.get_device_info()
9794

98-
@property
99-
def unique_id(self) -> str:
100-
"""Return the unique ID of the sensor."""
101-
return self.entity_id
102-
10395
@property
10496
def state(self):
10597
"""Return the state."""

0 commit comments

Comments
 (0)