-
Notifications
You must be signed in to change notification settings - Fork 60
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #228 from marcelvriend/feature/refactor-legacy-code
Bring code more up to current
- Loading branch information
Showing
15 changed files
with
583 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ logger: | |
default: info | ||
logs: | ||
custom_components.grocy: debug | ||
|
||
pygrocy.grocy_api_client: debug |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,45 +1,124 @@ | ||
"""Binary sensor platform for Grocy.""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
from homeassistant.components.binary_sensor import BinarySensorEntity | ||
from collections.abc import Callable, Mapping | ||
from dataclasses import dataclass | ||
from typing import Any, List | ||
|
||
from homeassistant.components.binary_sensor import ( | ||
BinarySensorEntity, | ||
BinarySensorEntityDescription, | ||
) | ||
from homeassistant.config_entries import ConfigEntry | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
# pylint: disable=relative-beyond-top-level | ||
from .const import ( | ||
ATTR_EXPIRED_PRODUCTS, | ||
ATTR_EXPIRING_PRODUCTS, | ||
ATTR_MISSING_PRODUCTS, | ||
ATTR_OVERDUE_CHORES, | ||
ATTR_OVERDUE_TASKS, | ||
DOMAIN, | ||
GrocyEntityType, | ||
) | ||
from .coordinator import GrocyDataUpdateCoordinator | ||
from .entity import GrocyEntity | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
BINARY_SENSOR_TYPES = [ | ||
GrocyEntityType.EXPIRED_PRODUCTS, | ||
GrocyEntityType.EXPIRING_PRODUCTS, | ||
GrocyEntityType.MISSING_PRODUCTS, | ||
GrocyEntityType.OVERDUE_CHORES, | ||
GrocyEntityType.OVERDUE_TASKS, | ||
] | ||
|
||
|
||
async def async_setup_entry(hass, entry, async_add_entities): | ||
"""Setup binary_sensor platform.""" | ||
coordinator = hass.data[DOMAIN] | ||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: ConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
): | ||
"""Setup binary sensor platform.""" | ||
coordinator: GrocyDataUpdateCoordinator = hass.data[DOMAIN] | ||
entities = [] | ||
for binary_sensor in BINARY_SENSOR_TYPES: | ||
_LOGGER.debug("Adding %s binary sensor", binary_sensor) | ||
entity = GrocyBinarySensor(coordinator, entry, binary_sensor) | ||
coordinator.entities.append(entity) | ||
entities.append(entity) | ||
for description in BINARY_SENSORS: | ||
if description.exists_fn(coordinator.available_entities): | ||
entity = GrocyBinarySensorEntity(coordinator, description, config_entry) | ||
coordinator.entities.append(entity) | ||
entities.append(entity) | ||
else: | ||
_LOGGER.debug( | ||
"Entity description '%s' is not available.", | ||
description.key, | ||
) | ||
|
||
async_add_entities(entities, True) | ||
|
||
|
||
class GrocyBinarySensor(GrocyEntity, BinarySensorEntity): | ||
"""Grocy binary_sensor class.""" | ||
@dataclass | ||
class GrocyBinarySensorEntityDescription(BinarySensorEntityDescription): | ||
"""Grocy binary sensor entity description.""" | ||
|
||
attributes_fn: Callable[[List[Any]], Mapping[str, Any] | None] = lambda _: None | ||
exists_fn: Callable[[List[str]], bool] = lambda _: True | ||
entity_registry_enabled_default: bool = False | ||
|
||
|
||
BINARY_SENSORS: tuple[GrocyBinarySensorEntityDescription, ...] = ( | ||
GrocyBinarySensorEntityDescription( | ||
key=ATTR_EXPIRED_PRODUCTS, | ||
name="Grocy expired products", | ||
icon="mdi:delete-alert-outline", | ||
exists_fn=lambda entities: ATTR_EXPIRED_PRODUCTS in entities, | ||
attributes_fn=lambda data: { | ||
"expired_products": [x.as_dict() for x in data], | ||
"count": len(data), | ||
}, | ||
), | ||
GrocyBinarySensorEntityDescription( | ||
key=ATTR_EXPIRING_PRODUCTS, | ||
name="Grocy expiring products", | ||
icon="mdi:clock-fast", | ||
exists_fn=lambda entities: ATTR_EXPIRING_PRODUCTS in entities, | ||
attributes_fn=lambda data: { | ||
"expiring_products": [x.as_dict() for x in data], | ||
"count": len(data), | ||
}, | ||
), | ||
GrocyBinarySensorEntityDescription( | ||
key=ATTR_MISSING_PRODUCTS, | ||
name="Grocy missing products", | ||
icon="mdi:flask-round-bottom-empty-outline", | ||
exists_fn=lambda entities: ATTR_MISSING_PRODUCTS in entities, | ||
attributes_fn=lambda data: { | ||
"missing_products": [x.as_dict() for x in data], | ||
"count": len(data), | ||
}, | ||
), | ||
GrocyBinarySensorEntityDescription( | ||
key=ATTR_OVERDUE_CHORES, | ||
name="Grocy overdue chores", | ||
icon="mdi:alert-circle-check-outline", | ||
exists_fn=lambda entities: ATTR_OVERDUE_CHORES in entities, | ||
attributes_fn=lambda data: { | ||
"overdue_chores": [x.as_dict() for x in data], | ||
"count": len(data), | ||
}, | ||
), | ||
GrocyBinarySensorEntityDescription( | ||
key=ATTR_OVERDUE_TASKS, | ||
name="Grocy overdue tasks", | ||
icon="mdi:alert-circle-check-outline", | ||
exists_fn=lambda entities: ATTR_OVERDUE_TASKS in entities, | ||
attributes_fn=lambda data: { | ||
"overdue_tasks": [x.as_dict() for x in data], | ||
"count": len(data), | ||
}, | ||
), | ||
) | ||
|
||
|
||
class GrocyBinarySensorEntity(GrocyEntity, BinarySensorEntity): | ||
"""Grocy binary sensor entity definition.""" | ||
|
||
@property | ||
def is_on(self): | ||
"""Return true if the binary_sensor is on.""" | ||
if not self.entity_data: | ||
return | ||
def is_on(self) -> bool | None: | ||
"""Return true if the binary sensor is on.""" | ||
entity_data = self.coordinator.data.get(self.entity_description.key, None) | ||
|
||
return len(self.entity_data) > 0 | ||
return len(entity_data) > 0 if entity_data else False |
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.