Use EntityDescription - notion (#55120)

This commit is contained in:
Aaron Bach 2021-08-25 02:42:57 -06:00 committed by GitHub
parent 4a03d8dc47
commit f92ba18a6b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 88 additions and 69 deletions

View File

@ -17,6 +17,7 @@ from homeassistant.helpers import (
config_validation as cv,
device_registry as dr,
)
from homeassistant.helpers.entity import EntityDescription
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
@ -137,14 +138,11 @@ class NotionEntity(CoordinatorEntity):
sensor_id: str,
bridge_id: str,
system_id: str,
name: str,
device_class: str,
description: EntityDescription,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self._attr_device_class = device_class
bridge = self.coordinator.data["bridges"].get(bridge_id, {})
sensor = self.coordinator.data["sensors"][sensor_id]
self._attr_device_info = {
@ -157,7 +155,7 @@ class NotionEntity(CoordinatorEntity):
}
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
self._attr_name = f'{sensor["name"]}: {name}'
self._attr_name = f'{sensor["name"]}: {description.name}'
self._attr_unique_id = (
f'{sensor_id}_{coordinator.data["tasks"][task_id]["task_type"]}'
)
@ -165,6 +163,7 @@ class NotionEntity(CoordinatorEntity):
self._sensor_id = sensor_id
self._system_id = system_id
self._task_id = task_id
self.entity_description = description
@property
def available(self) -> bool:

View File

@ -1,11 +1,16 @@
"""Support for Notion binary sensors."""
from __future__ import annotations
from homeassistant.components.binary_sensor import (
DEVICE_CLASS_BATTERY,
DEVICE_CLASS_CONNECTIVITY,
DEVICE_CLASS_DOOR,
DEVICE_CLASS_GARAGE_DOOR,
DEVICE_CLASS_MOISTURE,
DEVICE_CLASS_SMOKE,
DEVICE_CLASS_WINDOW,
BinarySensorEntity,
BinarySensorEntityDescription,
)
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant, callback
@ -28,18 +33,58 @@ from .const import (
SENSOR_WINDOW_HINGED_VERTICAL,
)
BINARY_SENSOR_TYPES = {
SENSOR_BATTERY: ("Low Battery", "battery"),
SENSOR_DOOR: ("Door", DEVICE_CLASS_DOOR),
SENSOR_GARAGE_DOOR: ("Garage Door", "garage_door"),
SENSOR_LEAK: ("Leak Detector", DEVICE_CLASS_MOISTURE),
SENSOR_MISSING: ("Missing", DEVICE_CLASS_CONNECTIVITY),
SENSOR_SAFE: ("Safe", DEVICE_CLASS_DOOR),
SENSOR_SLIDING: ("Sliding Door/Window", DEVICE_CLASS_DOOR),
SENSOR_SMOKE_CO: ("Smoke/Carbon Monoxide Detector", DEVICE_CLASS_SMOKE),
SENSOR_WINDOW_HINGED_HORIZONTAL: ("Hinged Window", DEVICE_CLASS_WINDOW),
SENSOR_WINDOW_HINGED_VERTICAL: ("Hinged Window", DEVICE_CLASS_WINDOW),
}
BINARY_SENSOR_DESCRIPTIONS = (
BinarySensorEntityDescription(
key=SENSOR_BATTERY,
name="Low Battery",
device_class=DEVICE_CLASS_BATTERY,
),
BinarySensorEntityDescription(
key=SENSOR_DOOR,
name="Door",
device_class=DEVICE_CLASS_DOOR,
),
BinarySensorEntityDescription(
key=SENSOR_GARAGE_DOOR,
name="Garage Door",
device_class=DEVICE_CLASS_GARAGE_DOOR,
),
BinarySensorEntityDescription(
key=SENSOR_LEAK,
name="Leak Detector",
device_class=DEVICE_CLASS_MOISTURE,
),
BinarySensorEntityDescription(
key=SENSOR_MISSING,
name="Missing",
device_class=DEVICE_CLASS_CONNECTIVITY,
),
BinarySensorEntityDescription(
key=SENSOR_SAFE,
name="Safe",
device_class=DEVICE_CLASS_DOOR,
),
BinarySensorEntityDescription(
key=SENSOR_SLIDING,
name="Sliding Door/Window",
device_class=DEVICE_CLASS_DOOR,
),
BinarySensorEntityDescription(
key=SENSOR_SMOKE_CO,
name="Smoke/Carbon Monoxide Detector",
device_class=DEVICE_CLASS_SMOKE,
),
BinarySensorEntityDescription(
key=SENSOR_WINDOW_HINGED_HORIZONTAL,
name="Hinged Window",
device_class=DEVICE_CLASS_WINDOW,
),
BinarySensorEntityDescription(
key=SENSOR_WINDOW_HINGED_VERTICAL,
name="Hinged Window",
device_class=DEVICE_CLASS_WINDOW,
),
)
async def async_setup_entry(
@ -48,28 +93,23 @@ async def async_setup_entry(
"""Set up Notion sensors based on a config entry."""
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
sensor_list = []
for task_id, task in coordinator.data["tasks"].items():
if task["task_type"] not in BINARY_SENSOR_TYPES:
continue
name, device_class = BINARY_SENSOR_TYPES[task["task_type"]]
sensor = coordinator.data["sensors"][task["sensor_id"]]
sensor_list.append(
async_add_entities(
[
NotionBinarySensor(
coordinator,
task_id,
sensor["id"],
sensor["bridge"]["id"],
sensor["system_id"],
name,
device_class,
description,
)
for task_id, task in coordinator.data["tasks"].items()
for description in BINARY_SENSOR_DESCRIPTIONS
if description.key == task["task_type"]
and (sensor := coordinator.data["sensors"][task["sensor_id"]])
]
)
async_add_entities(sensor_list)
class NotionBinarySensor(NotionEntity, BinarySensorEntity):
"""Define a Notion sensor."""

View File

@ -1,17 +1,21 @@
"""Support for Notion sensors."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
from . import NotionEntity
from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE
SENSOR_TYPES = {
SENSOR_TEMPERATURE: ("Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS)
}
SENSOR_DESCRIPTIONS = (
SensorEntityDescription(
key=SENSOR_TEMPERATURE,
name="Temperature",
device_class=DEVICE_CLASS_TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
),
)
async def async_setup_entry(
@ -20,51 +24,27 @@ async def async_setup_entry(
"""Set up Notion sensors based on a config entry."""
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
sensor_list = []
for task_id, task in coordinator.data["tasks"].items():
if task["task_type"] not in SENSOR_TYPES:
continue
name, device_class, unit = SENSOR_TYPES[task["task_type"]]
sensor = coordinator.data["sensors"][task["sensor_id"]]
sensor_list.append(
async_add_entities(
[
NotionSensor(
coordinator,
task_id,
sensor["id"],
sensor["bridge"]["id"],
sensor["system_id"],
name,
device_class,
unit,
description,
)
for task_id, task in coordinator.data["tasks"].items()
for description in SENSOR_DESCRIPTIONS
if description.key == task["task_type"]
and (sensor := coordinator.data["sensors"][task["sensor_id"]])
]
)
async_add_entities(sensor_list)
class NotionSensor(NotionEntity, SensorEntity):
"""Define a Notion sensor."""
def __init__(
self,
coordinator: DataUpdateCoordinator,
task_id: str,
sensor_id: str,
bridge_id: str,
system_id: str,
name: str,
device_class: str,
unit: str,
) -> None:
"""Initialize the entity."""
super().__init__(
coordinator, task_id, sensor_id, bridge_id, system_id, name, device_class
)
self._attr_native_unit_of_measurement = unit
@callback
def _async_update_from_latest_data(self) -> None:
"""Fetch new state data for the sensor."""