mirror of
https://github.com/home-assistant/core.git
synced 2025-07-23 21:27:38 +00:00
Use EntityDescription - broadlink (#55019)
This commit is contained in:
parent
a527872a10
commit
2796f65453
@ -1,4 +1,6 @@
|
|||||||
"""Support for Broadlink sensors."""
|
"""Support for Broadlink sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
@ -11,6 +13,7 @@ from homeassistant.components.sensor import (
|
|||||||
PLATFORM_SCHEMA,
|
PLATFORM_SCHEMA,
|
||||||
STATE_CLASS_MEASUREMENT,
|
STATE_CLASS_MEASUREMENT,
|
||||||
SensorEntity,
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
)
|
)
|
||||||
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
|
from homeassistant.const import CONF_HOST, PERCENTAGE, POWER_WATT, TEMP_CELSIUS
|
||||||
from homeassistant.helpers import config_validation as cv
|
from homeassistant.helpers import config_validation as cv
|
||||||
@ -21,29 +24,42 @@ from .helpers import import_device
|
|||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
|
||||||
"temperature": (
|
SensorEntityDescription(
|
||||||
"Temperature",
|
key="temperature",
|
||||||
TEMP_CELSIUS,
|
name="Temperature",
|
||||||
DEVICE_CLASS_TEMPERATURE,
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
STATE_CLASS_MEASUREMENT,
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
),
|
),
|
||||||
"air_quality": ("Air Quality", None, None, None),
|
SensorEntityDescription(
|
||||||
"humidity": (
|
key="air_quality",
|
||||||
"Humidity",
|
name="Air Quality",
|
||||||
PERCENTAGE,
|
|
||||||
DEVICE_CLASS_HUMIDITY,
|
|
||||||
STATE_CLASS_MEASUREMENT,
|
|
||||||
),
|
),
|
||||||
"light": ("Light", None, DEVICE_CLASS_ILLUMINANCE, None),
|
SensorEntityDescription(
|
||||||
"noise": ("Noise", None, None, None),
|
key="humidity",
|
||||||
"power": (
|
name="Humidity",
|
||||||
"Current power",
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
POWER_WATT,
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_POWER,
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
STATE_CLASS_MEASUREMENT,
|
|
||||||
),
|
),
|
||||||
}
|
SensorEntityDescription(
|
||||||
|
key="light",
|
||||||
|
name="Light",
|
||||||
|
device_class=DEVICE_CLASS_ILLUMINANCE,
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="noise",
|
||||||
|
name="Noise",
|
||||||
|
),
|
||||||
|
SensorEntityDescription(
|
||||||
|
key="power",
|
||||||
|
name="Current power",
|
||||||
|
native_unit_of_measurement=POWER_WATT,
|
||||||
|
device_class=DEVICE_CLASS_POWER,
|
||||||
|
state_class=STATE_CLASS_MEASUREMENT,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||||
{vol.Required(CONF_HOST): cv.string}, extra=vol.ALLOW_EXTRA
|
{vol.Required(CONF_HOST): cv.string}, extra=vol.ALLOW_EXTRA
|
||||||
@ -67,13 +83,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
device = hass.data[DOMAIN].devices[config_entry.entry_id]
|
device = hass.data[DOMAIN].devices[config_entry.entry_id]
|
||||||
sensor_data = device.update_manager.coordinator.data
|
sensor_data = device.update_manager.coordinator.data
|
||||||
sensors = [
|
sensors = [
|
||||||
BroadlinkSensor(device, monitored_condition)
|
BroadlinkSensor(device, description)
|
||||||
for monitored_condition in sensor_data
|
for description in SENSOR_TYPES
|
||||||
if monitored_condition in SENSOR_TYPES
|
if description.key in sensor_data
|
||||||
and (
|
and (
|
||||||
# These devices have optional sensors.
|
# These devices have optional sensors.
|
||||||
# We don't create entities if the value is 0.
|
# We don't create entities if the value is 0.
|
||||||
sensor_data[monitored_condition] != 0
|
sensor_data[description.key] != 0
|
||||||
or device.api.type not in {"RM4PRO", "RM4MINI"}
|
or device.api.type not in {"RM4PRO", "RM4MINI"}
|
||||||
)
|
)
|
||||||
]
|
]
|
||||||
@ -83,18 +99,15 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
class BroadlinkSensor(BroadlinkEntity, SensorEntity):
|
||||||
"""Representation of a Broadlink sensor."""
|
"""Representation of a Broadlink sensor."""
|
||||||
|
|
||||||
def __init__(self, device, monitored_condition):
|
def __init__(self, device, description: SensorEntityDescription):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(device)
|
super().__init__(device)
|
||||||
self._monitored_condition = monitored_condition
|
self.entity_description = description
|
||||||
|
|
||||||
self._attr_device_class = SENSOR_TYPES[monitored_condition][2]
|
self._attr_name = f"{device.name} {description.name}"
|
||||||
self._attr_name = f"{device.name} {SENSOR_TYPES[monitored_condition][0]}"
|
self._attr_native_value = self._coordinator.data[description.key]
|
||||||
self._attr_state_class = SENSOR_TYPES[monitored_condition][3]
|
self._attr_unique_id = f"{device.unique_id}-{description.key}"
|
||||||
self._attr_native_value = self._coordinator.data[monitored_condition]
|
|
||||||
self._attr_unique_id = f"{device.unique_id}-{monitored_condition}"
|
|
||||||
self._attr_native_unit_of_measurement = SENSOR_TYPES[monitored_condition][1]
|
|
||||||
|
|
||||||
def _update_state(self, data):
|
def _update_state(self, data):
|
||||||
"""Update the state of the entity."""
|
"""Update the state of the entity."""
|
||||||
self._attr_native_value = data[self._monitored_condition]
|
self._attr_native_value = data[self.entity_description.key]
|
||||||
|
Loading…
x
Reference in New Issue
Block a user