diff --git a/custom_components/grocy/__init__.py b/custom_components/grocy/__init__.py index 84bf260..22aa389 100644 --- a/custom_components/grocy/__init__.py +++ b/custom_components/grocy/__init__.py @@ -7,6 +7,7 @@ import asyncio import logging from datetime import timedelta +from typing import List from homeassistant.config_entries import ConfigEntry from homeassistant.core import Config, HomeAssistant @@ -20,6 +21,7 @@ CONF_URL, CONF_VERIFY_SSL, DOMAIN, + GrocyEntityType, PLATFORMS, STARTUP_MESSAGE, ) @@ -85,8 +87,9 @@ async def _async_update_data(self): data = {} try: grocy_data = GrocyData(self.hass, self.api) + features = await async_supported_features(grocy_data) for entity in self.entities: - if entity.enabled: + if entity.enabled and entity.entity_type in features: data[entity.entity_type] = await grocy_data.async_update_data( entity.entity_type ) @@ -95,6 +98,33 @@ async def _async_update_data(self): raise UpdateFailed(exception) +async def async_supported_features(grocy_data) -> List[str]: + """Return a list of supported features.""" + features = [] + config = await grocy_data.async_get_config() + if config: + if config["FEATURE_FLAG_STOCK"]: + features.append(GrocyEntityType.STOCK) + features.append(GrocyEntityType.PRODUCTS) + features.append(GrocyEntityType.MISSING_PRODUCTS) + features.append(GrocyEntityType.EXPIRED_PRODUCTS) + features.append(GrocyEntityType.EXPIRING_PRODUCTS) + + if config["FEATURE_FLAG_SHOPPINGLIST"]: + features.append(GrocyEntityType.SHOPPING_LIST) + + if config["FEATURE_FLAG_TASKS"]: + features.append(GrocyEntityType.TASKS) + + if config["FEATURE_FLAG_CHORES"]: + features.append(GrocyEntityType.CHORES) + + if config["FEATURE_FLAG_RECIPES"]: + features.append(GrocyEntityType.MEAL_PLAN) + + return features + + async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Handle removal of an entry.""" _LOGGER.debug("Unloading with state %s", entry.state) diff --git a/custom_components/grocy/grocy_data.py b/custom_components/grocy/grocy_data.py index d56fa04..c6e8077 100644 --- a/custom_components/grocy/grocy_data.py +++ b/custom_components/grocy/grocy_data.py @@ -68,6 +68,14 @@ def wrapper(): return await self.hass.async_add_executor_job(wrapper) + async def async_get_config(self): + """Get the configuration from Grocy.""" + + def wrapper(): + return self.client._api_client._do_get_request("/api/system/config") + + return await self.hass.async_add_executor_job(wrapper) + async def async_update_tasks(self): """Update data.""" # This is where the main logic to update platform data goes.