From 046a2d83fd51cde5d644c928c693fb4a28504c3e Mon Sep 17 00:00:00 2001 From: John Carr Date: Tue, 25 Feb 2020 12:34:30 +0000 Subject: [PATCH 1/2] Add some missing device class attributes to homekit_controller sensors --- .../components/homekit_controller/sensor.py | 18 ++++++++++++++++++ .../homekit_controller/test_sensor.py | 15 +++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/homeassistant/components/homekit_controller/sensor.py b/homeassistant/components/homekit_controller/sensor.py index a71ff7e4ac24f..ab8a6fa667246 100644 --- a/homeassistant/components/homekit_controller/sensor.py +++ b/homeassistant/components/homekit_controller/sensor.py @@ -4,6 +4,9 @@ from homeassistant.const import ( CONCENTRATION_PARTS_PER_MILLION, DEVICE_CLASS_BATTERY, + DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_TEMPERATURE, TEMP_CELSIUS, ) from homeassistant.core import callback @@ -31,6 +34,11 @@ def get_characteristic_types(self): """Define the homekit characteristics the entity is tracking.""" return [CharacteristicsTypes.RELATIVE_HUMIDITY_CURRENT] + @property + def device_class(self) -> str: + """Return the device class of the sensor.""" + return DEVICE_CLASS_HUMIDITY + @property def name(self): """Return the name of the device.""" @@ -67,6 +75,11 @@ def get_characteristic_types(self): """Define the homekit characteristics the entity is tracking.""" return [CharacteristicsTypes.TEMPERATURE_CURRENT] + @property + def device_class(self) -> str: + """Return the device class of the sensor.""" + return DEVICE_CLASS_TEMPERATURE + @property def name(self): """Return the name of the device.""" @@ -103,6 +116,11 @@ def get_characteristic_types(self): """Define the homekit characteristics the entity is tracking.""" return [CharacteristicsTypes.LIGHT_LEVEL_CURRENT] + @property + def device_class(self) -> str: + """Return the device class of the sensor.""" + return DEVICE_CLASS_ILLUMINANCE + @property def name(self): """Return the name of the device.""" diff --git a/tests/components/homekit_controller/test_sensor.py b/tests/components/homekit_controller/test_sensor.py index 5b1c5e1ac85ac..8b0528ea46d48 100644 --- a/tests/components/homekit_controller/test_sensor.py +++ b/tests/components/homekit_controller/test_sensor.py @@ -2,6 +2,13 @@ from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.services import ServicesTypes +from homeassistant.const import ( + DEVICE_CLASS_BATTERY, + DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_TEMPERATURE, +) + from tests.components.homekit_controller.common import setup_test_component TEMPERATURE = ("temperature", "temperature.current") @@ -75,6 +82,8 @@ async def test_temperature_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "20" + assert state.attributes["device_class"] == DEVICE_CLASS_TEMPERATURE + async def test_humidity_sensor_read_state(hass, utcnow): """Test reading the state of a HomeKit humidity sensor accessory.""" @@ -90,6 +99,8 @@ async def test_humidity_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "20" + assert state.attributes["device_class"] == DEVICE_CLASS_HUMIDITY + async def test_light_level_sensor_read_state(hass, utcnow): """Test reading the state of a HomeKit temperature sensor accessory.""" @@ -105,6 +116,8 @@ async def test_light_level_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "20" + assert state.attributes["device_class"] == DEVICE_CLASS_ILLUMINANCE + async def test_carbon_dioxide_level_sensor_read_state(hass, utcnow): """Test reading the state of a HomeKit carbon dioxide sensor accessory.""" @@ -137,6 +150,8 @@ async def test_battery_level_sensor(hass, utcnow): assert state.state == "20" assert state.attributes["icon"] == "mdi:battery-20" + assert state.attributes["device_class"] == DEVICE_CLASS_BATTERY + async def test_battery_charging(hass, utcnow): """Test reading the state of a HomeKit battery's charging state.""" From be1241747433f5ec6f831f216a2c6f1da26ce013 Mon Sep 17 00:00:00 2001 From: John Carr Date: Tue, 25 Feb 2020 13:14:33 +0000 Subject: [PATCH 2/2] Add classes for binary sensors --- .../components/homekit_controller/binary_sensor.py | 9 ++++++++- .../homekit_controller/test_binary_sensor.py | 12 +++++++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/homeassistant/components/homekit_controller/binary_sensor.py b/homeassistant/components/homekit_controller/binary_sensor.py index 467a956767666..39e37fab68efe 100644 --- a/homeassistant/components/homekit_controller/binary_sensor.py +++ b/homeassistant/components/homekit_controller/binary_sensor.py @@ -4,6 +4,8 @@ from aiohomekit.model.characteristics import CharacteristicsTypes from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_MOTION, + DEVICE_CLASS_OPENING, DEVICE_CLASS_SMOKE, BinarySensorDevice, ) @@ -32,7 +34,7 @@ def _update_motion_detected(self, value): @property def device_class(self): """Define this binary_sensor as a motion sensor.""" - return "motion" + return DEVICE_CLASS_MOTION @property def is_on(self): @@ -55,6 +57,11 @@ def get_characteristic_types(self): def _update_contact_state(self, value): self._state = value + @property + def device_class(self): + """Define this binary_sensor as a opening sensor.""" + return DEVICE_CLASS_OPENING + @property def is_on(self): """Return true if the binary sensor is on/open.""" diff --git a/tests/components/homekit_controller/test_binary_sensor.py b/tests/components/homekit_controller/test_binary_sensor.py index 2809ab860be86..5107ae32bd512 100644 --- a/tests/components/homekit_controller/test_binary_sensor.py +++ b/tests/components/homekit_controller/test_binary_sensor.py @@ -2,6 +2,12 @@ from aiohomekit.model.characteristics import CharacteristicsTypes from aiohomekit.model.services import ServicesTypes +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_MOTION, + DEVICE_CLASS_OPENING, + DEVICE_CLASS_SMOKE, +) + from tests.components.homekit_controller.common import setup_test_component MOTION_DETECTED = ("motion", "motion-detected") @@ -29,6 +35,8 @@ async def test_motion_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "on" + assert state.attributes["device_class"] == DEVICE_CLASS_MOTION + def create_contact_sensor_service(accessory): """Define contact characteristics.""" @@ -50,6 +58,8 @@ async def test_contact_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "on" + assert state.attributes["device_class"] == DEVICE_CLASS_OPENING + def create_smoke_sensor_service(accessory): """Define smoke sensor characteristics.""" @@ -71,4 +81,4 @@ async def test_smoke_sensor_read_state(hass, utcnow): state = await helper.poll_and_get_state() assert state.state == "on" - assert state.attributes["device_class"] == "smoke" + assert state.attributes["device_class"] == DEVICE_CLASS_SMOKE