From 1fb377e61eeea93336d8ca4d7941b3bceb436f61 Mon Sep 17 00:00:00 2001 From: SukramJ Date: Fri, 1 Nov 2019 21:25:33 +0100 Subject: [PATCH] Use defined device class constants for Homematic (#28438) * Use defined device classes for Homematic * Remove not required default None * Missed on None --- .../components/homematic/binary_sensor.py | 32 ++++++++----- homeassistant/components/homematic/sensor.py | 47 ++++++++++++------- 2 files changed, 49 insertions(+), 30 deletions(-) diff --git a/homeassistant/components/homematic/binary_sensor.py b/homeassistant/components/homematic/binary_sensor.py index 064b6f3e009..cc2907c64fb 100644 --- a/homeassistant/components/homematic/binary_sensor.py +++ b/homeassistant/components/homematic/binary_sensor.py @@ -1,26 +1,32 @@ """Support for HomeMatic binary sensors.""" import logging -from homeassistant.components.binary_sensor import BinarySensorDevice +from homeassistant.components.binary_sensor import ( + DEVICE_CLASS_BATTERY, + DEVICE_CLASS_MOTION, + DEVICE_CLASS_OPENING, + DEVICE_CLASS_PRESENCE, + DEVICE_CLASS_SMOKE, + BinarySensorDevice, +) from homeassistant.components.homematic import ATTR_DISCOVERY_TYPE, DISCOVER_BATTERY -from homeassistant.const import DEVICE_CLASS_BATTERY from . import ATTR_DISCOVER_DEVICES, HMDevice _LOGGER = logging.getLogger(__name__) SENSOR_TYPES_CLASS = { - "IPShutterContact": "opening", - "IPShutterContactSabotage": "opening", - "MaxShutterContact": "opening", - "Motion": "motion", - "MotionV2": "motion", - "PresenceIP": "motion", + "IPShutterContact": DEVICE_CLASS_OPENING, + "IPShutterContactSabotage": DEVICE_CLASS_OPENING, + "MaxShutterContact": DEVICE_CLASS_OPENING, + "Motion": DEVICE_CLASS_MOTION, + "MotionV2": DEVICE_CLASS_MOTION, + "PresenceIP": DEVICE_CLASS_PRESENCE, "Remote": None, "RemoteMotion": None, - "ShutterContact": "opening", - "Smoke": "smoke", - "SmokeV2": "smoke", + "ShutterContact": DEVICE_CLASS_OPENING, + "Smoke": DEVICE_CLASS_SMOKE, + "SmokeV2": DEVICE_CLASS_SMOKE, "TiltSensor": None, "WeatherSensor": None, } @@ -56,8 +62,8 @@ class HMBinarySensor(HMDevice, BinarySensorDevice): """Return the class of this sensor from DEVICE_CLASSES.""" # If state is MOTION (Only RemoteMotion working) if self._state == "MOTION": - return "motion" - return SENSOR_TYPES_CLASS.get(self._hmdevice.__class__.__name__, None) + return DEVICE_CLASS_MOTION + return SENSOR_TYPES_CLASS.get(self._hmdevice.__class__.__name__) def _init_data_struct(self): """Generate the data dictionary (self._data) from metadata.""" diff --git a/homeassistant/components/homematic/sensor.py b/homeassistant/components/homematic/sensor.py index a3fc9f7e0fa..10c402a0dd4 100644 --- a/homeassistant/components/homematic/sensor.py +++ b/homeassistant/components/homematic/sensor.py @@ -1,7 +1,15 @@ """Support for HomeMatic sensors.""" import logging -from homeassistant.const import ENERGY_WATT_HOUR, POWER_WATT, STATE_UNKNOWN +from homeassistant.const import ( + DEVICE_CLASS_HUMIDITY, + DEVICE_CLASS_ILLUMINANCE, + DEVICE_CLASS_POWER, + DEVICE_CLASS_TEMPERATURE, + ENERGY_WATT_HOUR, + POWER_WATT, + STATE_UNKNOWN, +) from . import ATTR_DISCOVER_DEVICES, HMDevice @@ -48,21 +56,21 @@ HM_UNIT_HA_CAST = { "VALUE": "#", } -HM_ICON_HA_CAST = { - "WIND_SPEED": "mdi:weather-windy", - "HUMIDITY": "mdi:water-percent", - "TEMPERATURE": "mdi:thermometer", - "ACTUAL_TEMPERATURE": "mdi:thermometer", - "LUX": "mdi:weather-sunny", - "CURRENT_ILLUMINATION": "mdi:weather-sunny", - "AVERAGE_ILLUMINATION": "mdi:weather-sunny", - "LOWEST_ILLUMINATION": "mdi:weather-sunny", - "HIGHEST_ILLUMINATION": "mdi:weather-sunny", - "BRIGHTNESS": "mdi:invert-colors", - "POWER": "mdi:flash-red-eye", - "CURRENT": "mdi:flash-red-eye", +HM_DEVICE_CLASS_HA_CAST = { + "HUMIDITY": DEVICE_CLASS_HUMIDITY, + "TEMPERATURE": DEVICE_CLASS_TEMPERATURE, + "ACTUAL_TEMPERATURE": DEVICE_CLASS_TEMPERATURE, + "LUX": DEVICE_CLASS_ILLUMINANCE, + "CURRENT_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE, + "AVERAGE_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE, + "LOWEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE, + "HIGHEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE, + "POWER": DEVICE_CLASS_POWER, + "CURRENT": DEVICE_CLASS_POWER, } +HM_ICON_HA_CAST = {"WIND_SPEED": "mdi:weather-windy", "BRIGHTNESS": "mdi:invert-colors"} + def setup_platform(hass, config, add_entities, discovery_info=None): """Set up the HomeMatic sensor platform.""" @@ -86,7 +94,7 @@ class HMSensor(HMDevice): # Does a cast exist for this class? name = self._hmdevice.__class__.__name__ if name in HM_STATE_HA_CAST: - return HM_STATE_HA_CAST[name].get(self._hm_get_state(), None) + return HM_STATE_HA_CAST[name].get(self._hm_get_state()) # No cast, return original value return self._hm_get_state() @@ -94,12 +102,17 @@ class HMSensor(HMDevice): @property def unit_of_measurement(self): """Return the unit of measurement of this entity, if any.""" - return HM_UNIT_HA_CAST.get(self._state, None) + return HM_UNIT_HA_CAST.get(self._state) + + @property + def device_class(self): + """Return the device class to use in the frontend, if any.""" + return HM_DEVICE_CLASS_HA_CAST.get(self._state) @property def icon(self): """Return the icon to use in the frontend, if any.""" - return HM_ICON_HA_CAST.get(self._state, None) + return HM_ICON_HA_CAST.get(self._state) def _init_data_struct(self): """Generate a data dictionary (self._data) from metadata."""