From 40fcc3b75bc74875d5e4d303ead44800ccee3f62 Mon Sep 17 00:00:00 2001 From: Harry Heymann Date: Fri, 4 Jul 2025 10:13:40 -0400 Subject: [PATCH] Rename Matter device conversion methods (#148090) --- .../components/matter/binary_sensor.py | 40 ++++++++-------- homeassistant/components/matter/entity.py | 4 +- homeassistant/components/matter/number.py | 42 ++++++++--------- homeassistant/components/matter/select.py | 34 +++++++------- homeassistant/components/matter/sensor.py | 46 +++++++++---------- homeassistant/components/matter/switch.py | 12 ++--- 6 files changed, 89 insertions(+), 89 deletions(-) diff --git a/homeassistant/components/matter/binary_sensor.py b/homeassistant/components/matter/binary_sensor.py index 95efe46309c..09321bd33b2 100644 --- a/homeassistant/components/matter/binary_sensor.py +++ b/homeassistant/components/matter/binary_sensor.py @@ -54,7 +54,7 @@ class MatterBinarySensor(MatterEntity, BinarySensorEntity): value = self.get_matter_attribute_value(self._entity_info.primary_attribute) if value in (None, NullValue): value = None - elif value_convert := self.entity_description.measurement_to_ha: + elif value_convert := self.entity_description.device_to_ha: value = value_convert(value) if TYPE_CHECKING: value = cast(bool | None, value) @@ -70,7 +70,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="HueMotionSensor", 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, required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), @@ -83,7 +83,7 @@ DISCOVERY_SCHEMAS = [ key="OccupancySensor", device_class=BinarySensorDeviceClass.OCCUPANCY, # 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, required_attributes=(clusters.OccupancySensing.Attributes.Occupancy,), @@ -94,7 +94,7 @@ DISCOVERY_SCHEMAS = [ key="BatteryChargeLevel", device_class=BinarySensorDeviceClass.BATTERY, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: x + device_to_ha=lambda x: x != clusters.PowerSource.Enums.BatChargeLevelEnum.kOk, ), entity_class=MatterBinarySensor, @@ -109,7 +109,7 @@ DISCOVERY_SCHEMAS = [ key="ContactSensor", device_class=BinarySensorDeviceClass.DOOR, # 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, required_attributes=(clusters.BooleanState.Attributes.StateValue,), @@ -153,7 +153,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="LockDoorStateSensor", device_class=BinarySensorDeviceClass.DOOR, - measurement_to_ha={ + device_to_ha={ clusters.DoorLock.Enums.DoorStateEnum.kDoorOpen: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorJammed: True, clusters.DoorLock.Enums.DoorStateEnum.kDoorForcedOpen: True, @@ -168,7 +168,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmDeviceMutedSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.SmokeCoAlarm.Enums.MuteStateEnum.kMuted ), translation_key="muted", @@ -181,7 +181,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmEndfOfServiceSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.SmokeCoAlarm.Enums.EndOfServiceEnum.kExpired ), translation_key="end_of_service", @@ -195,7 +195,7 @@ DISCOVERY_SCHEMAS = [ platform=Platform.BINARY_SENSOR, entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmBatteryAlertSensor", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="battery_alert", @@ -232,7 +232,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmSmokeStateSensor", device_class=BinarySensorDeviceClass.SMOKE, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), ), @@ -244,7 +244,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmInterconnectSmokeAlarmSensor", device_class=BinarySensorDeviceClass.SMOKE, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="interconnected_smoke_alarm", @@ -257,7 +257,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="SmokeCoAlarmInterconnectCOAlarmSensor", device_class=BinarySensorDeviceClass.CO, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x != clusters.SmokeCoAlarm.Enums.AlarmStateEnum.kNormal ), translation_key="interconnected_co_alarm", @@ -271,7 +271,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvseChargingStatusSensor", translation_key="evse_charging_status", device_class=BinarySensorDeviceClass.BATTERY_CHARGING, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: False, @@ -291,7 +291,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvsePlugStateSensor", translation_key="evse_plug_state", device_class=BinarySensorDeviceClass.PLUG, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.StateEnum.kNotPluggedIn: False, clusters.EnergyEvse.Enums.StateEnum.kPluggedInNoDemand: True, clusters.EnergyEvse.Enums.StateEnum.kPluggedInDemand: True, @@ -311,7 +311,7 @@ DISCOVERY_SCHEMAS = [ key="EnergyEvseSupplyStateSensor", translation_key="evse_supply_charging_state", device_class=BinarySensorDeviceClass.RUNNING, - measurement_to_ha={ + device_to_ha={ clusters.EnergyEvse.Enums.SupplyStateEnum.kDisabled: False, clusters.EnergyEvse.Enums.SupplyStateEnum.kChargingEnabled: True, clusters.EnergyEvse.Enums.SupplyStateEnum.kDischargingEnabled: False, @@ -327,7 +327,7 @@ DISCOVERY_SCHEMAS = [ entity_description=MatterBinarySensorEntityDescription( key="WaterHeaterManagementBoostStateSensor", translation_key="boost_state", - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.WaterHeaterManagement.Enums.BoostStateEnum.kActive ), ), @@ -342,7 +342,7 @@ DISCOVERY_SCHEMAS = [ device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, # DeviceFault or SupplyFault bit enabled - measurement_to_ha={ + device_to_ha={ clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kDeviceFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSupplyFault: True, clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kSpeedLow: False, @@ -366,7 +366,7 @@ DISCOVERY_SCHEMAS = [ key="PumpStatusRunning", translation_key="pump_running", device_class=BinarySensorDeviceClass.RUNNING, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.PumpConfigurationAndControl.Bitmaps.PumpStatusBitmap.kRunning ), @@ -384,7 +384,7 @@ DISCOVERY_SCHEMAS = [ translation_key="dishwasher_alarm_inflow", device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kInflowError ), ), @@ -399,7 +399,7 @@ DISCOVERY_SCHEMAS = [ translation_key="dishwasher_alarm_door", device_class=BinarySensorDeviceClass.PROBLEM, entity_category=EntityCategory.DIAGNOSTIC, - measurement_to_ha=lambda x: ( + device_to_ha=lambda x: ( x == clusters.DishwasherAlarm.Bitmaps.AlarmBitmap.kDoorError ), ), diff --git a/homeassistant/components/matter/entity.py b/homeassistant/components/matter/entity.py index fded57d34f5..028feab9c88 100644 --- a/homeassistant/components/matter/entity.py +++ b/homeassistant/components/matter/entity.py @@ -59,8 +59,8 @@ class MatterEntityDescription(EntityDescription): """Describe the Matter entity.""" # convert the value from the primary attribute to the value used by HA - measurement_to_ha: Callable[[Any], Any] | None = None - ha_to_native_value: Callable[[Any], Any] | None = None + device_to_ha: Callable[[Any], Any] | None = None + ha_to_device: Callable[[Any], Any] | None = None command_timeout: int | None = None diff --git a/homeassistant/components/matter/number.py b/homeassistant/components/matter/number.py index 7d138ba5018..c948f39834a 100644 --- a/homeassistant/components/matter/number.py +++ b/homeassistant/components/matter/number.py @@ -55,7 +55,7 @@ class MatterRangeNumberEntityDescription( ): """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 min_attribute: type[ClusterAttributeDescriptor] @@ -74,7 +74,7 @@ class MatterNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Update the current 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) await self.write_attribute( value=sendvalue, @@ -84,7 +84,7 @@ class MatterNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" 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) self._attr_native_value = value @@ -96,7 +96,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """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 await self.send_device_command( self.entity_description.command(send_value), @@ -106,7 +106,7 @@ class MatterRangeNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" 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) self._attr_native_value = value self._attr_native_min_value = ( @@ -133,7 +133,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity): async def async_set_native_value(self, value: float) -> None: """Set level 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) await self.send_device_command( clusters.LevelControl.Commands.MoveToLevel( @@ -145,7 +145,7 @@ class MatterLevelControlNumber(MatterEntity, NumberEntity): def _update_from_device(self) -> None: """Update from device.""" 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) self._attr_native_value = value @@ -162,8 +162,8 @@ DISCOVERY_SCHEMAS = [ native_min_value=0, mode=NumberMode.BOX, # use 255 to indicate that the value should revert to the default - measurement_to_ha=lambda x: 255 if x is None else x, - ha_to_native_value=lambda x: None if x == 255 else int(x), + device_to_ha=lambda x: 255 if x is None else x, + ha_to_device=lambda x: None if x == 255 else int(x), native_step=1, native_unit_of_measurement=None, ), @@ -180,8 +180,8 @@ DISCOVERY_SCHEMAS = [ translation_key="on_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -199,8 +199,8 @@ DISCOVERY_SCHEMAS = [ translation_key="off_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -218,8 +218,8 @@ DISCOVERY_SCHEMAS = [ translation_key="on_off_transition_time", native_max_value=65534, native_min_value=0, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), native_step=0.1, native_unit_of_measurement=UnitOfTime.SECONDS, mode=NumberMode.BOX, @@ -256,8 +256,8 @@ DISCOVERY_SCHEMAS = [ native_min_value=-50, native_step=0.5, native_unit_of_measurement=UnitOfTemperature.CELSIUS, - measurement_to_ha=lambda x: None if x is None else x / 10, - ha_to_native_value=lambda x: round(x * 10), + device_to_ha=lambda x: None if x is None else x / 10, + ha_to_device=lambda x: round(x * 10), mode=NumberMode.BOX, ), entity_class=MatterNumber, @@ -275,10 +275,10 @@ DISCOVERY_SCHEMAS = [ native_max_value=100, native_min_value=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) ), - ha_to_native_value=lambda x: round(x * 2), # HA range 0.5–100.0% + ha_to_device=lambda x: round(x * 2), # HA range 0.5–100.0% mode=NumberMode.SLIDER, ), entity_class=MatterLevelControlNumber, @@ -326,8 +326,8 @@ DISCOVERY_SCHEMAS = [ targetTemperature=value ), native_unit_of_measurement=UnitOfTemperature.CELSIUS, - measurement_to_ha=lambda x: None if x is None else x / 100, - ha_to_native_value=lambda x: round(x * 100), + device_to_ha=lambda x: None if x is None else x / 100, + ha_to_device=lambda x: round(x * 100), min_attribute=clusters.TemperatureControl.Attributes.MinTemperature, max_attribute=clusters.TemperatureControl.Attributes.MaxTemperature, mode=NumberMode.SLIDER, diff --git a/homeassistant/components/matter/select.py b/homeassistant/components/matter/select.py index ac1bc2d1f8f..d700b39258c 100644 --- a/homeassistant/components/matter/select.py +++ b/homeassistant/components/matter/select.py @@ -71,8 +71,8 @@ class MatterSelectEntityDescription(SelectEntityDescription, MatterEntityDescrip class MatterMapSelectEntityDescription(MatterSelectEntityDescription): """Describe Matter select entities for MatterMapSelectEntityDescription.""" - measurement_to_ha: Callable[[int], str | None] - ha_to_native_value: Callable[[str], int | None] + device_to_ha: Callable[[int], str | None] + ha_to_device: Callable[[str], int | None] # list attribute: the attribute descriptor to get the list of values (= list of integers) list_attribute: type[ClusterAttributeDescriptor] @@ -97,7 +97,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity): async def async_select_option(self, option: str) -> None: """Change the selected mode.""" - value_convert = self.entity_description.ha_to_native_value + value_convert = self.entity_description.ha_to_device if TYPE_CHECKING: assert value_convert is not None await self.write_attribute( @@ -109,7 +109,7 @@ class MatterAttributeSelectEntity(MatterEntity, SelectEntity): """Update from device.""" value: Nullable | int | None 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: assert value_convert is not None self._attr_current_option = value_convert(value) @@ -132,7 +132,7 @@ class MatterMapSelectEntity(MatterAttributeSelectEntity): self._attr_options = [ mapped_value 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 super()._update_from_device() @@ -333,13 +333,13 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="startup_on_off", options=["on", "off", "toggle", "previous"], - measurement_to_ha={ + device_to_ha={ 0: "off", 1: "on", 2: "toggle", None: "previous", }.get, - ha_to_native_value={ + ha_to_device={ "off": 0, "on": 1, "toggle": 2, @@ -358,12 +358,12 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="sensitivity_level", options=["high", "standard", "low"], - measurement_to_ha={ + device_to_ha={ 0: "high", 1: "standard", 2: "low", }.get, - ha_to_native_value={ + ha_to_device={ "high": 0, "standard": 1, "low": 2, @@ -379,11 +379,11 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="temperature_display_mode", options=["Celsius", "Fahrenheit"], - measurement_to_ha={ + device_to_ha={ 0: "Celsius", 1: "Fahrenheit", }.get, - ha_to_native_value={ + ha_to_device={ "Celsius": 0, "Fahrenheit": 1, }.get, @@ -432,8 +432,8 @@ DISCOVERY_SCHEMAS = [ key="MatterLaundryWasherNumberOfRinses", translation_key="laundry_washer_number_of_rinses", list_attribute=clusters.LaundryWasherControls.Attributes.SupportedRinses, - measurement_to_ha=NUMBER_OF_RINSES_STATE_MAP.get, - ha_to_native_value=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get, + device_to_ha=NUMBER_OF_RINSES_STATE_MAP.get, + ha_to_device=NUMBER_OF_RINSES_STATE_MAP_REVERSE.get, ), entity_class=MatterMapSelectEntity, required_attributes=( @@ -450,13 +450,13 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.CONFIG, translation_key="door_lock_sound_volume", options=["silent", "low", "medium", "high"], - measurement_to_ha={ + device_to_ha={ 0: "silent", 1: "low", 3: "medium", 2: "high", }.get, - ha_to_native_value={ + ha_to_device={ "silent": 0, "low": 1, "medium": 3, @@ -472,8 +472,8 @@ DISCOVERY_SCHEMAS = [ key="PumpConfigurationAndControlOperationMode", translation_key="pump_operation_mode", options=list(PUMP_OPERATION_MODE_MAP.values()), - measurement_to_ha=PUMP_OPERATION_MODE_MAP.get, - ha_to_native_value=PUMP_OPERATION_MODE_MAP_REVERSE.get, + device_to_ha=PUMP_OPERATION_MODE_MAP.get, + ha_to_device=PUMP_OPERATION_MODE_MAP_REVERSE.get, ), entity_class=MatterAttributeSelectEntity, required_attributes=( diff --git a/homeassistant/components/matter/sensor.py b/homeassistant/components/matter/sensor.py index f744ec8885a..62c70f777e7 100644 --- a/homeassistant/components/matter/sensor.py +++ b/homeassistant/components/matter/sensor.py @@ -194,7 +194,7 @@ class MatterSensor(MatterEntity, SensorEntity): value = self.get_matter_attribute_value(self._entity_info.primary_attribute) if value in (None, NullValue): value = None - elif value_convert := self.entity_description.measurement_to_ha: + elif value_convert := self.entity_description.device_to_ha: value = value_convert(value) self._attr_native_value = value @@ -296,7 +296,7 @@ DISCOVERY_SCHEMAS = [ key="TemperatureSensor", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -308,7 +308,7 @@ DISCOVERY_SCHEMAS = [ key="PressureSensor", native_unit_of_measurement=UnitOfPressure.KPA, device_class=SensorDeviceClass.PRESSURE, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -320,7 +320,7 @@ DISCOVERY_SCHEMAS = [ key="FlowSensor", native_unit_of_measurement=UnitOfVolumeFlowRate.CUBIC_METERS_PER_HOUR, translation_key="flow", - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -332,7 +332,7 @@ DISCOVERY_SCHEMAS = [ key="HumiditySensor", native_unit_of_measurement=PERCENTAGE, device_class=SensorDeviceClass.HUMIDITY, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -346,7 +346,7 @@ DISCOVERY_SCHEMAS = [ key="LightSensor", native_unit_of_measurement=LIGHT_LUX, 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, ), entity_class=MatterSensor, @@ -360,7 +360,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.BATTERY, entity_category=EntityCategory.DIAGNOSTIC, # value has double precision - measurement_to_ha=lambda x: int(x / 2), + device_to_ha=lambda x: int(x / 2), state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -402,7 +402,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, 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, required_attributes=(clusters.PowerSource.Attributes.BatChargeState,), @@ -589,7 +589,7 @@ DISCOVERY_SCHEMAS = [ state_class=None, # convert to set first to remove the duplicate unknown value 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, required_attributes=(clusters.AirQuality.Attributes.AirQuality,), @@ -668,7 +668,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=2, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 1000, + device_to_ha=lambda x: x / 1000, ), entity_class=MatterSensor, required_attributes=( @@ -685,7 +685,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfEnergy.WATT_HOUR, suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, - measurement_to_ha=lambda x: x / 1000, + device_to_ha=lambda x: x / 1000, ), entity_class=MatterSensor, required_attributes=( @@ -702,7 +702,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfPower.WATT, suggested_display_precision=2, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Watt,), @@ -731,7 +731,7 @@ DISCOVERY_SCHEMAS = [ native_unit_of_measurement=UnitOfElectricPotential.VOLT, suggested_display_precision=0, state_class=SensorStateClass.MEASUREMENT, - measurement_to_ha=lambda x: x / 10, + device_to_ha=lambda x: x / 10, ), entity_class=MatterSensor, required_attributes=(NeoCluster.Attributes.Voltage,), @@ -823,7 +823,7 @@ DISCOVERY_SCHEMAS = [ suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, # 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, required_attributes=( @@ -842,7 +842,7 @@ DISCOVERY_SCHEMAS = [ suggested_display_precision=3, state_class=SensorStateClass.TOTAL_INCREASING, # 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, required_attributes=( @@ -910,7 +910,7 @@ DISCOVERY_SCHEMAS = [ translation_key="contamination_state", device_class=SensorDeviceClass.ENUM, options=list(CONTAMINATION_STATE_MAP.values()), - measurement_to_ha=CONTAMINATION_STATE_MAP.get, + device_to_ha=CONTAMINATION_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.SmokeCoAlarm.Attributes.ContaminationState,), @@ -922,7 +922,7 @@ DISCOVERY_SCHEMAS = [ translation_key="expiry_date", device_class=SensorDeviceClass.TIMESTAMP, # raw value is epoch seconds - measurement_to_ha=datetime.fromtimestamp, + device_to_ha=datetime.fromtimestamp, ), entity_class=MatterSensor, required_attributes=(clusters.SmokeCoAlarm.Attributes.ExpiryDate,), @@ -993,7 +993,7 @@ DISCOVERY_SCHEMAS = [ key="ThermostatLocalTemperature", native_unit_of_measurement=UnitOfTemperature.CELSIUS, device_class=SensorDeviceClass.TEMPERATURE, - measurement_to_ha=lambda x: x / 100, + device_to_ha=lambda x: x / 100, state_class=SensorStateClass.MEASUREMENT, ), entity_class=MatterSensor, @@ -1044,7 +1044,7 @@ DISCOVERY_SCHEMAS = [ entity_category=EntityCategory.DIAGNOSTIC, entity_registry_enabled_default=False, 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, ), entity_class=MatterSensor, @@ -1060,7 +1060,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, 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, required_attributes=(clusters.EnergyEvse.Attributes.FaultState,), @@ -1173,7 +1173,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, options=list(ESA_STATE_MAP.values()), - measurement_to_ha=ESA_STATE_MAP.get, + device_to_ha=ESA_STATE_MAP.get, ), entity_class=MatterSensor, required_attributes=(clusters.DeviceEnergyManagement.Attributes.ESAState,), @@ -1186,7 +1186,7 @@ DISCOVERY_SCHEMAS = [ device_class=SensorDeviceClass.ENUM, entity_category=EntityCategory.DIAGNOSTIC, 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, required_attributes=(clusters.DeviceEnergyManagement.Attributes.OptOutState,), @@ -1200,7 +1200,7 @@ DISCOVERY_SCHEMAS = [ options=[ 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, required_attributes=( diff --git a/homeassistant/components/matter/switch.py b/homeassistant/components/matter/switch.py index 870a9098492..df8581c5c4f 100644 --- a/homeassistant/components/matter/switch.py +++ b/homeassistant/components/matter/switch.py @@ -95,7 +95,7 @@ class MatterGenericCommandSwitch(MatterSwitch): def _update_from_device(self) -> None: """Update from device.""" 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) self._attr_is_on = value @@ -141,7 +141,7 @@ class MatterNumericSwitch(MatterSwitch): async def _async_set_native_value(self, value: bool) -> None: """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) await self.write_attribute( value=send_value, @@ -159,7 +159,7 @@ class MatterNumericSwitch(MatterSwitch): def _update_from_device(self) -> None: """Update from device.""" 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) self._attr_is_on = value @@ -248,11 +248,11 @@ DISCOVERY_SCHEMAS = [ key="EveTrvChildLock", entity_category=EntityCategory.CONFIG, translation_key="child_lock", - measurement_to_ha={ + device_to_ha={ 0: False, 1: True, }.get, - ha_to_native_value={ + ha_to_device={ False: 0, True: 1, }.get, @@ -275,7 +275,7 @@ DISCOVERY_SCHEMAS = [ ), off_command=clusters.EnergyEvse.Commands.Disable, command_timeout=3000, - measurement_to_ha=EVSE_SUPPLY_STATE_MAP.get, + device_to_ha=EVSE_SUPPLY_STATE_MAP.get, ), entity_class=MatterGenericCommandSwitch, required_attributes=(