mirror of
https://github.com/home-assistant/core.git
synced 2025-07-15 17:27:10 +00:00
Refactor kaiterra to use SensorEntityDescription (#61865)
Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
parent
7bee0e6423
commit
6a81821399
@ -1,20 +1,25 @@
|
||||
"""Support for Kaiterra Temperature ahn Humidity Sensors."""
|
||||
from homeassistant.components.sensor import SensorEntity
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE_ID,
|
||||
CONF_NAME,
|
||||
DEVICE_CLASS_HUMIDITY,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
TEMP_CELSIUS,
|
||||
TEMP_FAHRENHEIT,
|
||||
from homeassistant.components.sensor import (
|
||||
SensorDeviceClass,
|
||||
SensorEntity,
|
||||
SensorEntityDescription,
|
||||
)
|
||||
from homeassistant.const import CONF_DEVICE_ID, CONF_NAME, TEMP_CELSIUS, TEMP_FAHRENHEIT
|
||||
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
||||
|
||||
from .const import DISPATCHER_KAITERRA, DOMAIN
|
||||
|
||||
SENSORS = [
|
||||
{"name": "Temperature", "prop": "rtemp", "device_class": DEVICE_CLASS_TEMPERATURE},
|
||||
{"name": "Humidity", "prop": "rhumid", "device_class": DEVICE_CLASS_HUMIDITY},
|
||||
SensorEntityDescription(
|
||||
name="Temperature",
|
||||
key="rtemp",
|
||||
device_class=SensorDeviceClass.TEMPERATURE,
|
||||
),
|
||||
SensorEntityDescription(
|
||||
name="Humidity",
|
||||
key="rhumid",
|
||||
device_class=SensorDeviceClass.HUMIDITY,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
@ -28,57 +33,40 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
||||
device_id = discovery_info[CONF_DEVICE_ID]
|
||||
|
||||
async_add_entities(
|
||||
[KaiterraSensor(api, name, device_id, sensor) for sensor in SENSORS]
|
||||
[KaiterraSensor(api, name, device_id, description) for description in SENSORS]
|
||||
)
|
||||
|
||||
|
||||
class KaiterraSensor(SensorEntity):
|
||||
"""Implementation of a Kaittera sensor."""
|
||||
|
||||
def __init__(self, api, name, device_id, sensor):
|
||||
_attr_should_poll = False
|
||||
|
||||
def __init__(self, api, name, device_id, description: SensorEntityDescription):
|
||||
"""Initialize the sensor."""
|
||||
self._api = api
|
||||
self._name = f'{name} {sensor["name"]}'
|
||||
self._device_id = device_id
|
||||
self._kind = sensor["name"].lower()
|
||||
self._property = sensor["prop"]
|
||||
self._device_class = sensor["device_class"]
|
||||
self.entity_description = description
|
||||
self._attr_name = f"{name} {description.name}"
|
||||
self._attr_unique_id = f"{device_id}_{description.name.lower()}"
|
||||
|
||||
@property
|
||||
def _sensor(self):
|
||||
"""Return the sensor data."""
|
||||
return self._api.data.get(self._device_id, {}).get(self._property, {})
|
||||
|
||||
@property
|
||||
def should_poll(self):
|
||||
"""Return that the sensor should not be polled."""
|
||||
return False
|
||||
return self._api.data.get(self._device_id, {}).get(
|
||||
self.entity_description.key, {}
|
||||
)
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return the availability of the sensor."""
|
||||
return self._api.data.get(self._device_id) is not None
|
||||
|
||||
@property
|
||||
def device_class(self):
|
||||
"""Return the device class."""
|
||||
return self._device_class
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def native_value(self):
|
||||
"""Return the state."""
|
||||
return self._sensor.get("value")
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return the sensor's unique id."""
|
||||
return f"{self._device_id}_{self._kind}"
|
||||
|
||||
@property
|
||||
def native_unit_of_measurement(self):
|
||||
"""Return the unit the value is expressed in."""
|
||||
|
Loading…
x
Reference in New Issue
Block a user