From 4e376181f5efa4bc15446897dd0510122252fede Mon Sep 17 00:00:00 2001 From: rforro Date: Sun, 23 Jan 2022 15:37:01 +0100 Subject: [PATCH] Expose entity device temperature from cluster in ZHA (#64189) * expose device temperature sensor in frontend * fixes after runnig test * specified device temp channel * add dev temp int test device aqara water * black fix --- .../components/zha/core/channels/general.py | 9 +++++++++ homeassistant/components/zha/core/const.py | 1 + homeassistant/components/zha/sensor.py | 13 ++++++++++++ tests/components/zha/test_channels.py | 2 ++ tests/components/zha/test_sensor.py | 14 +++++++++++++ tests/components/zha/zha_devices_list.py | 20 ++++++++++++++++++- 6 files changed, 58 insertions(+), 1 deletion(-) diff --git a/homeassistant/components/zha/core/channels/general.py b/homeassistant/components/zha/core/channels/general.py index d0216436ba2..89d750465b8 100644 --- a/homeassistant/components/zha/core/channels/general.py +++ b/homeassistant/components/zha/core/channels/general.py @@ -18,6 +18,8 @@ from ..const import ( REPORT_CONFIG_BATTERY_SAVE, REPORT_CONFIG_DEFAULT, REPORT_CONFIG_IMMEDIATE, + REPORT_CONFIG_MAX_INT, + REPORT_CONFIG_MIN_INT, SIGNAL_ATTR_UPDATED, SIGNAL_MOVE_LEVEL, SIGNAL_SET_LEVEL, @@ -170,6 +172,13 @@ class Commissioning(ZigbeeChannel): class DeviceTemperature(ZigbeeChannel): """Device Temperature channel.""" + REPORT_CONFIG = [ + { + "attr": "current_temperature", + "config": (REPORT_CONFIG_MIN_INT, REPORT_CONFIG_MAX_INT, 50), + } + ] + @registries.ZIGBEE_CHANNEL_REGISTRY.register(general.GreenPowerProxy.cluster_id) class GreenPowerProxy(ZigbeeChannel): diff --git a/homeassistant/components/zha/core/const.py b/homeassistant/components/zha/core/const.py index 79d61f809b1..9216b6dd600 100644 --- a/homeassistant/components/zha/core/const.py +++ b/homeassistant/components/zha/core/const.py @@ -70,6 +70,7 @@ CHANNEL_ATTRIBUTE = "attribute" CHANNEL_BASIC = "basic" CHANNEL_COLOR = "light_color" CHANNEL_COVER = "window_covering" +CHANNEL_DEVICE_TEMPERATURE = "device_temperature" CHANNEL_DOORLOCK = "door_lock" CHANNEL_ELECTRICAL_MEASUREMENT = "electrical_measurement" CHANNEL_EVENT_RELAY = "event_relay" diff --git a/homeassistant/components/zha/sensor.py b/homeassistant/components/zha/sensor.py index 3f46196d1c7..eb79634695f 100644 --- a/homeassistant/components/zha/sensor.py +++ b/homeassistant/components/zha/sensor.py @@ -52,6 +52,7 @@ from .core import discovery from .core.const import ( CHANNEL_ANALOG_INPUT, CHANNEL_BASIC, + CHANNEL_DEVICE_TEMPERATURE, CHANNEL_ELECTRICAL_MEASUREMENT, CHANNEL_HUMIDITY, CHANNEL_ILLUMINANCE, @@ -496,6 +497,18 @@ class Temperature(Sensor): _unit = TEMP_CELSIUS +@MULTI_MATCH(channel_names=CHANNEL_DEVICE_TEMPERATURE) +class DeviceTemperature(Sensor): + """Device Temperature Sensor.""" + + SENSOR_ATTR = "current_temperature" + _attr_device_class: SensorDeviceClass = SensorDeviceClass.TEMPERATURE + _attr_state_class: SensorStateClass = SensorStateClass.MEASUREMENT + _divisor = 100 + _unit = TEMP_CELSIUS + _attr_entity_category = EntityCategory.DIAGNOSTIC + + @MULTI_MATCH(channel_names="carbon_dioxide_concentration") class CarbonDioxideConcentration(Sensor): """Carbon Dioxide Concentration sensor.""" diff --git a/tests/components/zha/test_channels.py b/tests/components/zha/test_channels.py index 9005fd49d8f..8eafdc451cc 100644 --- a/tests/components/zha/test_channels.py +++ b/tests/components/zha/test_channels.py @@ -101,6 +101,7 @@ async def poll_control_device(zha_device_restored, zigpy_device_mock): [ (0x0000, 0, {}), (0x0001, 1, {"battery_voltage", "battery_percentage_remaining"}), + (0x0002, 1, {"current_temperature"}), (0x0003, 0, {}), (0x0004, 0, {}), (0x0005, 1, {}), @@ -203,6 +204,7 @@ async def test_in_channel_config( [ (0x0000, 0), (0x0001, 1), + (0x0002, 1), (0x0003, 0), (0x0004, 0), (0x0005, 1), diff --git a/tests/components/zha/test_sensor.py b/tests/components/zha/test_sensor.py index 3483077d7d8..c4e66e98098 100644 --- a/tests/components/zha/test_sensor.py +++ b/tests/components/zha/test_sensor.py @@ -241,6 +241,12 @@ async def async_test_powerconfiguration(hass, cluster, entity_id): assert hass.states.get(entity_id).attributes["battery_voltage"] == 2.0 +async def async_test_device_temperature(hass, cluster, entity_id): + """Test temperature sensor.""" + await send_attributes_report(hass, cluster, {0: 2900}) + assert_state(hass, entity_id, "29.0", TEMP_CELSIUS) + + @pytest.mark.parametrize( "cluster_id, entity_suffix, test_func, report_count, read_plug, unsupported_attrs", ( @@ -350,6 +356,14 @@ async def async_test_powerconfiguration(hass, cluster, entity_id): }, None, ), + ( + general.DeviceTemperature.cluster_id, + "device_temperature", + async_test_device_temperature, + 1, + None, + None, + ), ), ) async def test_sensor( diff --git a/tests/components/zha/zha_devices_list.py b/tests/components/zha/zha_devices_list.py index 9dd5c8f1de7..873c213527d 100644 --- a/tests/components/zha/zha_devices_list.py +++ b/tests/components/zha/zha_devices_list.py @@ -2237,6 +2237,7 @@ DEVICES = [ "switch.lumi_lumi_plug_maus01_77665544_on_off", "sensor.lumi_lumi_plug_maus01_77665544_basic_rssi", "sensor.lumi_lumi_plug_maus01_77665544_basic_lqi", + "sensor.lumi_lumi_plug_maus01_77665544_device_temperature", ], DEV_SIG_ENT_MAP: { ("switch", "00:11:22:33:44:55:66:77-1"): { @@ -2244,6 +2245,11 @@ DEVICES = [ DEV_SIG_ENT_MAP_CLASS: "Switch", DEV_SIG_ENT_MAP_ID: "switch.lumi_lumi_plug_maus01_77665544_on_off", }, + ("sensor", "00:11:22:33:44:55:66:77-1-2"): { + DEV_SIG_CHANNELS: ["device_temperature"], + DEV_SIG_ENT_MAP_CLASS: "DeviceTemperature", + DEV_SIG_ENT_MAP_ID: "sensor.lumi_lumi_plug_maus01_77665544_device_temperature", + }, ("button", "00:11:22:33:44:55:66:77-1-3"): { DEV_SIG_CHANNELS: ["identify"], DEV_SIG_ENT_MAP_CLASS: "ZHAIdentifyButton", @@ -2328,6 +2334,7 @@ DEVICES = [ "sensor.lumi_lumi_relay_c2acn01_77665544_electrical_measurement_rms_voltage", "sensor.lumi_lumi_relay_c2acn01_77665544_basic_rssi", "sensor.lumi_lumi_relay_c2acn01_77665544_basic_lqi", + "sensor.lumi_lumi_relay_c2acn01_77665544_device_temperature", ], DEV_SIG_ENT_MAP: { ("light", "00:11:22:33:44:55:66:77-1"): { @@ -2335,6 +2342,11 @@ DEVICES = [ DEV_SIG_ENT_MAP_CLASS: "Light", DEV_SIG_ENT_MAP_ID: "light.lumi_lumi_relay_c2acn01_77665544_on_off", }, + ("sensor", "00:11:22:33:44:55:66:77-1-2"): { + DEV_SIG_CHANNELS: ["device_temperature"], + DEV_SIG_ENT_MAP_CLASS: "DeviceTemperature", + DEV_SIG_ENT_MAP_ID: "sensor.lumi_lumi_relay_c2acn01_77665544_device_temperature", + }, ("button", "00:11:22:33:44:55:66:77-1-3"): { DEV_SIG_CHANNELS: ["identify"], DEV_SIG_ENT_MAP_CLASS: "ZHAIdentifyButton", @@ -3453,7 +3465,7 @@ DEVICES = [ 1: { SIG_EP_TYPE: 1026, DEV_SIG_EP_ID: 1, - SIG_EP_INPUT: [0, 1, 3, 1280], + SIG_EP_INPUT: [0, 1, 2, 3, 1280], SIG_EP_OUTPUT: [25], SIG_EP_PROFILE: 260, }, @@ -3465,6 +3477,7 @@ DEVICES = [ "binary_sensor.lumi_lumi_sensor_wleak_aq1_77665544_ias_zone", "sensor.lumi_lumi_sensor_wleak_aq1_77665544_basic_rssi", "sensor.lumi_lumi_sensor_wleak_aq1_77665544_basic_lqi", + "sensor.lumi_lumi_sensor_wleak_aq1_77665544_device_temperature", ], DEV_SIG_ENT_MAP: { ("binary_sensor", "00:11:22:33:44:55:66:77-1-1280"): { @@ -3472,6 +3485,11 @@ DEVICES = [ DEV_SIG_ENT_MAP_CLASS: "IASZone", DEV_SIG_ENT_MAP_ID: "binary_sensor.lumi_lumi_sensor_wleak_aq1_77665544_ias_zone", }, + ("sensor", "00:11:22:33:44:55:66:77-1-2"): { + DEV_SIG_CHANNELS: ["device_temperature"], + DEV_SIG_ENT_MAP_CLASS: "DeviceTemperature", + DEV_SIG_ENT_MAP_ID: "sensor.lumi_lumi_sensor_wleak_aq1_77665544_device_temperature", + }, ("button", "00:11:22:33:44:55:66:77-1-3"): { DEV_SIG_CHANNELS: ["identify"], DEV_SIG_ENT_MAP_CLASS: "ZHAIdentifyButton",