mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 19:27:45 +00:00
Use entity descriptions in Switcher (#98958)
This commit is contained in:
parent
3a71e21d6a
commit
57144a6064
@ -1,19 +1,19 @@
|
|||||||
"""Switcher integration Sensor platform."""
|
"""Switcher integration Sensor platform."""
|
||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from dataclasses import dataclass
|
|
||||||
|
|
||||||
from aioswitcher.device import DeviceCategory
|
from aioswitcher.device import DeviceCategory
|
||||||
|
|
||||||
from homeassistant.components.sensor import (
|
from homeassistant.components.sensor import (
|
||||||
SensorDeviceClass,
|
SensorDeviceClass,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import UnitOfElectricCurrent, UnitOfPower
|
from homeassistant.const import UnitOfElectricCurrent, UnitOfPower
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
from homeassistant.helpers import device_registry as dr
|
from homeassistant.helpers import device_registry as dr
|
||||||
|
from homeassistant.helpers.device_registry import DeviceInfo
|
||||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import StateType
|
from homeassistant.helpers.typing import StateType
|
||||||
@ -22,48 +22,36 @@ from homeassistant.helpers.update_coordinator import CoordinatorEntity
|
|||||||
from . import SwitcherDataUpdateCoordinator
|
from . import SwitcherDataUpdateCoordinator
|
||||||
from .const import SIGNAL_DEVICE_ADD
|
from .const import SIGNAL_DEVICE_ADD
|
||||||
|
|
||||||
|
POWER_SENSORS: list[SensorEntityDescription] = [
|
||||||
@dataclass
|
SensorEntityDescription(
|
||||||
class AttributeDescription:
|
key="power_consumption",
|
||||||
"""Class to describe a sensor."""
|
|
||||||
|
|
||||||
name: str
|
|
||||||
icon: str | None = None
|
|
||||||
unit: str | None = None
|
|
||||||
device_class: SensorDeviceClass | None = None
|
|
||||||
state_class: SensorStateClass | None = None
|
|
||||||
default_enabled: bool = True
|
|
||||||
|
|
||||||
|
|
||||||
POWER_SENSORS = {
|
|
||||||
"power_consumption": AttributeDescription(
|
|
||||||
name="Power Consumption",
|
name="Power Consumption",
|
||||||
unit=UnitOfPower.WATT,
|
native_unit_of_measurement=UnitOfPower.WATT,
|
||||||
device_class=SensorDeviceClass.POWER,
|
device_class=SensorDeviceClass.POWER,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
),
|
),
|
||||||
"electric_current": AttributeDescription(
|
SensorEntityDescription(
|
||||||
|
key="electric_current",
|
||||||
name="Electric Current",
|
name="Electric Current",
|
||||||
unit=UnitOfElectricCurrent.AMPERE,
|
native_unit_of_measurement=UnitOfElectricCurrent.AMPERE,
|
||||||
device_class=SensorDeviceClass.CURRENT,
|
device_class=SensorDeviceClass.CURRENT,
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
),
|
),
|
||||||
}
|
]
|
||||||
|
TIME_SENSORS: list[SensorEntityDescription] = [
|
||||||
TIME_SENSORS = {
|
SensorEntityDescription(
|
||||||
"remaining_time": AttributeDescription(
|
key="remaining_time", name="Remaining Time", icon="mdi:av-timer"
|
||||||
name="Remaining Time",
|
|
||||||
icon="mdi:av-timer",
|
|
||||||
),
|
),
|
||||||
"auto_off_set": AttributeDescription(
|
SensorEntityDescription(
|
||||||
|
key="auto_off_set",
|
||||||
name="Auto Shutdown",
|
name="Auto Shutdown",
|
||||||
icon="mdi:progress-clock",
|
icon="mdi:progress-clock",
|
||||||
default_enabled=False,
|
entity_registry_enabled_default=False,
|
||||||
),
|
),
|
||||||
}
|
]
|
||||||
|
|
||||||
POWER_PLUG_SENSORS = POWER_SENSORS
|
POWER_PLUG_SENSORS = POWER_SENSORS
|
||||||
WATER_HEATER_SENSORS = {**POWER_SENSORS, **TIME_SENSORS}
|
WATER_HEATER_SENSORS = [*POWER_SENSORS, *TIME_SENSORS]
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(
|
async def async_setup_entry(
|
||||||
@ -78,13 +66,13 @@ async def async_setup_entry(
|
|||||||
"""Add sensors from Switcher device."""
|
"""Add sensors from Switcher device."""
|
||||||
if coordinator.data.device_type.category == DeviceCategory.POWER_PLUG:
|
if coordinator.data.device_type.category == DeviceCategory.POWER_PLUG:
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SwitcherSensorEntity(coordinator, attribute, info)
|
SwitcherSensorEntity(coordinator, description)
|
||||||
for attribute, info in POWER_PLUG_SENSORS.items()
|
for description in POWER_PLUG_SENSORS
|
||||||
)
|
)
|
||||||
elif coordinator.data.device_type.category == DeviceCategory.WATER_HEATER:
|
elif coordinator.data.device_type.category == DeviceCategory.WATER_HEATER:
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
SwitcherSensorEntity(coordinator, attribute, info)
|
SwitcherSensorEntity(coordinator, description)
|
||||||
for attribute, info in WATER_HEATER_SENSORS.items()
|
for description in WATER_HEATER_SENSORS
|
||||||
)
|
)
|
||||||
|
|
||||||
config_entry.async_on_unload(
|
config_entry.async_on_unload(
|
||||||
@ -100,28 +88,23 @@ class SwitcherSensorEntity(
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
coordinator: SwitcherDataUpdateCoordinator,
|
coordinator: SwitcherDataUpdateCoordinator,
|
||||||
attribute: str,
|
description: SensorEntityDescription,
|
||||||
description: AttributeDescription,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize the entity."""
|
"""Initialize the entity."""
|
||||||
super().__init__(coordinator)
|
super().__init__(coordinator)
|
||||||
self.attribute = attribute
|
self.entity_description = description
|
||||||
|
|
||||||
# Entity class attributes
|
# Entity class attributes
|
||||||
self._attr_name = f"{coordinator.name} {description.name}"
|
self._attr_name = f"{coordinator.name} {description.name}"
|
||||||
self._attr_icon = description.icon
|
|
||||||
self._attr_native_unit_of_measurement = description.unit
|
|
||||||
self._attr_device_class = description.device_class
|
|
||||||
self._attr_entity_registry_enabled_default = description.default_enabled
|
|
||||||
|
|
||||||
self._attr_unique_id = (
|
self._attr_unique_id = (
|
||||||
f"{coordinator.device_id}-{coordinator.mac_address}-{attribute}"
|
f"{coordinator.device_id}-{coordinator.mac_address}-{description.key}"
|
||||||
|
)
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
connections={(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
|
||||||
)
|
)
|
||||||
self._attr_device_info = {
|
|
||||||
"connections": {(dr.CONNECTION_NETWORK_MAC, coordinator.mac_address)}
|
|
||||||
}
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self) -> StateType:
|
def native_value(self) -> StateType:
|
||||||
"""Return value of sensor."""
|
"""Return value of sensor."""
|
||||||
return getattr(self.coordinator.data, self.attribute) # type: ignore[no-any-return]
|
return getattr(self.coordinator.data, self.entity_description.key) # type: ignore[no-any-return]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user