Use defined device class constants for Homematic (#28438)

* Use defined device classes for Homematic

* Remove not required default None

* Missed on None
This commit is contained in:
SukramJ 2019-11-01 21:25:33 +01:00 committed by Pascal Vizeli
parent f8d779e840
commit 1fb377e61e
2 changed files with 49 additions and 30 deletions

View File

@ -1,26 +1,32 @@
"""Support for HomeMatic binary sensors.""" """Support for HomeMatic binary sensors."""
import logging 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.components.homematic import ATTR_DISCOVERY_TYPE, DISCOVER_BATTERY
from homeassistant.const import DEVICE_CLASS_BATTERY
from . import ATTR_DISCOVER_DEVICES, HMDevice from . import ATTR_DISCOVER_DEVICES, HMDevice
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
SENSOR_TYPES_CLASS = { SENSOR_TYPES_CLASS = {
"IPShutterContact": "opening", "IPShutterContact": DEVICE_CLASS_OPENING,
"IPShutterContactSabotage": "opening", "IPShutterContactSabotage": DEVICE_CLASS_OPENING,
"MaxShutterContact": "opening", "MaxShutterContact": DEVICE_CLASS_OPENING,
"Motion": "motion", "Motion": DEVICE_CLASS_MOTION,
"MotionV2": "motion", "MotionV2": DEVICE_CLASS_MOTION,
"PresenceIP": "motion", "PresenceIP": DEVICE_CLASS_PRESENCE,
"Remote": None, "Remote": None,
"RemoteMotion": None, "RemoteMotion": None,
"ShutterContact": "opening", "ShutterContact": DEVICE_CLASS_OPENING,
"Smoke": "smoke", "Smoke": DEVICE_CLASS_SMOKE,
"SmokeV2": "smoke", "SmokeV2": DEVICE_CLASS_SMOKE,
"TiltSensor": None, "TiltSensor": None,
"WeatherSensor": None, "WeatherSensor": None,
} }
@ -56,8 +62,8 @@ class HMBinarySensor(HMDevice, BinarySensorDevice):
"""Return the class of this sensor from DEVICE_CLASSES.""" """Return the class of this sensor from DEVICE_CLASSES."""
# If state is MOTION (Only RemoteMotion working) # If state is MOTION (Only RemoteMotion working)
if self._state == "MOTION": if self._state == "MOTION":
return "motion" return DEVICE_CLASS_MOTION
return SENSOR_TYPES_CLASS.get(self._hmdevice.__class__.__name__, None) return SENSOR_TYPES_CLASS.get(self._hmdevice.__class__.__name__)
def _init_data_struct(self): def _init_data_struct(self):
"""Generate the data dictionary (self._data) from metadata.""" """Generate the data dictionary (self._data) from metadata."""

View File

@ -1,7 +1,15 @@
"""Support for HomeMatic sensors.""" """Support for HomeMatic sensors."""
import logging 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 from . import ATTR_DISCOVER_DEVICES, HMDevice
@ -48,21 +56,21 @@ HM_UNIT_HA_CAST = {
"VALUE": "#", "VALUE": "#",
} }
HM_ICON_HA_CAST = { HM_DEVICE_CLASS_HA_CAST = {
"WIND_SPEED": "mdi:weather-windy", "HUMIDITY": DEVICE_CLASS_HUMIDITY,
"HUMIDITY": "mdi:water-percent", "TEMPERATURE": DEVICE_CLASS_TEMPERATURE,
"TEMPERATURE": "mdi:thermometer", "ACTUAL_TEMPERATURE": DEVICE_CLASS_TEMPERATURE,
"ACTUAL_TEMPERATURE": "mdi:thermometer", "LUX": DEVICE_CLASS_ILLUMINANCE,
"LUX": "mdi:weather-sunny", "CURRENT_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
"CURRENT_ILLUMINATION": "mdi:weather-sunny", "AVERAGE_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
"AVERAGE_ILLUMINATION": "mdi:weather-sunny", "LOWEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
"LOWEST_ILLUMINATION": "mdi:weather-sunny", "HIGHEST_ILLUMINATION": DEVICE_CLASS_ILLUMINANCE,
"HIGHEST_ILLUMINATION": "mdi:weather-sunny", "POWER": DEVICE_CLASS_POWER,
"BRIGHTNESS": "mdi:invert-colors", "CURRENT": DEVICE_CLASS_POWER,
"POWER": "mdi:flash-red-eye",
"CURRENT": "mdi:flash-red-eye",
} }
HM_ICON_HA_CAST = {"WIND_SPEED": "mdi:weather-windy", "BRIGHTNESS": "mdi:invert-colors"}
def setup_platform(hass, config, add_entities, discovery_info=None): def setup_platform(hass, config, add_entities, discovery_info=None):
"""Set up the HomeMatic sensor platform.""" """Set up the HomeMatic sensor platform."""
@ -86,7 +94,7 @@ class HMSensor(HMDevice):
# Does a cast exist for this class? # Does a cast exist for this class?
name = self._hmdevice.__class__.__name__ name = self._hmdevice.__class__.__name__
if name in HM_STATE_HA_CAST: 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 # No cast, return original value
return self._hm_get_state() return self._hm_get_state()
@ -94,12 +102,17 @@ class HMSensor(HMDevice):
@property @property
def unit_of_measurement(self): def unit_of_measurement(self):
"""Return the unit of measurement of this entity, if any.""" """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 @property
def icon(self): def icon(self):
"""Return the icon to use in the frontend, if any.""" """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): def _init_data_struct(self):
"""Generate a data dictionary (self._data) from metadata.""" """Generate a data dictionary (self._data) from metadata."""