Skip to content

Commit

Permalink
Merge pull request #74 from custom-components/develop
Browse files Browse the repository at this point in the history
Tasks and meal planning
  • Loading branch information
isabellaalstrom authored Aug 16, 2020
2 parents bb78a64 + b1af07e commit 1fcfa67
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 5 deletions.
28 changes: 28 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 @@ -185,6 +189,15 @@ def handle_complete_task(call):

hass.services.async_register(DOMAIN, "complete_task", handle_complete_task)

@callback
def handle_add_generic(call):
entity_type = call.data["entity_type"]
data = call.data["data"]

grocy.add_generic(entity_type, data)

hass.services.async_register(DOMAIN, "add_generic", handle_add_generic)

return True


Expand All @@ -203,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 @@ -212,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 @@ -295,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
9 changes: 7 additions & 2 deletions custom_components/grocy/config_flow.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
"""Adds config flow for grocy."""
from collections import OrderedDict

import logging
import voluptuous as vol
from homeassistant import config_entries
from pygrocy import Grocy

from .const import DEFAULT_PORT_NUMBER, DOMAIN

_LOGGER = logging.getLogger(__name__)


@config_entries.HANDLERS.register(DOMAIN)
class GrocyFlowHandler(config_entries.ConfigFlow):
Expand All @@ -31,8 +34,10 @@ async def async_step_user(

if user_input is not None:
valid = await self._test_credentials(
user_input["url"], user_input["api_key"],
user_input["port"],user_input["verify_ssl"]
user_input["url"],
user_input["api_key"],
user_input["port"],
user_input["verify_ssl"],
)
if valid:
return self.async_create_entry(title="Grocy", data=user_input)
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)
4 changes: 2 additions & 2 deletions custom_components/grocy/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"@isabellaalstrom"
],
"requirements": [
"pygrocy==0.18.0",
"pygrocy==0.20.0",
"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
10 changes: 10 additions & 0 deletions custom_components/grocy/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,13 @@ complete_task:
done_time:
example: '2019-06-18T19:00:00.251Z'
description: The timestamp at when the task got completed as ISO8601

add_generic:
description: Adds a single object of the given entity type
fields:
entity_type:
example: 'tasks'
description: Entity type is the type of entity you like to add. See Grocy api documentation on "Generic entity interactions" for more information.
data:
example: '{"name": "This is a task"}'
description: Data is a json object with what data you want to add.

0 comments on commit 1fcfa67

Please sign in to comment.