Expose mcu detected temperature & signal (#82905)

pull/82946/head
Matrix 2022-11-30 01:41:01 +08:00 committed by GitHub
parent c092f2be04
commit 4ab6f97341
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 39 additions and 5 deletions

View File

@ -13,8 +13,13 @@ from homeassistant.components.sensor import (
SensorStateClass,
)
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.helpers.entity import EntityCategory
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.util import percentage
@ -72,6 +77,12 @@ BATTERY_POWER_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:
"""Convert battery to percentage."""
@ -103,11 +114,30 @@ SENSOR_TYPES: tuple[YoLinkSensorEntityDescription, ...] = (
YoLinkSensorEntityDescription(
key="temperature",
device_class=SensorDeviceClass.TEMPERATURE,
native_unit_of_measurement=TEMP_CELSIUS,
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
name="Temperature",
state_class=SensorStateClass.MEASUREMENT,
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
def update_entity_state(self, state: dict) -> None:
"""Update HA Entity State."""
self._attr_native_value = self.entity_description.value(
state.get(self.entity_description.key)
)
if (
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()