mirror of
https://github.com/home-assistant/core.git
synced 2025-07-13 16:27:08 +00:00
Use EntityDescription - notion (#55120)
This commit is contained in:
parent
4a03d8dc47
commit
f92ba18a6b
@ -17,6 +17,7 @@ from homeassistant.helpers import (
|
|||||||
config_validation as cv,
|
config_validation as cv,
|
||||||
device_registry as dr,
|
device_registry as dr,
|
||||||
)
|
)
|
||||||
|
from homeassistant.helpers.entity import EntityDescription
|
||||||
from homeassistant.helpers.update_coordinator import (
|
from homeassistant.helpers.update_coordinator import (
|
||||||
CoordinatorEntity,
|
CoordinatorEntity,
|
||||||
DataUpdateCoordinator,
|
DataUpdateCoordinator,
|
||||||
@ -137,14 +138,11 @@ class NotionEntity(CoordinatorEntity):
|
|||||||
sensor_id: str,
|
sensor_id: str,
|
||||||
bridge_id: str,
|
bridge_id: str,
|
||||||
system_id: str,
|
system_id: str,
|
||||||
name: str,
|
description: EntityDescription,
|
||||||
device_class: str,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
|
|
||||||
self._attr_device_class = device_class
|
|
||||||
|
|
||||||
bridge = self.coordinator.data["bridges"].get(bridge_id, {})
|
bridge = self.coordinator.data["bridges"].get(bridge_id, {})
|
||||||
sensor = self.coordinator.data["sensors"][sensor_id]
|
sensor = self.coordinator.data["sensors"][sensor_id]
|
||||||
self._attr_device_info = {
|
self._attr_device_info = {
|
||||||
@ -157,7 +155,7 @@ class NotionEntity(CoordinatorEntity):
|
|||||||
}
|
}
|
||||||
|
|
||||||
self._attr_extra_state_attributes = {ATTR_ATTRIBUTION: DEFAULT_ATTRIBUTION}
|
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 = (
|
self._attr_unique_id = (
|
||||||
f'{sensor_id}_{coordinator.data["tasks"][task_id]["task_type"]}'
|
f'{sensor_id}_{coordinator.data["tasks"][task_id]["task_type"]}'
|
||||||
)
|
)
|
||||||
@ -165,6 +163,7 @@ class NotionEntity(CoordinatorEntity):
|
|||||||
self._sensor_id = sensor_id
|
self._sensor_id = sensor_id
|
||||||
self._system_id = system_id
|
self._system_id = system_id
|
||||||
self._task_id = task_id
|
self._task_id = task_id
|
||||||
|
self.entity_description = description
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
|
@ -1,11 +1,16 @@
|
|||||||
"""Support for Notion binary sensors."""
|
"""Support for Notion binary sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
|
DEVICE_CLASS_BATTERY,
|
||||||
DEVICE_CLASS_CONNECTIVITY,
|
DEVICE_CLASS_CONNECTIVITY,
|
||||||
DEVICE_CLASS_DOOR,
|
DEVICE_CLASS_DOOR,
|
||||||
|
DEVICE_CLASS_GARAGE_DOOR,
|
||||||
DEVICE_CLASS_MOISTURE,
|
DEVICE_CLASS_MOISTURE,
|
||||||
DEVICE_CLASS_SMOKE,
|
DEVICE_CLASS_SMOKE,
|
||||||
DEVICE_CLASS_WINDOW,
|
DEVICE_CLASS_WINDOW,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
|
BinarySensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
@ -28,18 +33,58 @@ from .const import (
|
|||||||
SENSOR_WINDOW_HINGED_VERTICAL,
|
SENSOR_WINDOW_HINGED_VERTICAL,
|
||||||
)
|
)
|
||||||
|
|
||||||
BINARY_SENSOR_TYPES = {
|
BINARY_SENSOR_DESCRIPTIONS = (
|
||||||
SENSOR_BATTERY: ("Low Battery", "battery"),
|
BinarySensorEntityDescription(
|
||||||
SENSOR_DOOR: ("Door", DEVICE_CLASS_DOOR),
|
key=SENSOR_BATTERY,
|
||||||
SENSOR_GARAGE_DOOR: ("Garage Door", "garage_door"),
|
name="Low Battery",
|
||||||
SENSOR_LEAK: ("Leak Detector", DEVICE_CLASS_MOISTURE),
|
device_class=DEVICE_CLASS_BATTERY,
|
||||||
SENSOR_MISSING: ("Missing", DEVICE_CLASS_CONNECTIVITY),
|
),
|
||||||
SENSOR_SAFE: ("Safe", DEVICE_CLASS_DOOR),
|
BinarySensorEntityDescription(
|
||||||
SENSOR_SLIDING: ("Sliding Door/Window", DEVICE_CLASS_DOOR),
|
key=SENSOR_DOOR,
|
||||||
SENSOR_SMOKE_CO: ("Smoke/Carbon Monoxide Detector", DEVICE_CLASS_SMOKE),
|
name="Door",
|
||||||
SENSOR_WINDOW_HINGED_HORIZONTAL: ("Hinged Window", DEVICE_CLASS_WINDOW),
|
device_class=DEVICE_CLASS_DOOR,
|
||||||
SENSOR_WINDOW_HINGED_VERTICAL: ("Hinged Window", DEVICE_CLASS_WINDOW),
|
),
|
||||||
}
|
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(
|
async def async_setup_entry(
|
||||||
@ -48,27 +93,22 @@ async def async_setup_entry(
|
|||||||
"""Set up Notion sensors based on a config entry."""
|
"""Set up Notion sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||||
|
|
||||||
sensor_list = []
|
async_add_entities(
|
||||||
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(
|
|
||||||
NotionBinarySensor(
|
NotionBinarySensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
task_id,
|
task_id,
|
||||||
sensor["id"],
|
sensor["id"],
|
||||||
sensor["bridge"]["id"],
|
sensor["bridge"]["id"],
|
||||||
sensor["system_id"],
|
sensor["system_id"],
|
||||||
name,
|
description,
|
||||||
device_class,
|
|
||||||
)
|
)
|
||||||
)
|
for task_id, task in coordinator.data["tasks"].items()
|
||||||
|
for description in BINARY_SENSOR_DESCRIPTIONS
|
||||||
async_add_entities(sensor_list)
|
if description.key == task["task_type"]
|
||||||
|
and (sensor := coordinator.data["sensors"][task["sensor_id"]])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NotionBinarySensor(NotionEntity, BinarySensorEntity):
|
class NotionBinarySensor(NotionEntity, BinarySensorEntity):
|
||||||
|
@ -1,17 +1,21 @@
|
|||||||
"""Support for Notion sensors."""
|
"""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.config_entries import ConfigEntry
|
||||||
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
|
from homeassistant.const import DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
|
||||||
|
|
||||||
from . import NotionEntity
|
from . import NotionEntity
|
||||||
from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE
|
from .const import DATA_COORDINATOR, DOMAIN, LOGGER, SENSOR_TEMPERATURE
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_DESCRIPTIONS = (
|
||||||
SENSOR_TEMPERATURE: ("Temperature", DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS)
|
SensorEntityDescription(
|
||||||
}
|
key=SENSOR_TEMPERATURE,
|
||||||
|
name="Temperature",
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -20,51 +24,27 @@ async def async_setup_entry(
|
|||||||
"""Set up Notion sensors based on a config entry."""
|
"""Set up Notion sensors based on a config entry."""
|
||||||
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
coordinator = hass.data[DOMAIN][DATA_COORDINATOR][entry.entry_id]
|
||||||
|
|
||||||
sensor_list = []
|
async_add_entities(
|
||||||
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(
|
|
||||||
NotionSensor(
|
NotionSensor(
|
||||||
coordinator,
|
coordinator,
|
||||||
task_id,
|
task_id,
|
||||||
sensor["id"],
|
sensor["id"],
|
||||||
sensor["bridge"]["id"],
|
sensor["bridge"]["id"],
|
||||||
sensor["system_id"],
|
sensor["system_id"],
|
||||||
name,
|
description,
|
||||||
device_class,
|
|
||||||
unit,
|
|
||||||
)
|
)
|
||||||
)
|
for task_id, task in coordinator.data["tasks"].items()
|
||||||
|
for description in SENSOR_DESCRIPTIONS
|
||||||
async_add_entities(sensor_list)
|
if description.key == task["task_type"]
|
||||||
|
and (sensor := coordinator.data["sensors"][task["sensor_id"]])
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class NotionSensor(NotionEntity, SensorEntity):
|
class NotionSensor(NotionEntity, SensorEntity):
|
||||||
"""Define a Notion sensor."""
|
"""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
|
@callback
|
||||||
def _async_update_from_latest_data(self) -> None:
|
def _async_update_from_latest_data(self) -> None:
|
||||||
"""Fetch new state data for the sensor."""
|
"""Fetch new state data for the sensor."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user