mirror of
https://github.com/home-assistant/core.git
synced 2025-07-24 13:47:35 +00:00
Use EntityDescription - point (#54363)
This commit is contained in:
parent
f9fbcd4aec
commit
236ccb933c
@ -1,7 +1,14 @@
|
|||||||
"""Support for Minut Point sensors."""
|
"""Support for Minut Point sensors."""
|
||||||
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from homeassistant.components.sensor import DOMAIN, SensorEntity
|
from homeassistant.components.sensor import (
|
||||||
|
DOMAIN,
|
||||||
|
SensorEntity,
|
||||||
|
SensorEntityDescription,
|
||||||
|
)
|
||||||
from homeassistant.const import (
|
from homeassistant.const import (
|
||||||
DEVICE_CLASS_HUMIDITY,
|
DEVICE_CLASS_HUMIDITY,
|
||||||
DEVICE_CLASS_PRESSURE,
|
DEVICE_CLASS_PRESSURE,
|
||||||
@ -21,12 +28,48 @@ _LOGGER = logging.getLogger(__name__)
|
|||||||
|
|
||||||
DEVICE_CLASS_SOUND = "sound_level"
|
DEVICE_CLASS_SOUND = "sound_level"
|
||||||
|
|
||||||
SENSOR_TYPES = {
|
|
||||||
DEVICE_CLASS_TEMPERATURE: (None, 1, TEMP_CELSIUS),
|
@dataclass
|
||||||
DEVICE_CLASS_PRESSURE: (None, 0, PRESSURE_HPA),
|
class MinutPointRequiredKeysMixin:
|
||||||
DEVICE_CLASS_HUMIDITY: (None, 1, PERCENTAGE),
|
"""Mixin for required keys."""
|
||||||
DEVICE_CLASS_SOUND: ("mdi:ear-hearing", 1, SOUND_PRESSURE_WEIGHTED_DBA),
|
|
||||||
}
|
precision: int
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class MinutPointSensorEntityDescription(
|
||||||
|
SensorEntityDescription, MinutPointRequiredKeysMixin
|
||||||
|
):
|
||||||
|
"""Describes MinutPoint sensor entity."""
|
||||||
|
|
||||||
|
|
||||||
|
SENSOR_TYPES: tuple[MinutPointSensorEntityDescription, ...] = (
|
||||||
|
MinutPointSensorEntityDescription(
|
||||||
|
key="temperature",
|
||||||
|
precision=1,
|
||||||
|
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||||
|
native_unit_of_measurement=TEMP_CELSIUS,
|
||||||
|
),
|
||||||
|
MinutPointSensorEntityDescription(
|
||||||
|
key="pressure",
|
||||||
|
precision=0,
|
||||||
|
device_class=DEVICE_CLASS_PRESSURE,
|
||||||
|
native_unit_of_measurement=PRESSURE_HPA,
|
||||||
|
),
|
||||||
|
MinutPointSensorEntityDescription(
|
||||||
|
key="humidity",
|
||||||
|
precision=1,
|
||||||
|
device_class=DEVICE_CLASS_HUMIDITY,
|
||||||
|
native_unit_of_measurement=PERCENTAGE,
|
||||||
|
),
|
||||||
|
MinutPointSensorEntityDescription(
|
||||||
|
key="sound",
|
||||||
|
precision=1,
|
||||||
|
device_class=DEVICE_CLASS_SOUND,
|
||||||
|
icon="mdi:ear-hearing",
|
||||||
|
native_unit_of_measurement=SOUND_PRESSURE_WEIGHTED_DBA,
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
async def async_setup_entry(hass, config_entry, async_add_entities):
|
async def async_setup_entry(hass, config_entry, async_add_entities):
|
||||||
@ -36,10 +79,10 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
"""Discover and add a discovered sensor."""
|
"""Discover and add a discovered sensor."""
|
||||||
client = hass.data[POINT_DOMAIN][config_entry.entry_id]
|
client = hass.data[POINT_DOMAIN][config_entry.entry_id]
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
(
|
[
|
||||||
MinutPointSensor(client, device_id, sensor_type)
|
MinutPointSensor(client, device_id, description)
|
||||||
for sensor_type in SENSOR_TYPES
|
for description in SENSOR_TYPES
|
||||||
),
|
],
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -51,10 +94,14 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
|
|||||||
class MinutPointSensor(MinutPointEntity, SensorEntity):
|
class MinutPointSensor(MinutPointEntity, SensorEntity):
|
||||||
"""The platform class required by Home Assistant."""
|
"""The platform class required by Home Assistant."""
|
||||||
|
|
||||||
def __init__(self, point_client, device_id, device_class):
|
entity_description: MinutPointSensorEntityDescription
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, point_client, device_id, description: MinutPointSensorEntityDescription
|
||||||
|
):
|
||||||
"""Initialize the sensor."""
|
"""Initialize the sensor."""
|
||||||
super().__init__(point_client, device_id, device_class)
|
super().__init__(point_client, device_id, description.device_class)
|
||||||
self._device_prop = SENSOR_TYPES[device_class]
|
self.entity_description = description
|
||||||
|
|
||||||
async def _update_callback(self):
|
async def _update_callback(self):
|
||||||
"""Update the value of the sensor."""
|
"""Update the value of the sensor."""
|
||||||
@ -64,19 +111,9 @@ class MinutPointSensor(MinutPointEntity, SensorEntity):
|
|||||||
self._updated = parse_datetime(self.device.last_update)
|
self._updated = parse_datetime(self.device.last_update)
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
||||||
@property
|
|
||||||
def icon(self):
|
|
||||||
"""Return the icon representation."""
|
|
||||||
return self._device_prop[0]
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def native_value(self):
|
def native_value(self):
|
||||||
"""Return the state of the sensor."""
|
"""Return the state of the sensor."""
|
||||||
if self.value is None:
|
if self.value is None:
|
||||||
return None
|
return None
|
||||||
return round(self.value, self._device_prop[1])
|
return round(self.value, self.entity_description.precision)
|
||||||
|
|
||||||
@property
|
|
||||||
def native_unit_of_measurement(self):
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return self._device_prop[2]
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user