diff --git a/custom_components/ecocito/__init__.py b/custom_components/ecocito/__init__.py index 1fa729c..29b9fe9 100644 --- a/custom_components/ecocito/__init__.py +++ b/custom_components/ecocito/__init__.py @@ -11,10 +11,8 @@ from homeassistant.core import HomeAssistant from .client import EcocitoClient from .const import ( ECOCITO_DEFAULT_REFRESH_MIN, - ECOCITO_GARBAGE_COLLECTION_TYPE, ECOCITO_GARBAGE_TYPE, ECOCITO_RECYCLE_TYPE, - ECOCITO_RECYCLING_COLLECTION_TYPE, ECOCITO_REFRESH_MIN_KEY, ) from .coordinator import ( @@ -29,13 +27,11 @@ PLATFORMS: list[Platform] = [Platform.SENSOR] class EcocitoData: """Ecocito data type.""" - # TODO: Possibly at some point we can build dynamic sensors depending on user needs - - garbage_collections: CollectionDataUpdateCoordinator - garbage_collections_previous: CollectionDataUpdateCoordinator - recycling_collections: CollectionDataUpdateCoordinator - recycling_collections_previous: CollectionDataUpdateCoordinator - waste_depot_visits: WasteDepotVisitsDataUpdateCoordinator + garbage_collections: CollectionDataUpdateCoordinator | None + garbage_collections_previous: CollectionDataUpdateCoordinator | None + recycling_collections: CollectionDataUpdateCoordinator | None + recycling_collections_previous: CollectionDataUpdateCoordinator | None + waste_depot_visits: WasteDepotVisitsDataUpdateCoordinator # Maybe we could have an optional here if we had some checkbox config? type EcocitoConfigEntry = ConfigEntry[EcocitoData] @@ -50,29 +46,31 @@ async def async_setup_entry(hass: HomeAssistant, entry: EcocitoConfigEntry) -> b ) await client.authenticate() - garbage_id = entry.data.get(ECOCITO_GARBAGE_TYPE, ECOCITO_GARBAGE_COLLECTION_TYPE) - recycle_id = entry.data.get(ECOCITO_RECYCLE_TYPE, ECOCITO_RECYCLING_COLLECTION_TYPE) + garbage_id = entry.data.get(ECOCITO_GARBAGE_TYPE) + recycle_id = entry.data.get(ECOCITO_RECYCLE_TYPE) refresh_time = entry.data.get(ECOCITO_REFRESH_MIN_KEY, ECOCITO_DEFAULT_REFRESH_MIN) + data = EcocitoData( garbage_collections=CollectionDataUpdateCoordinator( hass, client, 0, garbage_id, refresh_time - ), + ) if garbage_id is not None else None, garbage_collections_previous=CollectionDataUpdateCoordinator( hass, client, -1, garbage_id, refresh_time - ), + ) if garbage_id is not None else None, recycling_collections=CollectionDataUpdateCoordinator( hass, client, 0, recycle_id, refresh_time - ), + ) if recycle_id is not None else None, recycling_collections_previous=CollectionDataUpdateCoordinator( hass, client, -1, recycle_id, refresh_time - ), + ) if recycle_id is not None else None, waste_depot_visits=WasteDepotVisitsDataUpdateCoordinator( hass, client, 0, refresh_time ), ) for field in fields(data): coordinator = getattr(data, field.name) - await coordinator.async_config_entry_first_refresh() + if coordinator: + await coordinator.async_config_entry_first_refresh() entry.runtime_data = data await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS) diff --git a/custom_components/ecocito/options_flow.py b/custom_components/ecocito/options_flow.py index bdda0be..bc4bc69 100644 --- a/custom_components/ecocito/options_flow.py +++ b/custom_components/ecocito/options_flow.py @@ -16,6 +16,9 @@ from .const import ( ECOCITO_REFRESH_MIN_KEY, ) +# Hardcode a value for "skip synchro" +# -> it seems empty value would fill in previous known value +SKIP_SYNC_VALUE = "-42" def build_schema(type_mapping: dict[int, str], current: dict[str, Any]) -> vol.Schema: """Build the schema.""" @@ -23,6 +26,10 @@ def build_schema(type_mapping: dict[int, str], current: dict[str, Any]) -> vol.S {"value": str(type_id), "label": type_label} for type_id, type_label in type_mapping.items() ] + types_options.append({ + "value": SKIP_SYNC_VALUE, + "label": "Skip synchro" + }) return vol.Schema( { vol.Optional(ECOCITO_GARBAGE_TYPE, default=current[ECOCITO_GARBAGE_TYPE]): @@ -71,6 +78,9 @@ class EcocitoOptionsFlowHandler(config_entries.OptionsFlow): ]: if key not in user_input: continue + if user_input[key] == SKIP_SYNC_VALUE: + new_data.pop(key, None) # = None + continue int_val = int(user_input[key]) if (key not in new_data or int_val != new_data.get(key)) and int_val > 0: new_data[key] = int_val @@ -89,8 +99,8 @@ class EcocitoOptionsFlowHandler(config_entries.OptionsFlow): await self.update_config(user_input) return self.async_abort(reason="Changes saved.") placeholders = { - ECOCITO_GARBAGE_TYPE: str(self._entry.data.get(ECOCITO_GARBAGE_TYPE, 15)), - ECOCITO_RECYCLE_TYPE: str(self._entry.data.get(ECOCITO_RECYCLE_TYPE, 16)), + ECOCITO_GARBAGE_TYPE: str(self._entry.data.get(ECOCITO_GARBAGE_TYPE, SKIP_SYNC_VALUE)), + ECOCITO_RECYCLE_TYPE: str(self._entry.data.get(ECOCITO_RECYCLE_TYPE, SKIP_SYNC_VALUE)), ECOCITO_REFRESH_MIN_KEY: int( self._entry.data.get(ECOCITO_REFRESH_MIN_KEY, 60) ), diff --git a/custom_components/ecocito/sensor.py b/custom_components/ecocito/sensor.py index a04b209..c4dc3b1 100644 --- a/custom_components/ecocito/sensor.py +++ b/custom_components/ecocito/sensor.py @@ -237,5 +237,6 @@ async def async_setup_entry( entities: list[EcocitoSensor[Any]] = [] for coordinator_type, description in SENSOR_TYPES: coordinator = getattr(entry.runtime_data, coordinator_type) - entities.append(EcocitoSensor(coordinator, description)) + if coordinator: + entities.append(EcocitoSensor(coordinator, description)) async_add_entities(entities)