Skip to content

Commit

Permalink
Merge pull request #90 from custom-components/supported-features
Browse files Browse the repository at this point in the history
Add supported features
  • Loading branch information
isabellaalstrom authored Sep 6, 2020
2 parents a07117f + f618af8 commit a35df91
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 1 deletion.
32 changes: 31 additions & 1 deletion custom_components/grocy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -20,6 +21,7 @@
CONF_URL,
CONF_VERIFY_SSL,
DOMAIN,
GrocyEntityType,
PLATFORMS,
STARTUP_MESSAGE,
)
Expand Down Expand Up @@ -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
)
Expand All @@ -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)
Expand Down
8 changes: 8 additions & 0 deletions custom_components/grocy/grocy_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit a35df91

Please sign in to comment.