|
21 | 21 |
|
22 | 22 | async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
23 | 23 | """Set up BEST Bottrop from a config entry."""
|
24 |
| - await get_coordinator(hass) |
| 24 | + coordinator = BESTCoordinator(hass) |
| 25 | + hass.data[DOMAIN] = coordinator |
| 26 | + |
25 | 27 | 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 | + |
26 | 34 | return True
|
27 | 35 |
|
28 | 36 |
|
29 | 37 | async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
30 | 38 | """Unload BEST Bottrop config entry."""
|
| 39 | + |
31 | 40 | unload_ok = await hass.config_entries.async_unload_platforms(entry, PLATFORMS)
|
| 41 | + |
32 | 42 | if len(hass.config_entries.async_entries(DOMAIN)) == 1:
|
33 | 43 | hass.data.pop(DOMAIN)
|
34 | 44 | return unload_ok
|
35 | 45 |
|
36 | 46 |
|
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.""" |
43 | 49 |
|
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 | + ) |
45 | 60 |
|
| 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 | + """ |
46 | 67 | # create a list with responses
|
| 68 | + |
47 | 69 | # streetid : JSON-Object
|
48 |
| - ret_list: list = [] |
| 70 | + ret_dict: dict = {} |
49 | 71 |
|
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 | + ) |
52 | 76 |
|
53 | 77 | # ClientError Exceptions already caught by HA
|
54 | 78 | async with async_timeout.timeout(10):
|
55 | 79 | resp = await BESTBottropGarbageCollectionDates().get_dates_as_json(
|
56 | 80 | entry.data["street_id"], entry.data["number"]
|
57 | 81 | )
|
58 | 82 | 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 |
62 | 85 |
|
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