Use EntityDescription - picnic (#55682)
* Use EntityDescription - picnic * Change _attr_extra_state_attributes to be static * Fix tests
This commit is contained in:
parent
9db13a3e74
commit
ce6921d73c
3 changed files with 150 additions and 134 deletions
|
@ -1,5 +1,12 @@
|
||||||
"""Constants for the Picnic integration."""
|
"""Constants for the Picnic integration."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Any, Callable, Literal
|
||||||
|
|
||||||
|
from homeassistant.components.sensor import SensorEntityDescription
|
||||||
from homeassistant.const import CURRENCY_EURO, DEVICE_CLASS_TIMESTAMP
|
from homeassistant.const import CURRENCY_EURO, DEVICE_CLASS_TIMESTAMP
|
||||||
|
from homeassistant.helpers.typing import StateType
|
||||||
|
|
||||||
DOMAIN = "picnic"
|
DOMAIN = "picnic"
|
||||||
|
|
||||||
|
@ -28,91 +35,122 @@ SENSOR_LAST_ORDER_ETA_END = "last_order_eta_end"
|
||||||
SENSOR_LAST_ORDER_DELIVERY_TIME = "last_order_delivery_time"
|
SENSOR_LAST_ORDER_DELIVERY_TIME = "last_order_delivery_time"
|
||||||
SENSOR_LAST_ORDER_TOTAL_PRICE = "last_order_total_price"
|
SENSOR_LAST_ORDER_TOTAL_PRICE = "last_order_total_price"
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
|
||||||
SENSOR_CART_ITEMS_COUNT: {
|
@dataclass
|
||||||
"icon": "mdi:format-list-numbered",
|
class PicnicRequiredKeysMixin:
|
||||||
"data_type": CART_DATA,
|
"""Mixin for required keys."""
|
||||||
"state": lambda cart: cart.get("total_count", 0),
|
|
||||||
},
|
data_type: Literal["cart_data", "slot_data", "last_order_data"]
|
||||||
SENSOR_CART_TOTAL_PRICE: {
|
state: Callable[[Any], StateType]
|
||||||
"unit": CURRENCY_EURO,
|
|
||||||
"icon": "mdi:currency-eur",
|
|
||||||
"default_enabled": True,
|
@dataclass
|
||||||
"data_type": CART_DATA,
|
class PicnicSensorEntityDescription(SensorEntityDescription, PicnicRequiredKeysMixin):
|
||||||
"state": lambda cart: cart.get("total_price", 0) / 100,
|
"""Describes Picnic sensor entity."""
|
||||||
},
|
|
||||||
SENSOR_SELECTED_SLOT_START: {
|
entity_registry_enabled_default: bool = False
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
|
||||||
"icon": "mdi:calendar-start",
|
|
||||||
"default_enabled": True,
|
SENSOR_TYPES: tuple[PicnicSensorEntityDescription, ...] = (
|
||||||
"data_type": SLOT_DATA,
|
PicnicSensorEntityDescription(
|
||||||
"state": lambda slot: slot.get("window_start"),
|
key=SENSOR_CART_ITEMS_COUNT,
|
||||||
},
|
icon="mdi:format-list-numbered",
|
||||||
SENSOR_SELECTED_SLOT_END: {
|
data_type="cart_data",
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
state=lambda cart: cart.get("total_count", 0),
|
||||||
"icon": "mdi:calendar-end",
|
),
|
||||||
"default_enabled": True,
|
PicnicSensorEntityDescription(
|
||||||
"data_type": SLOT_DATA,
|
key=SENSOR_CART_TOTAL_PRICE,
|
||||||
"state": lambda slot: slot.get("window_end"),
|
native_unit_of_measurement=CURRENCY_EURO,
|
||||||
},
|
icon="mdi:currency-eur",
|
||||||
SENSOR_SELECTED_SLOT_MAX_ORDER_TIME: {
|
entity_registry_enabled_default=True,
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
data_type="cart_data",
|
||||||
"icon": "mdi:clock-alert-outline",
|
state=lambda cart: cart.get("total_price", 0) / 100,
|
||||||
"default_enabled": True,
|
),
|
||||||
"data_type": SLOT_DATA,
|
PicnicSensorEntityDescription(
|
||||||
"state": lambda slot: slot.get("cut_off_time"),
|
key=SENSOR_SELECTED_SLOT_START,
|
||||||
},
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
SENSOR_SELECTED_SLOT_MIN_ORDER_VALUE: {
|
icon="mdi:calendar-start",
|
||||||
"unit": CURRENCY_EURO,
|
entity_registry_enabled_default=True,
|
||||||
"icon": "mdi:currency-eur",
|
data_type="slot_data",
|
||||||
"default_enabled": True,
|
state=lambda slot: slot.get("window_start"),
|
||||||
"data_type": SLOT_DATA,
|
),
|
||||||
"state": lambda slot: slot["minimum_order_value"] / 100
|
PicnicSensorEntityDescription(
|
||||||
if slot.get("minimum_order_value")
|
key=SENSOR_SELECTED_SLOT_END,
|
||||||
else None,
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
},
|
icon="mdi:calendar-end",
|
||||||
SENSOR_LAST_ORDER_SLOT_START: {
|
entity_registry_enabled_default=True,
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
data_type="slot_data",
|
||||||
"icon": "mdi:calendar-start",
|
state=lambda slot: slot.get("window_end"),
|
||||||
"data_type": LAST_ORDER_DATA,
|
),
|
||||||
"state": lambda last_order: last_order.get("slot", {}).get("window_start"),
|
PicnicSensorEntityDescription(
|
||||||
},
|
key=SENSOR_SELECTED_SLOT_MAX_ORDER_TIME,
|
||||||
SENSOR_LAST_ORDER_SLOT_END: {
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
icon="mdi:clock-alert-outline",
|
||||||
"icon": "mdi:calendar-end",
|
entity_registry_enabled_default=True,
|
||||||
"data_type": LAST_ORDER_DATA,
|
data_type="slot_data",
|
||||||
"state": lambda last_order: last_order.get("slot", {}).get("window_end"),
|
state=lambda slot: slot.get("cut_off_time"),
|
||||||
},
|
),
|
||||||
SENSOR_LAST_ORDER_STATUS: {
|
PicnicSensorEntityDescription(
|
||||||
"icon": "mdi:list-status",
|
key=SENSOR_SELECTED_SLOT_MIN_ORDER_VALUE,
|
||||||
"data_type": LAST_ORDER_DATA,
|
native_unit_of_measurement=CURRENCY_EURO,
|
||||||
"state": lambda last_order: last_order.get("status"),
|
icon="mdi:currency-eur",
|
||||||
},
|
entity_registry_enabled_default=True,
|
||||||
SENSOR_LAST_ORDER_ETA_START: {
|
data_type="slot_data",
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
state=lambda slot: (
|
||||||
"icon": "mdi:clock-start",
|
slot["minimum_order_value"] / 100
|
||||||
"default_enabled": True,
|
if slot.get("minimum_order_value")
|
||||||
"data_type": LAST_ORDER_DATA,
|
else None
|
||||||
"state": lambda last_order: last_order.get("eta", {}).get("start"),
|
),
|
||||||
},
|
),
|
||||||
SENSOR_LAST_ORDER_ETA_END: {
|
PicnicSensorEntityDescription(
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
key=SENSOR_LAST_ORDER_SLOT_START,
|
||||||
"icon": "mdi:clock-end",
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
"default_enabled": True,
|
icon="mdi:calendar-start",
|
||||||
"data_type": LAST_ORDER_DATA,
|
data_type="last_order_data",
|
||||||
"state": lambda last_order: last_order.get("eta", {}).get("end"),
|
state=lambda last_order: last_order.get("slot", {}).get("window_start"),
|
||||||
},
|
),
|
||||||
SENSOR_LAST_ORDER_DELIVERY_TIME: {
|
PicnicSensorEntityDescription(
|
||||||
"class": DEVICE_CLASS_TIMESTAMP,
|
key=SENSOR_LAST_ORDER_SLOT_END,
|
||||||
"icon": "mdi:timeline-clock",
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
"default_enabled": True,
|
icon="mdi:calendar-end",
|
||||||
"data_type": LAST_ORDER_DATA,
|
data_type="last_order_data",
|
||||||
"state": lambda last_order: last_order.get("delivery_time", {}).get("start"),
|
state=lambda last_order: last_order.get("slot", {}).get("window_end"),
|
||||||
},
|
),
|
||||||
SENSOR_LAST_ORDER_TOTAL_PRICE: {
|
PicnicSensorEntityDescription(
|
||||||
"unit": CURRENCY_EURO,
|
key=SENSOR_LAST_ORDER_STATUS,
|
||||||
"icon": "mdi:cash-marker",
|
icon="mdi:list-status",
|
||||||
"data_type": LAST_ORDER_DATA,
|
data_type="last_order_data",
|
||||||
"state": lambda last_order: last_order.get("total_price", 0) / 100,
|
state=lambda last_order: last_order.get("status"),
|
||||||
},
|
),
|
||||||
}
|
PicnicSensorEntityDescription(
|
||||||
|
key=SENSOR_LAST_ORDER_ETA_START,
|
||||||
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
|
icon="mdi:clock-start",
|
||||||
|
entity_registry_enabled_default=True,
|
||||||
|
data_type="last_order_data",
|
||||||
|
state=lambda last_order: last_order.get("eta", {}).get("start"),
|
||||||
|
),
|
||||||
|
PicnicSensorEntityDescription(
|
||||||
|
key=SENSOR_LAST_ORDER_ETA_END,
|
||||||
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
|
icon="mdi:clock-end",
|
||||||
|
entity_registry_enabled_default=True,
|
||||||
|
data_type="last_order_data",
|
||||||
|
state=lambda last_order: last_order.get("eta", {}).get("end"),
|
||||||
|
),
|
||||||
|
PicnicSensorEntityDescription(
|
||||||
|
key=SENSOR_LAST_ORDER_DELIVERY_TIME,
|
||||||
|
device_class=DEVICE_CLASS_TIMESTAMP,
|
||||||
|
icon="mdi:timeline-clock",
|
||||||
|
entity_registry_enabled_default=True,
|
||||||
|
data_type="last_order_data",
|
||||||
|
state=lambda last_order: last_order.get("delivery_time", {}).get("start"),
|
||||||
|
),
|
||||||
|
PicnicSensorEntityDescription(
|
||||||
|
key=SENSOR_LAST_ORDER_TOTAL_PRICE,
|
||||||
|
native_unit_of_measurement=CURRENCY_EURO,
|
||||||
|
icon="mdi:cash-marker",
|
||||||
|
data_type="last_order_data",
|
||||||
|
state=lambda last_order: last_order.get("total_price", 0) / 100,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
|
@ -13,7 +13,14 @@ from homeassistant.helpers.update_coordinator import (
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
)
|
)
|
||||||
|
|
||||||
from .const import ADDRESS, ATTRIBUTION, CONF_COORDINATOR, DOMAIN, SENSOR_TYPES
|
from .const import (
|
||||||
|
ADDRESS,
|
||||||
|
ATTRIBUTION,
|
||||||
|
CONF_COORDINATOR,
|
||||||
|
DOMAIN,
|
||||||
|
SENSOR_TYPES,
|
||||||
|
PicnicSensorEntityDescription,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
|
@ -24,8 +31,8 @@ async def async_setup_entry(
|
||||||
|
|
||||||
# Add an entity for each sensor type
|
# Add an entity for each sensor type
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
PicnicSensor(picnic_coordinator, config_entry, sensor_type, props)
|
PicnicSensor(picnic_coordinator, config_entry, description)
|
||||||
for sensor_type, props in SENSOR_TYPES.items()
|
for description in SENSOR_TYPES
|
||||||
)
|
)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
|
@ -34,71 +41,40 @@ async def async_setup_entry(
|
||||||
class PicnicSensor(SensorEntity, CoordinatorEntity):
|
class PicnicSensor(SensorEntity, CoordinatorEntity):
|
||||||
"""The CoordinatorEntity subclass representing Picnic sensors."""
|
"""The CoordinatorEntity subclass representing Picnic sensors."""
|
||||||
|
|
||||||
|
entity_description: PicnicSensorEntityDescription
|
||||||
|
_attr_extra_state_attributes = {ATTR_ATTRIBUTION: ATTRIBUTION}
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: DataUpdateCoordinator[Any],
|
coordinator: DataUpdateCoordinator[Any],
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
sensor_type,
|
description: PicnicSensorEntityDescription,
|
||||||
properties,
|
) -> None:
|
||||||
):
|
|
||||||
"""Init a Picnic sensor."""
|
"""Init a Picnic sensor."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
self.sensor_type = sensor_type
|
self.entity_id = f"sensor.picnic_{description.key}"
|
||||||
self.properties = properties
|
|
||||||
self.entity_id = f"sensor.picnic_{sensor_type}"
|
|
||||||
self._service_unique_id = config_entry.unique_id
|
self._service_unique_id = config_entry.unique_id
|
||||||
|
|
||||||
@property
|
self._attr_name = self._to_capitalized_name(description.key)
|
||||||
def native_unit_of_measurement(self) -> str | None:
|
self._attr_unique_id = f"{config_entry.unique_id}.{description.key}"
|
||||||
"""Return the unit this state is expressed in."""
|
|
||||||
return self.properties.get("unit")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def unique_id(self) -> str | None:
|
|
||||||
"""Return a unique ID."""
|
|
||||||
return f"{self._service_unique_id}.{self.sensor_type}"
|
|
||||||
|
|
||||||
@property
|
|
||||||
def name(self) -> str | None:
|
|
||||||
"""Return the name of the entity."""
|
|
||||||
return self._to_capitalized_name(self.sensor_type)
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> StateType:
|
||||||
"""Return the state of the entity."""
|
"""Return the state of the entity."""
|
||||||
data_set = (
|
data_set = (
|
||||||
self.coordinator.data.get(self.properties["data_type"], {})
|
self.coordinator.data.get(self.entity_description.data_type, {})
|
||||||
if self.coordinator.data is not None
|
if self.coordinator.data is not None
|
||||||
else {}
|
else {}
|
||||||
)
|
)
|
||||||
return self.properties["state"](data_set)
|
return self.entity_description.state(data_set)
|
||||||
|
|
||||||
@property
|
|
||||||
def device_class(self) -> str | None:
|
|
||||||
"""Return the class of this device, from component DEVICE_CLASSES."""
|
|
||||||
return self.properties.get("class")
|
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self) -> str | None:
|
|
||||||
"""Return the icon to use in the frontend, if any."""
|
|
||||||
return self.properties["icon"]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return True if entity is available."""
|
"""Return True if entity is available."""
|
||||||
return self.coordinator.last_update_success and self.state is not None
|
return self.coordinator.last_update_success and self.state is not None
|
||||||
|
|
||||||
@property
|
|
||||||
def entity_registry_enabled_default(self) -> bool:
|
|
||||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
|
||||||
return self.properties.get("default_enabled", False)
|
|
||||||
|
|
||||||
@property
|
|
||||||
def extra_state_attributes(self):
|
|
||||||
"""Return the sensor specific state attributes."""
|
|
||||||
return {ATTR_ATTRIBUTION: ATTRIBUTION}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_info(self):
|
def device_info(self):
|
||||||
"""Return device info."""
|
"""Return device info."""
|
||||||
|
|
|
@ -85,6 +85,8 @@ DEFAULT_DELIVERY_RESPONSE = {
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
SENSOR_KEYS = [desc.key for desc in SENSOR_TYPES]
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.usefixtures("hass_storage")
|
@pytest.mark.usefixtures("hass_storage")
|
||||||
class TestPicnicSensor(unittest.IsolatedAsyncioTestCase):
|
class TestPicnicSensor(unittest.IsolatedAsyncioTestCase):
|
||||||
|
@ -161,7 +163,7 @@ class TestPicnicSensor(unittest.IsolatedAsyncioTestCase):
|
||||||
async def _enable_all_sensors(self):
|
async def _enable_all_sensors(self):
|
||||||
"""Enable all sensors of the Picnic integration."""
|
"""Enable all sensors of the Picnic integration."""
|
||||||
# Enable the sensors
|
# Enable the sensors
|
||||||
for sensor_type in SENSOR_TYPES.keys():
|
for sensor_type in SENSOR_KEYS:
|
||||||
updated_entry = self.entity_registry.async_update_entity(
|
updated_entry = self.entity_registry.async_update_entity(
|
||||||
f"sensor.picnic_{sensor_type}", disabled_by=None
|
f"sensor.picnic_{sensor_type}", disabled_by=None
|
||||||
)
|
)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue