Rename Matter device conversion methods (#148090)

This commit is contained in:
Harry Heymann 2025-07-04 10:13:40 -04:00 committed by GitHub
parent 510fd09163
commit 40fcc3b75b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 89 additions and 89 deletions

View File

@ -54,7 +54,7 @@ class MatterBinarySensor(MatterEntity, BinarySensorEntity):
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value in (None, NullValue): if value in (None, NullValue):
value = None value = None
elif value_convert := self.entity_description.measurement_to_ha: elif value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
if TYPE_CHECKING: if TYPE_CHECKING:
value = cast(bool | None, value) value = cast(bool | None, value)
@ -70,7 +70,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="HueMotionSensor", key="HueMotionSensor",
device_class=BinarySensorDeviceClass.MOTION, device_class=BinarySensorDeviceClass.MOTION,
measurement_to_ha=lambda x: (x & 1 == 1) if x is not None else None, device_to_ha=lambda x: (x & 1 == 1) if x is not None else None,
), ),
entity_class=MatterBinarySensor, entity_class=MatterBinarySensor,
required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,),
@ -83,7 +83,7 @@ DISCOVERY_SCHEMAS = [
key="OccupancySensor", key="OccupancySensor",
device_class=BinarySensorDeviceClass.OCCUPANCY, device_class=BinarySensorDeviceClass.OCCUPANCY,
# The first bit = if occupied # The first bit = if occupied
measurement_to_ha=lambda x: (x & 1 == 1) if x is not None else None, device_to_ha=lambda x: (x & 1 == 1) if x is not None else None,
), ),
entity_class=MatterBinarySensor, entity_class=MatterBinarySensor,
required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,),
@ -94,7 +94,7 @@ DISCOVERY_SCHEMAS = [
key="BatteryChargeLevel", key="BatteryChargeLevel",
device_class=BinarySensorDeviceClass.BATTERY, device_class=BinarySensorDeviceClass.BATTERY,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
measurement_to_ha=lambda x: x device_to_ha=lambda x: x
!= clusters.PowerSource.Enums.BatChargeLevelEnum.kOk, != clusters.PowerSource.Enums.BatChargeLevelEnum.kOk,
), ),
entity_class=MatterBinarySensor, entity_class=MatterBinarySensor,
@ -109,7 +109,7 @@ DISCOVERY_SCHEMAS = [
key="ContactSensor", key="ContactSensor",
device_class=BinarySensorDeviceClass.DOOR, device_class=BinarySensorDeviceClass.DOOR,
# value is inverted on matter to what we expect # value is inverted on matter to what we expect
measurement_to_ha=lambda x: not x, device_to_ha=lambda x: not x,
), ),
entity_class=MatterBinarySensor, entity_class=MatterBinarySensor,
required_attributes=(clusters.BooleanState.Attributes.StateValue,), required_attributes=(clusters.BooleanState.Attributes.StateValue,),
@ -153,7 +153,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="LockDoorStateSensor", key="LockDoorStateSensor",
device_class=BinarySensorDeviceClass.DOOR, device_class=BinarySensorDeviceClass.DOOR,
measurement_to_ha={ device_to_ha={
clusters.DoorLock.Enums.DoorStateEnum.kDoorOpen: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorOpen: True,
clusters.DoorLock.Enums.DoorStateEnum.kDoorJammed: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorJammed: True,
clusters.DoorLock.Enums.DoorStateEnum.kDoorForcedOpen: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorForcedOpen: True,
@ -168,7 +168,7 @@ DISCOVERY_SCHEMAS = [
platform=Platform.BINARY_SENSOR, platform=Platform.BINARY_SENSOR,
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmDeviceMutedSensor", key="SmokeCoAlarmDeviceMutedSensor",
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x == clusters.SmokeCoAlarm.Enums.MuteStateEnum.kMuted x == clusters.SmokeCoAlarm.Enums.MuteStateEnum.kMuted
), ),
translation_key="muted", translation_key="muted",
@ -181,7 +181,7 @@ DISCOVERY_SCHEMAS = [
platform=Platform.BINARY_SENSOR, platform=Platform.BINARY_SENSOR,
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmEndfOfServiceSensor", key="SmokeCoAlarmEndfOfServiceSensor",
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x == clusters.SmokeCoAlarm.Enums.EndOfServiceEnum.kExpired x == clusters.SmokeCoAlarm.Enums.EndOfServiceEnum.kExpired
), ),
translation_key="end_of_service", translation_key="end_of_service",
@ -195,7 +195,7 @@ DISCOVERY_SCHEMAS = [
platform=Platform.BINARY_SENSOR, platform=Platform.BINARY_SENSOR,
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmBatteryAlertSensor", key="SmokeCoAlarmBatteryAlertSensor",
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal
), ),
translation_key="battery_alert", translation_key="battery_alert",
@ -232,7 +232,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmSmokeStateSensor", key="SmokeCoAlarmSmokeStateSensor",
device_class=BinarySensorDeviceClass.SMOKE, device_class=BinarySensorDeviceClass.SMOKE,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal
), ),
), ),
@ -244,7 +244,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmInterconnectSmokeAlarmSensor", key="SmokeCoAlarmInterconnectSmokeAlarmSensor",
device_class=BinarySensorDeviceClass.SMOKE, device_class=BinarySensorDeviceClass.SMOKE,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal
), ),
translation_key="interconnected_smoke_alarm", translation_key="interconnected_smoke_alarm",
@ -257,7 +257,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="SmokeCoAlarmInterconnectCOAlarmSensor", key="SmokeCoAlarmInterconnectCOAlarmSensor",
device_class=BinarySensorDeviceClass.CO, device_class=BinarySensorDeviceClass.CO,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal
), ),
translation_key="interconnected_co_alarm", translation_key="interconnected_co_alarm",
@ -271,7 +271,7 @@ DISCOVERY_SCHEMAS = [
key="EnergyEvseChargingStatusSensor", key="EnergyEvseChargingStatusSensor",
translation_key="evse_charging_status", translation_key="evse_charging_status",
device_class=BinarySensorDeviceClass.BATTERY_CHARGING, device_class=BinarySensorDeviceClass.BATTERY_CHARGING,
measurement_to_ha={ device_to_ha={
clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False,
clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: False,
clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: False,
@ -291,7 +291,7 @@ DISCOVERY_SCHEMAS = [
key="EnergyEvsePlugStateSensor", key="EnergyEvsePlugStateSensor",
translation_key="evse_plug_state", translation_key="evse_plug_state",
device_class=BinarySensorDeviceClass.PLUG, device_class=BinarySensorDeviceClass.PLUG,
measurement_to_ha={ device_to_ha={
clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False,
clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: True, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: True,
clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: True, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: True,
@ -311,7 +311,7 @@ DISCOVERY_SCHEMAS = [
key="EnergyEvseSupplyStateSensor", key="EnergyEvseSupplyStateSensor",
translation_key="evse_supply_charging_state", translation_key="evse_supply_charging_state",
device_class=BinarySensorDeviceClass.RUNNING, device_class=BinarySensorDeviceClass.RUNNING,
measurement_to_ha={ device_to_ha={
clusters.EnergyEvse.Enums.SupplyStateEnum.kDisabled: False, clusters.EnergyEvse.Enums.SupplyStateEnum.kDisabled: False,
clusters.EnergyEvse.Enums.SupplyStateEnum.kChargingEnabled: True, clusters.EnergyEvse.Enums.SupplyStateEnum.kChargingEnabled: True,
clusters.EnergyEvse.Enums.SupplyStateEnum.kDischargingEnabled: False, clusters.EnergyEvse.Enums.SupplyStateEnum.kDischargingEnabled: False,
@ -327,7 +327,7 @@ DISCOVERY_SCHEMAS = [
entity_description=MatterBinarySensorEntityDescription( entity_description=MatterBinarySensorEntityDescription(
key="WaterHeaterManagementBoostStateSensor", key="WaterHeaterManagementBoostStateSensor",
translation_key="boost_state", translation_key="boost_state",
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x == clusters.WaterHeaterManagement.Enums.BoostStateEnum.kActive x == clusters.WaterHeaterManagement.Enums.BoostStateEnum.kActive
), ),
), ),
@ -342,7 +342,7 @@ DISCOVERY_SCHEMAS = [
device_class=BinarySensorDeviceClass.PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
# DeviceFault or SupplyFault bit enabled # DeviceFault or SupplyFault bit enabled
measurement_to_ha={ device_to_ha={
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault: True,
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault: True,
clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedLow: False, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedLow: False,
@ -366,7 +366,7 @@ DISCOVERY_SCHEMAS = [
key="PumpStatusRunning", key="PumpStatusRunning",
translation_key="pump_running", translation_key="pump_running",
device_class=BinarySensorDeviceClass.RUNNING, device_class=BinarySensorDeviceClass.RUNNING,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x x
== clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning == clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning
), ),
@ -384,7 +384,7 @@ DISCOVERY_SCHEMAS = [
translation_key="dishwasher_alarm_inflow", translation_key="dishwasher_alarm_inflow",
device_class=BinarySensorDeviceClass.PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError
), ),
), ),
@ -399,7 +399,7 @@ DISCOVERY_SCHEMAS = [
translation_key="dishwasher_alarm_door", translation_key="dishwasher_alarm_door",
device_class=BinarySensorDeviceClass.PROBLEM, device_class=BinarySensorDeviceClass.PROBLEM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
measurement_to_ha=lambda x: ( device_to_ha=lambda x: (
x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError
), ),
), ),

View File

@ -59,8 +59,8 @@ class MatterEntityDescription(EntityDescription):
"""Describe the Matter entity.""" """Describe the Matter entity."""
# convert the value from the primary attribute to the value used by HA # convert the value from the primary attribute to the value used by HA
measurement_to_ha: Callable[[Any], Any] | None = None device_to_ha: Callable[[Any], Any] | None = None
ha_to_native_value: Callable[[Any], Any] | None = None ha_to_device: Callable[[Any], Any] | None = None
command_timeout: int | None = None command_timeout: int | None = None

View File

@ -55,7 +55,7 @@ class MatterRangeNumberEntityDescription(
): ):
"""Describe Matter Number Input entities with min and max values.""" """Describe Matter Number Input entities with min and max values."""
ha_to_native_value: Callable[[Any], Any] ha_to_device: Callable[[Any], Any]
# attribute descriptors to get the min and max value # attribute descriptors to get the min and max value
min_attribute: type[ClusterAttributeDescriptor] min_attribute: type[ClusterAttributeDescriptor]
@ -74,7 +74,7 @@ class MatterNumber(MatterEntity, NumberEntity):
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
sendvalue = int(value) sendvalue = int(value)
if value_convert := self.entity_description.ha_to_native_value: if value_convert := self.entity_description.ha_to_device:
sendvalue = value_convert(value) sendvalue = value_convert(value)
await self.write_attribute( await self.write_attribute(
value=sendvalue, value=sendvalue,
@ -84,7 +84,7 @@ class MatterNumber(MatterEntity, NumberEntity):
def _update_from_device(self) -> None: def _update_from_device(self) -> None:
"""Update from device.""" """Update from device."""
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value_convert := self.entity_description.measurement_to_ha: if value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_native_value = value self._attr_native_value = value
@ -96,7 +96,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity):
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Update the current value.""" """Update the current value."""
send_value = self.entity_description.ha_to_native_value(value) send_value = self.entity_description.ha_to_device(value)
# custom command defined to set the new value # custom command defined to set the new value
await self.send_device_command( await self.send_device_command(
self.entity_description.command(send_value), self.entity_description.command(send_value),
@ -106,7 +106,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity):
def _update_from_device(self) -> None: def _update_from_device(self) -> None:
"""Update from device.""" """Update from device."""
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value_convert := self.entity_description.measurement_to_ha: if value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_native_value = value self._attr_native_value = value
self._attr_native_min_value = ( self._attr_native_min_value = (
@ -133,7 +133,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity):
async def async_set_native_value(self, value: float) -> None: async def async_set_native_value(self, value: float) -> None:
"""Set level value.""" """Set level value."""
send_value = int(value) send_value = int(value)
if value_convert := self.entity_description.ha_to_native_value: if value_convert := self.entity_description.ha_to_device:
send_value = value_convert(value) send_value = value_convert(value)
await self.send_device_command( await self.send_device_command(
clusters.LevelControl.Commands.MoveToLevel( clusters.LevelControl.Commands.MoveToLevel(
@ -145,7 +145,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity):
def _update_from_device(self) -> None: def _update_from_device(self) -> None:
"""Update from device.""" """Update from device."""
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value_convert := self.entity_description.measurement_to_ha: if value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_native_value = value self._attr_native_value = value
@ -162,8 +162,8 @@ DISCOVERY_SCHEMAS = [
native_min_value=0, native_min_value=0,
mode=NumberMode.BOX, mode=NumberMode.BOX,
# use 255 to indicate that the value should revert to the default # use 255 to indicate that the value should revert to the default
measurement_to_ha=lambda x: 255 if x is None else x, device_to_ha=lambda x: 255 if x is None else x,
ha_to_native_value=lambda x: None if x == 255 else int(x), ha_to_device=lambda x: None if x == 255 else int(x),
native_step=1, native_step=1,
native_unit_of_measurement=None, native_unit_of_measurement=None,
), ),
@ -180,8 +180,8 @@ DISCOVERY_SCHEMAS = [
translation_key="on_transition_time", translation_key="on_transition_time",
native_max_value=65534, native_max_value=65534,
native_min_value=0, native_min_value=0,
measurement_to_ha=lambda x: None if x is None else x / 10, device_to_ha=lambda x: None if x is None else x / 10,
ha_to_native_value=lambda x: round(x * 10), ha_to_device=lambda x: round(x * 10),
native_step=0.1, native_step=0.1,
native_unit_of_measurement=UnitOfTime.SECONDS, native_unit_of_measurement=UnitOfTime.SECONDS,
mode=NumberMode.BOX, mode=NumberMode.BOX,
@ -199,8 +199,8 @@ DISCOVERY_SCHEMAS = [
translation_key="off_transition_time", translation_key="off_transition_time",
native_max_value=65534, native_max_value=65534,
native_min_value=0, native_min_value=0,
measurement_to_ha=lambda x: None if x is None else x / 10, device_to_ha=lambda x: None if x is None else x / 10,
ha_to_native_value=lambda x: round(x * 10), ha_to_device=lambda x: round(x * 10),
native_step=0.1, native_step=0.1,
native_unit_of_measurement=UnitOfTime.SECONDS, native_unit_of_measurement=UnitOfTime.SECONDS,
mode=NumberMode.BOX, mode=NumberMode.BOX,
@ -218,8 +218,8 @@ DISCOVERY_SCHEMAS = [
translation_key="on_off_transition_time", translation_key="on_off_transition_time",
native_max_value=65534, native_max_value=65534,
native_min_value=0, native_min_value=0,
measurement_to_ha=lambda x: None if x is None else x / 10, device_to_ha=lambda x: None if x is None else x / 10,
ha_to_native_value=lambda x: round(x * 10), ha_to_device=lambda x: round(x * 10),
native_step=0.1, native_step=0.1,
native_unit_of_measurement=UnitOfTime.SECONDS, native_unit_of_measurement=UnitOfTime.SECONDS,
mode=NumberMode.BOX, mode=NumberMode.BOX,
@ -256,8 +256,8 @@ DISCOVERY_SCHEMAS = [
native_min_value=-50, native_min_value=-50,
native_step=0.5, native_step=0.5,
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
measurement_to_ha=lambda x: None if x is None else x / 10, device_to_ha=lambda x: None if x is None else x / 10,
ha_to_native_value=lambda x: round(x * 10), ha_to_device=lambda x: round(x * 10),
mode=NumberMode.BOX, mode=NumberMode.BOX,
), ),
entity_class=MatterNumber, entity_class=MatterNumber,
@ -275,10 +275,10 @@ DISCOVERY_SCHEMAS = [
native_max_value=100, native_max_value=100,
native_min_value=0.5, native_min_value=0.5,
native_step=0.5, native_step=0.5,
measurement_to_ha=( device_to_ha=(
lambda x: None if x is None else x / 2 # Matter range (1-200) lambda x: None if x is None else x / 2 # Matter range (1-200)
), ),
ha_to_native_value=lambda x: round(x * 2), # HA range 0.5100.0% ha_to_device=lambda x: round(x * 2), # HA range 0.5100.0%
mode=NumberMode.SLIDER, mode=NumberMode.SLIDER,
), ),
entity_class=MatterLevelControlNumber, entity_class=MatterLevelControlNumber,
@ -326,8 +326,8 @@ DISCOVERY_SCHEMAS = [
targetTemperature=value targetTemperature=value
), ),
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
measurement_to_ha=lambda x: None if x is None else x / 100, device_to_ha=lambda x: None if x is None else x / 100,
ha_to_native_value=lambda x: round(x * 100), ha_to_device=lambda x: round(x * 100),
min_attribute=clusters.TemperatureControl.Attributes.MinTemperature, min_attribute=clusters.TemperatureControl.Attributes.MinTemperature,
max_attribute=clusters.TemperatureControl.Attributes.MaxTemperature, max_attribute=clusters.TemperatureControl.Attributes.MaxTemperature,
mode=NumberMode.SLIDER, mode=NumberMode.SLIDER,

View File

@ -71,8 +71,8 @@ class MatterSelectEntityDescription(SelectEntityDescription, MatterEntityDescrip
class MatterMapSelectEntityDescription(MatterSelectEntityDescription): class MatterMapSelectEntityDescription(MatterSelectEntityDescription):
"""Describe Matter select entities for MatterMapSelectEntityDescription.""" """Describe Matter select entities for MatterMapSelectEntityDescription."""
measurement_to_ha: Callable[[int], str | None] device_to_ha: Callable[[int], str | None]
ha_to_native_value: Callable[[str], int | None] ha_to_device: Callable[[str], int | None]
# list attribute: the attribute descriptor to get the list of values (= list of integers) # list attribute: the attribute descriptor to get the list of values (= list of integers)
list_attribute: type[ClusterAttributeDescriptor] list_attribute: type[ClusterAttributeDescriptor]
@ -97,7 +97,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity):
async def async_select_option(self, option: str) -> None: async def async_select_option(self, option: str) -> None:
"""Change the selected mode.""" """Change the selected mode."""
value_convert = self.entity_description.ha_to_native_value value_convert = self.entity_description.ha_to_device
if TYPE_CHECKING: if TYPE_CHECKING:
assert value_convert is not None assert value_convert is not None
await self.write_attribute( await self.write_attribute(
@ -109,7 +109,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity):
"""Update from device.""" """Update from device."""
value: Nullable | int | None value: Nullable | int | None
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
value_convert = self.entity_description.measurement_to_ha value_convert = self.entity_description.device_to_ha
if TYPE_CHECKING: if TYPE_CHECKING:
assert value_convert is not None assert value_convert is not None
self._attr_current_option = value_convert(value) self._attr_current_option = value_convert(value)
@ -132,7 +132,7 @@ class MatterMapSelectEntity(MatterAttributeSelectEntity):
self._attr_options = [ self._attr_options = [
mapped_value mapped_value
for value in available_values for value in available_values
if (mapped_value := self.entity_description.measurement_to_ha(value)) if (mapped_value := self.entity_description.device_to_ha(value))
] ]
# use base implementation from MatterAttributeSelectEntity to set the current option # use base implementation from MatterAttributeSelectEntity to set the current option
super()._update_from_device() super()._update_from_device()
@ -333,13 +333,13 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
translation_key="startup_on_off", translation_key="startup_on_off",
options=["on", "off", "toggle", "previous"], options=["on", "off", "toggle", "previous"],
measurement_to_ha={ device_to_ha={
0: "off", 0: "off",
1: "on", 1: "on",
2: "toggle", 2: "toggle",
None: "previous", None: "previous",
}.get, }.get,
ha_to_native_value={ ha_to_device={
"off": 0, "off": 0,
"on": 1, "on": 1,
"toggle": 2, "toggle": 2,
@ -358,12 +358,12 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
translation_key="sensitivity_level", translation_key="sensitivity_level",
options=["high", "standard", "low"], options=["high", "standard", "low"],
measurement_to_ha={ device_to_ha={
0: "high", 0: "high",
1: "standard", 1: "standard",
2: "low", 2: "low",
}.get, }.get,
ha_to_native_value={ ha_to_device={
"high": 0, "high": 0,
"standard": 1, "standard": 1,
"low": 2, "low": 2,
@ -379,11 +379,11 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
translation_key="temperature_display_mode", translation_key="temperature_display_mode",
options=["Celsius", "Fahrenheit"], options=["Celsius", "Fahrenheit"],
measurement_to_ha={ device_to_ha={
0: "Celsius", 0: "Celsius",
1: "Fahrenheit", 1: "Fahrenheit",
}.get, }.get,
ha_to_native_value={ ha_to_device={
"Celsius": 0, "Celsius": 0,
"Fahrenheit": 1, "Fahrenheit": 1,
}.get, }.get,
@ -432,8 +432,8 @@ DISCOVERY_SCHEMAS = [
key="MatterLaundryWasherNumberOfRinses", key="MatterLaundryWasherNumberOfRinses",
translation_key="laundry_washer_number_of_rinses", translation_key="laundry_washer_number_of_rinses",
list_attribute=clusters.LaundryWasherControls.Attributes.SupportedRinses, list_attribute=clusters.LaundryWasherControls.Attributes.SupportedRinses,
measurement_to_ha=NUMBER_OF_RINSES_STATE_MAP.get, device_to_ha=NUMBER_OF_RINSES_STATE_MAP.get,
ha_to_native_value=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get, ha_to_device=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get,
), ),
entity_class=MatterMapSelectEntity, entity_class=MatterMapSelectEntity,
required_attributes=( required_attributes=(
@ -450,13 +450,13 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
translation_key="door_lock_sound_volume", translation_key="door_lock_sound_volume",
options=["silent", "low", "medium", "high"], options=["silent", "low", "medium", "high"],
measurement_to_ha={ device_to_ha={
0: "silent", 0: "silent",
1: "low", 1: "low",
3: "medium", 3: "medium",
2: "high", 2: "high",
}.get, }.get,
ha_to_native_value={ ha_to_device={
"silent": 0, "silent": 0,
"low": 1, "low": 1,
"medium": 3, "medium": 3,
@ -472,8 +472,8 @@ DISCOVERY_SCHEMAS = [
key="PumpConfigurationAndControlOperationMode", key="PumpConfigurationAndControlOperationMode",
translation_key="pump_operation_mode", translation_key="pump_operation_mode",
options=list(PUMP_OPERATION_MODE_MAP.values()), options=list(PUMP_OPERATION_MODE_MAP.values()),
measurement_to_ha=PUMP_OPERATION_MODE_MAP.get, device_to_ha=PUMP_OPERATION_MODE_MAP.get,
ha_to_native_value=PUMP_OPERATION_MODE_MAP_REVERSE.get, ha_to_device=PUMP_OPERATION_MODE_MAP_REVERSE.get,
), ),
entity_class=MatterAttributeSelectEntity, entity_class=MatterAttributeSelectEntity,
required_attributes=( required_attributes=(

View File

@ -194,7 +194,7 @@ class MatterSensor(MatterEntity, SensorEntity):
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value in (None, NullValue): if value in (None, NullValue):
value = None value = None
elif value_convert := self.entity_description.measurement_to_ha: elif value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_native_value = value self._attr_native_value = value
@ -296,7 +296,7 @@ DISCOVERY_SCHEMAS = [
key="TemperatureSensor", key="TemperatureSensor",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
measurement_to_ha=lambda x: x / 100, device_to_ha=lambda x: x / 100,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -308,7 +308,7 @@ DISCOVERY_SCHEMAS = [
key="PressureSensor", key="PressureSensor",
native_unit_of_measurement=UnitOfPressure.KPA, native_unit_of_measurement=UnitOfPressure.KPA,
device_class=SensorDeviceClass.PRESSURE, device_class=SensorDeviceClass.PRESSURE,
measurement_to_ha=lambda x: x / 10, device_to_ha=lambda x: x / 10,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -320,7 +320,7 @@ DISCOVERY_SCHEMAS = [
key="FlowSensor", key="FlowSensor",
native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR, native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR,
translation_key="flow", translation_key="flow",
measurement_to_ha=lambda x: x / 10, device_to_ha=lambda x: x / 10,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -332,7 +332,7 @@ DISCOVERY_SCHEMAS = [
key="HumiditySensor", key="HumiditySensor",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
device_class=SensorDeviceClass.HUMIDITY, device_class=SensorDeviceClass.HUMIDITY,
measurement_to_ha=lambda x: x / 100, device_to_ha=lambda x: x / 100,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -346,7 +346,7 @@ DISCOVERY_SCHEMAS = [
key="LightSensor", key="LightSensor",
native_unit_of_measurement=LIGHT_LUX, native_unit_of_measurement=LIGHT_LUX,
device_class=SensorDeviceClass.ILLUMINANCE, device_class=SensorDeviceClass.ILLUMINANCE,
measurement_to_ha=lambda x: round(pow(10, ((x - 1) / 10000)), 1), device_to_ha=lambda x: round(pow(10, ((x - 1) / 10000)), 1),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -360,7 +360,7 @@ DISCOVERY_SCHEMAS = [
device_class=SensorDeviceClass.BATTERY, device_class=SensorDeviceClass.BATTERY,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
# value has double precision # value has double precision
measurement_to_ha=lambda x: int(x / 2), device_to_ha=lambda x: int(x / 2),
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -402,7 +402,7 @@ DISCOVERY_SCHEMAS = [
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
options=[state for state in CHARGE_STATE_MAP.values() if state is not None], options=[state for state in CHARGE_STATE_MAP.values() if state is not None],
measurement_to_ha=CHARGE_STATE_MAP.get, device_to_ha=CHARGE_STATE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.PowerSource.Attributes.BatChargeState,), required_attributes=(clusters.PowerSource.Attributes.BatChargeState,),
@ -589,7 +589,7 @@ DISCOVERY_SCHEMAS = [
state_class=None, state_class=None,
# convert to set first to remove the duplicate unknown value # convert to set first to remove the duplicate unknown value
options=[x for x in AIR_QUALITY_MAP.values() if x is not None], options=[x for x in AIR_QUALITY_MAP.values() if x is not None],
measurement_to_ha=lambda x: AIR_QUALITY_MAP[x], device_to_ha=lambda x: AIR_QUALITY_MAP[x],
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.AirQuality.Attributes.AirQuality,), required_attributes=(clusters.AirQuality.Attributes.AirQuality,),
@ -668,7 +668,7 @@ DISCOVERY_SCHEMAS = [
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
suggested_display_precision=2, suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 1000, device_to_ha=lambda x: x / 1000,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(
@ -685,7 +685,7 @@ DISCOVERY_SCHEMAS = [
native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, native_unit_of_measurement=UnitOfEnergy.WATT_HOUR,
suggested_display_precision=3, suggested_display_precision=3,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
measurement_to_ha=lambda x: x / 1000, device_to_ha=lambda x: x / 1000,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(
@ -702,7 +702,7 @@ DISCOVERY_SCHEMAS = [
native_unit_of_measurement=UnitOfPower.WATT, native_unit_of_measurement=UnitOfPower.WATT,
suggested_display_precision=2, suggested_display_precision=2,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 10, device_to_ha=lambda x: x / 10,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Watt,), required_attributes=(NeoCluster.Attributes.Watt,),
@ -731,7 +731,7 @@ DISCOVERY_SCHEMAS = [
native_unit_of_measurement=UnitOfElectricPotential.VOLT, native_unit_of_measurement=UnitOfElectricPotential.VOLT,
suggested_display_precision=0, suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
measurement_to_ha=lambda x: x / 10, device_to_ha=lambda x: x / 10,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(NeoCluster.Attributes.Voltage,), required_attributes=(NeoCluster.Attributes.Voltage,),
@ -823,7 +823,7 @@ DISCOVERY_SCHEMAS = [
suggested_display_precision=3, suggested_display_precision=3,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
# id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh) # id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh)
measurement_to_ha=lambda x: x.energy, device_to_ha=lambda x: x.energy,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(
@ -842,7 +842,7 @@ DISCOVERY_SCHEMAS = [
suggested_display_precision=3, suggested_display_precision=3,
state_class=SensorStateClass.TOTAL_INCREASING, state_class=SensorStateClass.TOTAL_INCREASING,
# id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh) # id 0 of the EnergyMeasurementStruct is the cumulative energy (in mWh)
measurement_to_ha=lambda x: x.energy, device_to_ha=lambda x: x.energy,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(
@ -910,7 +910,7 @@ DISCOVERY_SCHEMAS = [
translation_key="contamination_state", translation_key="contamination_state",
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
options=list(CONTAMINATION_STATE_MAP.values()), options=list(CONTAMINATION_STATE_MAP.values()),
measurement_to_ha=CONTAMINATION_STATE_MAP.get, device_to_ha=CONTAMINATION_STATE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.SmokeCoAlarm.Attributes.ContaminationState,), required_attributes=(clusters.SmokeCoAlarm.Attributes.ContaminationState,),
@ -922,7 +922,7 @@ DISCOVERY_SCHEMAS = [
translation_key="expiry_date", translation_key="expiry_date",
device_class=SensorDeviceClass.TIMESTAMP, device_class=SensorDeviceClass.TIMESTAMP,
# raw value is epoch seconds # raw value is epoch seconds
measurement_to_ha=datetime.fromtimestamp, device_to_ha=datetime.fromtimestamp,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.SmokeCoAlarm.Attributes.ExpiryDate,), required_attributes=(clusters.SmokeCoAlarm.Attributes.ExpiryDate,),
@ -993,7 +993,7 @@ DISCOVERY_SCHEMAS = [
key="ThermostatLocalTemperature", key="ThermostatLocalTemperature",
native_unit_of_measurement=UnitOfTemperature.CELSIUS, native_unit_of_measurement=UnitOfTemperature.CELSIUS,
device_class=SensorDeviceClass.TEMPERATURE, device_class=SensorDeviceClass.TEMPERATURE,
measurement_to_ha=lambda x: x / 100, device_to_ha=lambda x: x / 100,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -1044,7 +1044,7 @@ DISCOVERY_SCHEMAS = [
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
entity_registry_enabled_default=False, entity_registry_enabled_default=False,
translation_key="window_covering_target_position", translation_key="window_covering_target_position",
measurement_to_ha=lambda x: round((10000 - x) / 100), device_to_ha=lambda x: round((10000 - x) / 100),
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
@ -1060,7 +1060,7 @@ DISCOVERY_SCHEMAS = [
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
options=list(EVSE_FAULT_STATE_MAP.values()), options=list(EVSE_FAULT_STATE_MAP.values()),
measurement_to_ha=EVSE_FAULT_STATE_MAP.get, device_to_ha=EVSE_FAULT_STATE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.EnergyEvse.Attributes.FaultState,), required_attributes=(clusters.EnergyEvse.Attributes.FaultState,),
@ -1173,7 +1173,7 @@ DISCOVERY_SCHEMAS = [
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
options=list(ESA_STATE_MAP.values()), options=list(ESA_STATE_MAP.values()),
measurement_to_ha=ESA_STATE_MAP.get, device_to_ha=ESA_STATE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.DeviceEnergyManagement.Attributes.ESAState,), required_attributes=(clusters.DeviceEnergyManagement.Attributes.ESAState,),
@ -1186,7 +1186,7 @@ DISCOVERY_SCHEMAS = [
device_class=SensorDeviceClass.ENUM, device_class=SensorDeviceClass.ENUM,
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
options=list(DEM_OPT_OUT_STATE_MAP.values()), options=list(DEM_OPT_OUT_STATE_MAP.values()),
measurement_to_ha=DEM_OPT_OUT_STATE_MAP.get, device_to_ha=DEM_OPT_OUT_STATE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=(clusters.DeviceEnergyManagement.Attributes.OptOutState,), required_attributes=(clusters.DeviceEnergyManagement.Attributes.OptOutState,),
@ -1200,7 +1200,7 @@ DISCOVERY_SCHEMAS = [
options=[ options=[
mode for mode in PUMP_CONTROL_MODE_MAP.values() if mode is not None mode for mode in PUMP_CONTROL_MODE_MAP.values() if mode is not None
], ],
measurement_to_ha=PUMP_CONTROL_MODE_MAP.get, device_to_ha=PUMP_CONTROL_MODE_MAP.get,
), ),
entity_class=MatterSensor, entity_class=MatterSensor,
required_attributes=( required_attributes=(

View File

@ -95,7 +95,7 @@ class MatterGenericCommandSwitch(MatterSwitch):
def _update_from_device(self) -> None: def _update_from_device(self) -> None:
"""Update from device.""" """Update from device."""
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value_convert := self.entity_description.measurement_to_ha: if value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_is_on = value self._attr_is_on = value
@ -141,7 +141,7 @@ class MatterNumericSwitch(MatterSwitch):
async def _async_set_native_value(self, value: bool) -> None: async def _async_set_native_value(self, value: bool) -> None:
"""Update the current value.""" """Update the current value."""
if value_convert := self.entity_description.ha_to_native_value: if value_convert := self.entity_description.ha_to_device:
send_value = value_convert(value) send_value = value_convert(value)
await self.write_attribute( await self.write_attribute(
value=send_value, value=send_value,
@ -159,7 +159,7 @@ class MatterNumericSwitch(MatterSwitch):
def _update_from_device(self) -> None: def _update_from_device(self) -> None:
"""Update from device.""" """Update from device."""
value = self.get_matter_attribute_value(self._entity_info.primary_attribute) value = self.get_matter_attribute_value(self._entity_info.primary_attribute)
if value_convert := self.entity_description.measurement_to_ha: if value_convert := self.entity_description.device_to_ha:
value = value_convert(value) value = value_convert(value)
self._attr_is_on = value self._attr_is_on = value
@ -248,11 +248,11 @@ DISCOVERY_SCHEMAS = [
key="EveTrvChildLock", key="EveTrvChildLock",
entity_category=EntityCategory.CONFIG, entity_category=EntityCategory.CONFIG,
translation_key="child_lock", translation_key="child_lock",
measurement_to_ha={ device_to_ha={
0: False, 0: False,
1: True, 1: True,
}.get, }.get,
ha_to_native_value={ ha_to_device={
False: 0, False: 0,
True: 1, True: 1,
}.get, }.get,
@ -275,7 +275,7 @@ DISCOVERY_SCHEMAS = [
), ),
off_command=clusters.EnergyEvse.Commands.Disable, off_command=clusters.EnergyEvse.Commands.Disable,
command_timeout=3000, command_timeout=3000,
measurement_to_ha=EVSE_SUPPLY_STATE_MAP.get, device_to_ha=EVSE_SUPPLY_STATE_MAP.get,
), ),
entity_class=MatterGenericCommandSwitch, entity_class=MatterGenericCommandSwitch,
required_attributes=( required_attributes=(