Skip to content

Commit

Permalink
Merge pull request #95 from custom-components/overdue-sensors
Browse files Browse the repository at this point in the history
Make new binary sensors for overdue tasks and chores
  • Loading branch information
isabellaalstrom authored Sep 7, 2020
2 parents 2f5044c + 81d726d commit e1b1fb2
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
2 changes: 2 additions & 0 deletions custom_components/grocy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,11 @@ async def async_supported_features(grocy_data) -> List[str]:

if config["FEATURE_FLAG_TASKS"] != "0":
features.append(GrocyEntityType.TASKS)
features.append(GrocyEntityType.OVERDUE_TASKS)

if config["FEATURE_FLAG_CHORES"] != "0":
features.append(GrocyEntityType.CHORES)
features.append(GrocyEntityType.OVERDUE_CHORES)

if config["FEATURE_FLAG_RECIPES"] != "0":
features.append(GrocyEntityType.MEAL_PLAN)
Expand Down
2 changes: 2 additions & 0 deletions custom_components/grocy/binary_sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
GrocyEntityType.EXPIRED_PRODUCTS,
GrocyEntityType.EXPIRING_PRODUCTS,
GrocyEntityType.MISSING_PRODUCTS,
GrocyEntityType.OVERDUE_CHORES,
GrocyEntityType.OVERDUE_TASKS,
]


Expand Down
6 changes: 5 additions & 1 deletion custom_components/grocy/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ class GrocyEntityType(str, Enum):
EXPIRING_PRODUCTS = "Expiring_products"
MEAL_PLAN = "Meal_plan"
MISSING_PRODUCTS = "Missing_products"
OVERDUE_CHORES = "Overdue_chores"
OVERDUE_TASKS = "Overdue_tasks"
PRODUCTS = "Products"
SHOPPING_LIST = "Shopping_list"
STOCK = "Stock"
Expand All @@ -61,10 +63,12 @@ class GrocyEntityIcon(str, Enum):
DEFAULT = "mdi:format-quote-close"

CHORES = "mdi:broom"
EXPIRED_PRODUCTS = "mdi:clock-end"
EXPIRED_PRODUCTS = "mdi:delete-alert-outline"
EXPIRING_PRODUCTS = "mdi:clock-fast"
MEAL_PLAN = "mdi:silverware-variant"
MISSING_PRODUCTS = "mdi:flask-round-bottom-empty-outline"
OVERDUE_CHORES = "mdi:alert-circle-check-outline"
OVERDUE_TASKS = "mdi:alert-circle-check-outline"
PRODUCTS = "mdi:food-fork-drink"
SHOPPING_LIST = "mdi:cart-outline"
STOCK = "mdi:fridge-outline"
Expand Down
4 changes: 4 additions & 0 deletions custom_components/grocy/entity.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,10 @@ def device_state_attributes(self):
return {"meals": [x.as_dict() for x in self.entity_data]}
elif self.entity_type == GrocyEntityType.MISSING_PRODUCTS:
return {"missing": [x.as_dict() for x in self.entity_data]}
elif self.entity_type == GrocyEntityType.OVERDUE_CHORES:
return {"chores": [x.as_dict() for x in self.entity_data]}
elif self.entity_type == GrocyEntityType.OVERDUE_TASKS:
return {"tasks": [x.as_dict() for x in self.entity_data]}
elif self.entity_type == GrocyEntityType.PRODUCTS:
return {"products": [x.as_dict() for x in self.entity_data]}
elif self.entity_type == GrocyEntityType.SHOPPING_LIST:
Expand Down
39 changes: 39 additions & 0 deletions custom_components/grocy/grocy_data.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from aiohttp import hdrs, web
from datetime import timedelta, datetime
import logging
import pytz

from homeassistant.components.http import HomeAssistantView
from homeassistant.helpers.aiohttp_client import async_get_clientsession
Expand All @@ -13,6 +15,9 @@
from .helpers import MealPlanItem

MIN_TIME_BETWEEN_UPDATES = timedelta(seconds=60)
_LOGGER = logging.getLogger(__name__)

utc = pytz.UTC


class GrocyData:
Expand All @@ -31,6 +36,8 @@ def __init__(self, hass, client):
GrocyEntityType.EXPIRED_PRODUCTS: self.async_update_expired_products,
GrocyEntityType.MISSING_PRODUCTS: self.async_update_missing_products,
GrocyEntityType.MEAL_PLAN: self.async_update_meal_plan,
GrocyEntityType.OVERDUE_CHORES: self.async_update_overdue_chores,
GrocyEntityType.OVERDUE_TASKS: self.async_update_overdue_tasks,
}
self.sensor_update_dict = {
GrocyEntityType.STOCK: None,
Expand All @@ -41,6 +48,8 @@ def __init__(self, hass, client):
GrocyEntityType.EXPIRED_PRODUCTS: None,
GrocyEntityType.MISSING_PRODUCTS: None,
GrocyEntityType.MEAL_PLAN: None,
GrocyEntityType.OVERDUE_CHORES: None,
GrocyEntityType.OVERDUE_TASKS: None,
}

async def async_update_data(self, sensor_type):
Expand Down Expand Up @@ -68,6 +77,22 @@ def wrapper():

return await self.hass.async_add_executor_job(wrapper)

async def async_update_overdue_chores(self):
"""Update data."""
# This is where the main logic to update platform data goes.
def wrapper():
return self.client.chores(True)

chores = await self.hass.async_add_executor_job(wrapper)
overdue_chores = []
for chore in chores:
if chore.next_estimated_execution_time:
now = datetime.now().replace(tzinfo=utc)
due = chore.next_estimated_execution_time.replace(tzinfo=utc)
if due < now:
overdue_chores.append(chore)
return overdue_chores

async def async_get_config(self):
"""Get the configuration from Grocy."""

Expand All @@ -81,6 +106,20 @@ async def async_update_tasks(self):
# This is where the main logic to update platform data goes.
return await self.hass.async_add_executor_job(self.client.tasks)

async def async_update_overdue_tasks(self):
"""Update data."""
# This is where the main logic to update platform data goes.
tasks = await self.hass.async_add_executor_job(self.client.tasks)

overdue_tasks = []
for task in tasks:
if task.due_date:
now = datetime.now().replace(tzinfo=utc)
due = task.due_date.replace(tzinfo=utc)
if due < now:
overdue_tasks.append(task)
return overdue_tasks

async def async_update_shopping_list(self):
"""Update data."""
# This is where the main logic to update platform data goes.
Expand Down

0 comments on commit e1b1fb2

Please sign in to comment.