2022-12-06 20:39:00 +00:00
|
|
|
"""Matter sensors."""
|
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-03-27 20:21:56 +00:00
|
|
|
from dataclasses import dataclass
|
|
|
|
|
2022-12-06 20:39:00 +00:00
|
|
|
from chip.clusters import Objects as clusters
|
|
|
|
from chip.clusters.Types import Nullable, NullValue
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
SensorStateClass,
|
|
|
|
)
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import (
|
|
|
|
LIGHT_LUX,
|
|
|
|
PERCENTAGE,
|
|
|
|
Platform,
|
|
|
|
UnitOfPressure,
|
|
|
|
UnitOfTemperature,
|
2022-12-21 10:00:24 +00:00
|
|
|
UnitOfVolumeFlowRate,
|
2022-12-06 20:39:00 +00:00
|
|
|
)
|
|
|
|
from homeassistant.core import HomeAssistant, callback
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
|
|
|
|
2023-03-27 20:21:56 +00:00
|
|
|
from .entity import MatterEntity, MatterEntityDescription
|
2022-12-20 12:06:24 +00:00
|
|
|
from .helpers import get_matter
|
2023-02-23 19:58:37 +00:00
|
|
|
from .models import MatterDiscoverySchema
|
2022-12-06 20:39:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
|
|
|
"""Set up Matter sensors from Config Entry."""
|
2022-12-20 12:06:24 +00:00
|
|
|
matter = get_matter(hass)
|
2022-12-06 20:39:00 +00:00
|
|
|
matter.register_platform_handler(Platform.SENSOR, async_add_entities)
|
|
|
|
|
|
|
|
|
2023-03-27 20:21:56 +00:00
|
|
|
@dataclass
|
|
|
|
class MatterSensorEntityDescription(SensorEntityDescription, MatterEntityDescription):
|
|
|
|
"""Describe Matter sensor entities."""
|
|
|
|
|
|
|
|
|
2022-12-06 20:39:00 +00:00
|
|
|
class MatterSensor(MatterEntity, SensorEntity):
|
|
|
|
"""Representation of a Matter sensor."""
|
|
|
|
|
|
|
|
_attr_state_class = SensorStateClass.MEASUREMENT
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description: MatterSensorEntityDescription
|
2022-12-06 20:39:00 +00:00
|
|
|
|
|
|
|
@callback
|
|
|
|
def _update_from_device(self) -> None:
|
|
|
|
"""Update from device."""
|
2023-02-23 19:58:37 +00:00
|
|
|
value: Nullable | float | None
|
|
|
|
value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
|
|
|
|
if value in (None, NullValue):
|
|
|
|
value = None
|
2023-03-27 20:21:56 +00:00
|
|
|
elif value_convert := self.entity_description.measurement_to_ha:
|
2023-02-23 19:58:37 +00:00
|
|
|
value = value_convert(value)
|
|
|
|
self._attr_native_value = value
|
|
|
|
|
|
|
|
|
|
|
|
# Discovery schema(s) to map Matter Attributes to HA entities
|
|
|
|
DISCOVERY_SCHEMAS = [
|
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="TemperatureSensor",
|
|
|
|
name="Temperature",
|
|
|
|
native_unit_of_measurement=UnitOfTemperature.CELSIUS,
|
|
|
|
device_class=SensorDeviceClass.TEMPERATURE,
|
2023-03-27 20:21:56 +00:00
|
|
|
measurement_to_ha=lambda x: x / 100,
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(clusters.TemperatureMeasurement.Attributes.MeasuredValue,),
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="PressureSensor",
|
|
|
|
name="Pressure",
|
|
|
|
native_unit_of_measurement=UnitOfPressure.KPA,
|
|
|
|
device_class=SensorDeviceClass.PRESSURE,
|
2023-03-27 20:21:56 +00:00
|
|
|
measurement_to_ha=lambda x: x / 10,
|
2023-02-23 19:58:37 +00:00
|
|
|
),
|
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(clusters.PressureMeasurement.Attributes.MeasuredValue,),
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="FlowSensor",
|
|
|
|
name="Flow",
|
|
|
|
native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
|
|
|
|
device_class=SensorDeviceClass.WATER, # what is the device class here ?
|
2023-03-27 20:21:56 +00:00
|
|
|
measurement_to_ha=lambda x: x / 10,
|
2023-02-23 19:58:37 +00:00
|
|
|
),
|
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(clusters.FlowMeasurement.Attributes.MeasuredValue,),
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="HumiditySensor",
|
|
|
|
name="Humidity",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
device_class=SensorDeviceClass.HUMIDITY,
|
2023-03-27 20:21:56 +00:00
|
|
|
measurement_to_ha=lambda x: x / 100,
|
2023-02-23 19:58:37 +00:00
|
|
|
),
|
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(
|
2022-12-06 20:39:00 +00:00
|
|
|
clusters.RelativeHumidityMeasurement.Attributes.MeasuredValue,
|
|
|
|
),
|
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="LightSensor",
|
|
|
|
name="Illuminance",
|
|
|
|
native_unit_of_measurement=LIGHT_LUX,
|
|
|
|
device_class=SensorDeviceClass.ILLUMINANCE,
|
2023-03-27 20:21:56 +00:00
|
|
|
measurement_to_ha=lambda x: round(pow(10, ((x - 1) / 10000)), 1),
|
2023-02-23 19:58:37 +00:00
|
|
|
),
|
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(clusters.IlluminanceMeasurement.Attributes.MeasuredValue,),
|
|
|
|
),
|
|
|
|
MatterDiscoverySchema(
|
|
|
|
platform=Platform.SENSOR,
|
2023-03-27 20:21:56 +00:00
|
|
|
entity_description=MatterSensorEntityDescription(
|
2023-02-23 19:58:37 +00:00
|
|
|
key="PowerSource",
|
|
|
|
name="Battery",
|
|
|
|
native_unit_of_measurement=PERCENTAGE,
|
|
|
|
device_class=SensorDeviceClass.BATTERY,
|
2023-03-27 20:21:56 +00:00
|
|
|
# value has double precision
|
|
|
|
measurement_to_ha=lambda x: int(x / 2),
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
entity_class=MatterSensor,
|
|
|
|
required_attributes=(clusters.PowerSource.Attributes.BatPercentRemaining,),
|
2022-12-06 20:39:00 +00:00
|
|
|
),
|
2023-02-23 19:58:37 +00:00
|
|
|
]
|