Skip to content

Commit dbb15fd

Browse files
committed
Refactored setup code. Fixed a bug, which could lead to a sem lock during boot and avoid data from being updated after boot or setup until first 12 hrs refresh
1 parent ccd79be commit dbb15fd

File tree

1 file changed

+38
-26
lines changed
  • custom_components/best_bottrop_garbage_collection

1 file changed

+38
-26
lines changed

custom_components/best_bottrop_garbage_collection/__init__.py

+38-26
Original file line numberDiff line numberDiff line change
@@ -21,54 +21,66 @@
2121

2222
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
2323
"""Set up BEST Bottrop from a config entry."""
24-
await get_coordinator(hass)
24+
coordinator = BESTCoordinator(hass)
25+
hass.data[DOMAIN] = coordinator
26+
2527
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
28+
29+
# Update data for the first time. This has to be done after adding the entities,
30+
# otherwise the listeners won't be ready and won't be updated after reboot or
31+
# adding the component for the first time
32+
await coordinator.async_refresh()
33+
2634
return True
2735

2836

2937
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
3038
"""Unload BEST Bottrop config entry."""
39+
3140
unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
41+
3242
if len(hass.config_entries.async_entries(DOMAIN)) == 1:
3343
hass.data.pop(DOMAIN)
3444
return unload_ok
3545

3646

37-
async def get_coordinator(
38-
hass: HomeAssistant,
39-
) -> DataUpdateCoordinator:
40-
"""Get the data update coordinator."""
41-
if DOMAIN in hass.data:
42-
return hass.data[DOMAIN]
47+
class BESTCoordinator(DataUpdateCoordinator):
48+
"""This is the coordinator for the BEST custom component."""
4349

44-
async def async_update_collection_dates() -> list[dict]:
50+
def __init__(self, hass):
51+
"""Initialize my coordinator."""
52+
super().__init__(
53+
hass,
54+
logging.getLogger(__name__),
55+
# Name of the data. For logging purposes.
56+
name=DOMAIN,
57+
# Polling interval. Will only be polled if there are subscribers.
58+
update_interval=timedelta(hours=12),
59+
)
4560

61+
async def _async_update_data(self) -> dict:
62+
"""Fetch data from API endpoint.
63+
64+
This is the place to pre-process the data to lookup tables
65+
so entities can quickly look up their data.
66+
"""
4667
# create a list with responses
68+
4769
# streetid : JSON-Object
48-
ret_list: list = []
70+
ret_dict: dict = {}
4971

50-
for entry in hass.config_entries.async_entries(DOMAIN):
51-
_LOGGER.debug("Request for update for entry %s", entry.data["street_id"])
72+
for entry in self.hass.config_entries.async_entries(DOMAIN):
73+
_LOGGER.debug(
74+
"Request for data fetch for street_id %s", entry.data["street_id"]
75+
)
5276

5377
# ClientError Exceptions already caught by HA
5478
async with async_timeout.timeout(10):
5579
resp = await BESTBottropGarbageCollectionDates().get_dates_as_json(
5680
entry.data["street_id"], entry.data["number"]
5781
)
5882
resp_list = list(resp)
59-
ret_list.append([entry.data["street_id"], resp_list])
60-
61-
return ret_list
83+
# the data is structured as a dict. The key is the street_id. That data to that key is the JSON-dict.
84+
ret_dict[entry.data["street_id"]] = resp_list
6285

63-
# Update dates every 12 hrs
64-
coordinator = DataUpdateCoordinator(
65-
hass,
66-
logging.getLogger(__name__),
67-
name=DOMAIN,
68-
update_method=async_update_collection_dates,
69-
update_interval=timedelta(hours=12),
70-
)
71-
# await coordinator.async_config_entry_first_refresh()
72-
73-
hass.data[DOMAIN] = coordinator
74-
return coordinator
86+
return ret_dict

0 commit comments

Comments
 (0)