Expose mcu detected temperature & signal (#82905)
parent
c092f2be04
commit
4ab6f97341
|
@ -13,8 +13,13 @@ from homeassistant.components.sensor import (
|
||||||
SensorStateClass,
|
SensorStateClass,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import PERCENTAGE, TEMP_CELSIUS
|
from homeassistant.const import (
|
||||||
|
PERCENTAGE,
|
||||||
|
SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
|
UnitOfTemperature,
|
||||||
|
)
|
||||||
from homeassistant.core import HomeAssistant, callback
|
from homeassistant.core import HomeAssistant, callback
|
||||||
|
from homeassistant.helpers.entity import EntityCategory
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.util import percentage
|
from homeassistant.util import percentage
|
||||||
|
|
||||||
|
@ -72,6 +77,12 @@ BATTERY_POWER_SENSOR = [
|
||||||
ATTR_DEVICE_CO_SMOKE_SENSOR,
|
ATTR_DEVICE_CO_SMOKE_SENSOR,
|
||||||
]
|
]
|
||||||
|
|
||||||
|
MCU_DEV_TEMPERATURE_SENSOR = [
|
||||||
|
ATTR_DEVICE_LEAK_SENSOR,
|
||||||
|
ATTR_DEVICE_MOTION_SENSOR,
|
||||||
|
ATTR_DEVICE_CO_SMOKE_SENSOR,
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
def cvt_battery(val: int | None) -> int | None:
|
def cvt_battery(val: int | None) -> int | None:
|
||||||
"""Convert battery to percentage."""
|
"""Convert battery to percentage."""
|
||||||
|
@ -103,11 +114,30 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
|
||||||
YoLinkSensorEntityDescription(
|
YoLinkSensorEntityDescription(
|
||||||
key="temperature",
|
key="temperature",
|
||||||
device_class=SensorDeviceClass.TEMPERATURE,
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
native_unit_of_measurement=TEMP_CELSIUS,
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
name="Temperature",
|
name="Temperature",
|
||||||
state_class=SensorStateClass.MEASUREMENT,
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_TH_SENSOR],
|
exists_fn=lambda device: device.device_type in [ATTR_DEVICE_TH_SENSOR],
|
||||||
),
|
),
|
||||||
|
# mcu temperature
|
||||||
|
YoLinkSensorEntityDescription(
|
||||||
|
key="devTemperature",
|
||||||
|
device_class=SensorDeviceClass.TEMPERATURE,
|
||||||
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
||||||
|
name="Temperature",
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
exists_fn=lambda device: device.device_type in MCU_DEV_TEMPERATURE_SENSOR,
|
||||||
|
),
|
||||||
|
YoLinkSensorEntityDescription(
|
||||||
|
key="loraInfo",
|
||||||
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
||||||
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS_MILLIWATT,
|
||||||
|
name="Signal",
|
||||||
|
value=lambda value: value["signal"] if value is not None else None,
|
||||||
|
state_class=SensorStateClass.MEASUREMENT,
|
||||||
|
entity_category=EntityCategory.DIAGNOSTIC,
|
||||||
|
entity_registry_enabled_default=False,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,7 +191,11 @@ class YoLinkSensorEntity(YoLinkEntity, SensorEntity):
|
||||||
@callback
|
@callback
|
||||||
def update_entity_state(self, state: dict) -> None:
|
def update_entity_state(self, state: dict) -> None:
|
||||||
"""Update HA Entity State."""
|
"""Update HA Entity State."""
|
||||||
self._attr_native_value = self.entity_description.value(
|
if (
|
||||||
state.get(self.entity_description.key)
|
attr_val := self.entity_description.value(
|
||||||
)
|
state.get(self.entity_description.key)
|
||||||
|
)
|
||||||
|
) is None and self.entity_description.key in ["devTemperature", "loraInfo"]:
|
||||||
|
return
|
||||||
|
self._attr_native_value = attr_val
|
||||||
self.async_write_ha_state()
|
self.async_write_ha_state()
|
||||||
|
|
Loading…
Reference in New Issue