2021-10-30 14:27:48 +00:00
|
|
|
|
"""Support for Ridwell sensors."""
|
|
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
|
|
from collections.abc import Mapping
|
2021-11-19 05:24:12 +00:00
|
|
|
|
from datetime import date, datetime
|
2021-10-30 14:27:48 +00:00
|
|
|
|
from typing import Any
|
|
|
|
|
|
2021-12-18 23:26:37 +00:00
|
|
|
|
from aioridwell.model import RidwellAccount, RidwellPickupEvent
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
2021-12-19 06:06:17 +00:00
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
|
SensorDeviceClass,
|
|
|
|
|
SensorEntity,
|
|
|
|
|
SensorEntityDescription,
|
|
|
|
|
)
|
2021-10-30 14:27:48 +00:00
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-11-06 16:11:00 +00:00
|
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-30 14:27:48 +00:00
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
from homeassistant.helpers.typing import StateType
|
2021-12-19 06:06:17 +00:00
|
|
|
|
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
2021-12-19 06:06:17 +00:00
|
|
|
|
from . import RidwellEntity
|
|
|
|
|
from .const import DATA_ACCOUNT, DATA_COORDINATOR, DOMAIN, SENSOR_TYPE_NEXT_PICKUP
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
|
|
|
|
ATTR_CATEGORY = "category"
|
|
|
|
|
ATTR_PICKUP_STATE = "pickup_state"
|
|
|
|
|
ATTR_PICKUP_TYPES = "pickup_types"
|
|
|
|
|
ATTR_QUANTITY = "quantity"
|
|
|
|
|
|
2021-12-19 06:06:17 +00:00
|
|
|
|
SENSOR_DESCRIPTION = SensorEntityDescription(
|
|
|
|
|
key=SENSOR_TYPE_NEXT_PICKUP,
|
|
|
|
|
name="Ridwell Pickup",
|
|
|
|
|
device_class=SensorDeviceClass.DATE,
|
|
|
|
|
)
|
|
|
|
|
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
|
) -> None:
|
2021-12-19 06:06:17 +00:00
|
|
|
|
"""Set up Ridwell sensors based on a config entry."""
|
2021-10-30 14:27:48 +00:00
|
|
|
|
accounts = hass.data[DOMAIN][entry.entry_id][DATA_ACCOUNT]
|
|
|
|
|
coordinator = hass.data[DOMAIN][entry.entry_id][DATA_COORDINATOR]
|
2021-12-19 06:06:17 +00:00
|
|
|
|
|
2021-10-30 14:27:48 +00:00
|
|
|
|
async_add_entities(
|
2021-12-19 06:06:17 +00:00
|
|
|
|
[
|
|
|
|
|
RidwellSensor(coordinator, account, SENSOR_DESCRIPTION)
|
|
|
|
|
for account in accounts.values()
|
|
|
|
|
]
|
2021-10-30 14:27:48 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
2021-12-19 06:06:17 +00:00
|
|
|
|
class RidwellSensor(RidwellEntity, SensorEntity):
|
2021-10-30 14:27:48 +00:00
|
|
|
|
"""Define a Ridwell pickup sensor."""
|
|
|
|
|
|
|
|
|
|
def __init__(
|
2021-12-19 06:06:17 +00:00
|
|
|
|
self,
|
|
|
|
|
coordinator: DataUpdateCoordinator,
|
|
|
|
|
account: RidwellAccount,
|
|
|
|
|
description: SensorEntityDescription,
|
2021-10-30 14:27:48 +00:00
|
|
|
|
) -> None:
|
2021-12-19 06:06:17 +00:00
|
|
|
|
"""Initialize."""
|
|
|
|
|
super().__init__(coordinator, account, description)
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
2021-12-19 06:06:17 +00:00
|
|
|
|
self._attr_name = f"{description.name} ({account.address['street1']})"
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
|
|
|
|
@property
|
|
|
|
|
def extra_state_attributes(self) -> Mapping[str, Any]:
|
|
|
|
|
"""Return entity specific state attributes."""
|
|
|
|
|
event = self.coordinator.data[self._account.account_id]
|
|
|
|
|
|
|
|
|
|
attrs: dict[str, Any] = {
|
|
|
|
|
ATTR_PICKUP_TYPES: {},
|
2021-12-18 23:26:37 +00:00
|
|
|
|
ATTR_PICKUP_STATE: event.state.value,
|
2021-10-30 14:27:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for pickup in event.pickups:
|
|
|
|
|
if pickup.name not in attrs[ATTR_PICKUP_TYPES]:
|
|
|
|
|
attrs[ATTR_PICKUP_TYPES][pickup.name] = {
|
2021-12-18 23:26:37 +00:00
|
|
|
|
ATTR_CATEGORY: pickup.category.value,
|
2021-10-30 14:27:48 +00:00
|
|
|
|
ATTR_QUANTITY: pickup.quantity,
|
|
|
|
|
}
|
|
|
|
|
else:
|
|
|
|
|
# Ridwell's API will return distinct objects, even if they have the
|
|
|
|
|
# same name (e.g. two pickups of Latex Paint will show up as two
|
|
|
|
|
# objects) – so, we sum the quantities:
|
2021-11-06 16:11:00 +00:00
|
|
|
|
attrs[ATTR_PICKUP_TYPES][pickup.name][ATTR_QUANTITY] += pickup.quantity
|
2021-10-30 14:27:48 +00:00
|
|
|
|
|
|
|
|
|
return attrs
|
|
|
|
|
|
|
|
|
|
@property
|
2021-11-19 05:24:12 +00:00
|
|
|
|
def native_value(self) -> StateType | date | datetime:
|
2021-10-30 14:27:48 +00:00
|
|
|
|
"""Return the value reported by the sensor."""
|
2021-11-06 16:11:00 +00:00
|
|
|
|
event: RidwellPickupEvent = self.coordinator.data[self._account.account_id]
|
2021-11-19 05:24:12 +00:00
|
|
|
|
return event.pickup_date
|