Skip to content

Commit

Permalink
Merge pull request #71 from nervetattoo/develop
Browse files Browse the repository at this point in the history
Add meal_plan sensor
  • Loading branch information
isabellaalstrom authored Aug 16, 2020
2 parents c42ca6e + d44860c commit b1af07e
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 2 deletions.
19 changes: 19 additions & 0 deletions custom_components/grocy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
EXPIRING_PRODUCTS_NAME,
ISSUE_URL,
MISSING_PRODUCTS_NAME,
MEAL_PLAN_NAME,
PLATFORMS,
REQUIRED_FILES,
SHOPPING_LIST_NAME,
Expand All @@ -39,6 +40,8 @@
VERSION,
)

from .helpers import MealPlanItem

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)

_LOGGER = logging.getLogger(__name__)
Expand Down Expand Up @@ -118,6 +121,7 @@ async def async_setup_entry(hass, config_entry):
grocy = Grocy(url, api_key, port_number, verify_ssl)
hass.data[DOMAIN_DATA]["client"] = GrocyData(hass, grocy)
hass.data[DOMAIN_DATA]["hash_key"] = hash_key
hass.data[DOMAIN_DATA]["url"] = f"{url}:{port_number}"

# Add sensor
hass.async_add_job(
Expand Down Expand Up @@ -212,6 +216,7 @@ def __init__(self, hass, client):
EXPIRING_PRODUCTS_NAME: self.async_update_expiring_products,
EXPIRED_PRODUCTS_NAME: self.async_update_expired_products,
MISSING_PRODUCTS_NAME: self.async_update_missing_products,
MEAL_PLAN_NAME : self.async_update_meal_plan,
}
self.sensor_update_dict = {
STOCK_NAME: None,
Expand All @@ -221,6 +226,7 @@ def __init__(self, hass, client):
EXPIRING_PRODUCTS_NAME: None,
EXPIRED_PRODUCTS_NAME: None,
MISSING_PRODUCTS_NAME: None,
MEAL_PLAN_NAME : None,
}

async def async_update_data(self, sensor_type):
Expand Down Expand Up @@ -304,6 +310,19 @@ def wrapper():
MISSING_PRODUCTS_NAME
] = await self.hass.async_add_executor_job(wrapper)

@Throttle(MIN_TIME_BETWEEN_UPDATES)
async def async_update_meal_plan(self):
"""Update data."""
# This is where the main logic to update platform data goes.
def wrapper():
meal_plan = self.client.meal_plan(True)
base_url = self.hass.data[DOMAIN_DATA]["url"]
return [MealPlanItem(item, base_url) for item in meal_plan]

self.hass.data[DOMAIN_DATA][
MEAL_PLAN_NAME
] = await self.hass.async_add_executor_job(wrapper)


def check_files(hass):
"""Return bool that indicates if all files are present."""
Expand Down
4 changes: 3 additions & 1 deletion custom_components/grocy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,17 @@
SENSOR_PRODUCTS_UNIT_OF_MEASUREMENT = "Product(s)"
SENSOR_CHORES_UNIT_OF_MEASUREMENT = "Chore(s)"
SENSOR_TASKS_UNIT_OF_MEASUREMENT = "Task(s)"
SENSOR_MEALS_UNIT_OF_MEASUREMENT = "Meal(s)"
STOCK_NAME = "stock"
CHORES_NAME = "chores"
TASKS_NAME = "tasks"
SHOPPING_LIST_NAME = "shopping_list"
EXPIRING_PRODUCTS_NAME = "expiring_products"
EXPIRED_PRODUCTS_NAME = "expired_products"
MISSING_PRODUCTS_NAME = "missing_products"
MEAL_PLAN_NAME = "meal_plan"

SENSOR_TYPES = [STOCK_NAME, CHORES_NAME, TASKS_NAME, SHOPPING_LIST_NAME]
SENSOR_TYPES = [STOCK_NAME, CHORES_NAME, TASKS_NAME, SHOPPING_LIST_NAME, MEAL_PLAN_NAME]
BINARY_SENSOR_TYPES = [
EXPIRING_PRODUCTS_NAME,
EXPIRED_PRODUCTS_NAME,
Expand Down
12 changes: 12 additions & 0 deletions custom_components/grocy/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class MealPlanItem(object):
def __init__(self, data, base_url):
self.day = data.day
self.note = data.note
self.recipe_name = data.recipe.name
self.desired_servings = data.recipe.desired_servings

picture_path = data.recipe.get_picture_url_path(400)
self.picture_url = f"{base_url}/api/{picture_path}"

def as_dict(self):
return vars(self)
2 changes: 1 addition & 1 deletion custom_components/grocy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@
"iso8601==0.1.12",
"integrationhelper"
]
}
}
4 changes: 4 additions & 0 deletions custom_components/grocy/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@
ATTRIBUTION,
CHORES_NAME,
TASKS_NAME,
MEAL_PLAN_NAME,
DEFAULT_NAME,
DOMAIN,
DOMAIN_DATA,
ICON,
SENSOR_CHORES_UNIT_OF_MEASUREMENT,
SENSOR_TASKS_UNIT_OF_MEASUREMENT,
SENSOR_PRODUCTS_UNIT_OF_MEASUREMENT,
SENSOR_MEALS_UNIT_OF_MEASUREMENT,
SENSOR_TYPES,
)

Expand Down Expand Up @@ -90,6 +92,8 @@ def unit_of_measurement(self):
return SENSOR_CHORES_UNIT_OF_MEASUREMENT
elif self.sensor_type == TASKS_NAME:
return SENSOR_TASKS_UNIT_OF_MEASUREMENT
elif self.sensor_type == MEAL_PLAN_NAME:
return SENSOR_MEALS_UNIT_OF_MEASUREMENT
else:
return SENSOR_PRODUCTS_UNIT_OF_MEASUREMENT

Expand Down

0 comments on commit b1af07e

Please sign in to comment.