From 37f7d262d752f5509b599b0f6924c72351777f42 Mon Sep 17 00:00:00 2001 From: Robert Svensson Date: Tue, 2 Jun 2020 16:17:21 +0200 Subject: [PATCH] Change deCONZ sensor device classes (#36352) * Change vibration type to vibration instead of motion * Also replace icon and unit of measurement --- .../components/deconz/binary_sensor.py | 28 +++++++++---- homeassistant/components/deconz/sensor.py | 41 +++++++++++++++++-- tests/components/deconz/test_binary_sensor.py | 6 +++ tests/components/deconz/test_sensor.py | 9 ++++ 4 files changed, 73 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/deconz/binary_sensor.py b/homeassistant/components/deconz/binary_sensor.py index 95fa223c697..c8917934dd7 100644 --- a/homeassistant/components/deconz/binary_sensor.py +++ b/homeassistant/components/deconz/binary_sensor.py @@ -1,7 +1,15 @@ """Support for deCONZ binary sensors.""" -from pydeconz.sensor import Presence, Vibration +from pydeconz.sensor import CarbonMonoxide, Fire, OpenClose, Presence, Vibration, Water -from homeassistant.components.binary_sensor import BinarySensorEntity +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_GAS, + DEVICE_CLASS_MOISTURE, + DEVICE_CLASS_MOTION, + DEVICE_CLASS_OPENING, + DEVICE_CLASS_SMOKE, + DEVICE_CLASS_VIBRATION, + BinarySensorEntity, +) from homeassistant.const import ATTR_TEMPERATURE from homeassistant.core import callback from homeassistant.helpers.dispatcher import async_dispatcher_connect @@ -14,6 +22,15 @@ ATTR_ORIENTATION = "orientation" ATTR_TILTANGLE = "tiltangle" ATTR_VIBRATIONSTRENGTH = "vibrationstrength" +DEVICE_CLASS = { + CarbonMonoxide: DEVICE_CLASS_GAS, + Fire: DEVICE_CLASS_SMOKE, + OpenClose: DEVICE_CLASS_OPENING, + Presence: DEVICE_CLASS_MOTION, + Vibration: DEVICE_CLASS_VIBRATION, + Water: DEVICE_CLASS_MOISTURE, +} + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Old way of setting up deCONZ platforms.""" @@ -74,12 +91,7 @@ class DeconzBinarySensor(DeconzDevice, BinarySensorEntity): @property def device_class(self): """Return the class of the sensor.""" - return self._device.SENSOR_CLASS - - @property - def icon(self): - """Return the icon to use in the frontend.""" - return self._device.SENSOR_ICON + return DEVICE_CLASS.get(type(self._device)) @property def device_state_attributes(self): diff --git a/homeassistant/components/deconz/sensor.py b/homeassistant/components/deconz/sensor.py index ae0e55ae51f..330c262110a 100644 --- a/homeassistant/components/deconz/sensor.py +++ b/homeassistant/components/deconz/sensor.py @@ -3,9 +3,12 @@ from pydeconz.sensor import ( Battery, Consumption, Daylight, + Humidity, LightLevel, Power, + Pressure, Switch, + Temperature, Thermostat, ) @@ -13,6 +16,15 @@ from homeassistant.const import ( ATTR_TEMPERATURE, ATTR_VOLTAGE, DEVICE_CLASS_BATTERY, + DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_POWER, + DEVICE_CLASS_PRESSURE, + DEVICE_CLASS_TEMPERATURE, + ENERGY_KILO_WATT_HOUR, + POWER_WATT, + PRESSURE_HPA, + TEMP_CELSIUS, UNIT_PERCENTAGE, ) from homeassistant.core import callback @@ -31,6 +43,29 @@ ATTR_POWER = "power" ATTR_DAYLIGHT = "daylight" ATTR_EVENT_ID = "event_id" +DEVICE_CLASS = { + Humidity: DEVICE_CLASS_HUMIDITY, + LightLevel: DEVICE_CLASS_ILLUMINANCE, + Power: DEVICE_CLASS_POWER, + Pressure: DEVICE_CLASS_PRESSURE, + Temperature: DEVICE_CLASS_TEMPERATURE, +} + +ICON = { + Daylight: "mdi:white-balance-sunny", + Pressure: "mdi:gauge", + Temperature: "mdi:thermometer", +} + +UNIT_OF_MEASUREMENT = { + Consumption: ENERGY_KILO_WATT_HOUR, + Humidity: UNIT_PERCENTAGE, + LightLevel: "lx", + Power: POWER_WATT, + Pressure: PRESSURE_HPA, + Temperature: TEMP_CELSIUS, +} + async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Old way of setting up deCONZ platforms.""" @@ -119,17 +154,17 @@ class DeconzSensor(DeconzDevice): @property def device_class(self): """Return the class of the sensor.""" - return self._device.SENSOR_CLASS + return DEVICE_CLASS.get(type(self._device)) @property def icon(self): """Return the icon to use in the frontend.""" - return self._device.SENSOR_ICON + return ICON.get(type(self._device)) @property def unit_of_measurement(self): """Return the unit of measurement of this sensor.""" - return self._device.SENSOR_UNIT + return UNIT_OF_MEASUREMENT.get(type(self._device)) @property def device_state_attributes(self): diff --git a/tests/components/deconz/test_binary_sensor.py b/tests/components/deconz/test_binary_sensor.py index 864ba91fbc1..30f7251e067 100644 --- a/tests/components/deconz/test_binary_sensor.py +++ b/tests/components/deconz/test_binary_sensor.py @@ -3,6 +3,10 @@ from copy import deepcopy from homeassistant.components import deconz import homeassistant.components.binary_sensor as binary_sensor +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_MOTION, + DEVICE_CLASS_VIBRATION, +) from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -79,6 +83,7 @@ async def test_binary_sensors(hass): presence_sensor = hass.states.get("binary_sensor.presence_sensor") assert presence_sensor.state == "off" + assert presence_sensor.attributes["device_class"] == DEVICE_CLASS_MOTION temperature_sensor = hass.states.get("binary_sensor.temperature_sensor") assert temperature_sensor is None @@ -88,6 +93,7 @@ async def test_binary_sensors(hass): vibration_sensor = hass.states.get("binary_sensor.vibration_sensor") assert vibration_sensor.state == "on" + assert vibration_sensor.attributes["device_class"] == DEVICE_CLASS_VIBRATION state_changed_event = { "t": "event", diff --git a/tests/components/deconz/test_sensor.py b/tests/components/deconz/test_sensor.py index cda3138557d..9d87c7b91cb 100644 --- a/tests/components/deconz/test_sensor.py +++ b/tests/components/deconz/test_sensor.py @@ -3,6 +3,11 @@ from copy import deepcopy from homeassistant.components import deconz import homeassistant.components.sensor as sensor +from homeassistant.const import ( + DEVICE_CLASS_BATTERY, + DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_POWER, +) from homeassistant.setup import async_setup_component from .test_gateway import DECONZ_WEB_REQUEST, setup_deconz_integration @@ -112,6 +117,7 @@ async def test_sensors(hass): light_level_sensor = hass.states.get("sensor.light_level_sensor") assert light_level_sensor.state == "999.8" + assert light_level_sensor.attributes["device_class"] == DEVICE_CLASS_ILLUMINANCE presence_sensor = hass.states.get("sensor.presence_sensor") assert presence_sensor is None @@ -127,15 +133,18 @@ async def test_sensors(hass): switch_2_battery_level = hass.states.get("sensor.switch_2_battery_level") assert switch_2_battery_level.state == "100" + assert switch_2_battery_level.attributes["device_class"] == DEVICE_CLASS_BATTERY daylight_sensor = hass.states.get("sensor.daylight_sensor") assert daylight_sensor is None power_sensor = hass.states.get("sensor.power_sensor") assert power_sensor.state == "6" + assert power_sensor.attributes["device_class"] == DEVICE_CLASS_POWER consumption_sensor = hass.states.get("sensor.consumption_sensor") assert consumption_sensor.state == "0.002" + assert "device_class" not in consumption_sensor.attributes state_changed_event = { "t": "event",