Unify device_state_attributes handling for Homematic IP Cloud (#26449)

*  unifi DSA for Homematic IP Cloud

* sabotage is not relevant for state

* TODAY_SUNSHINE_DURATION is not a group attribute

* Separated the words as requested

* add missing underscores
This commit is contained in:
SukramJ 2019-09-06 15:28:24 +02:00 committed by Martin Hjelmare
parent a202afcac2
commit f540d74b65
5 changed files with 77 additions and 81 deletions

View File

@ -43,14 +43,25 @@ from .device import ATTR_GROUP_MEMBER_UNREACHABLE, ATTR_MODEL_TYPE
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
ATTR_LOW_BATTERY = "low_battery" ATTR_LOW_BATTERY = "low_battery"
ATTR_MOTIONDETECTED = "motion detected" ATTR_MOISTURE_DETECTED = "moisture_detected"
ATTR_PRESENCEDETECTED = "presence detected" ATTR_MOTION_DETECTED = "motion_detected"
ATTR_POWERMAINSFAILURE = "power mains failure" ATTR_POWER_MAINS_FAILURE = "power_mains_failure"
ATTR_WINDOWSTATE = "window state" ATTR_PRESENCE_DETECTED = "presence_detected"
ATTR_MOISTUREDETECTED = "moisture detected" ATTR_SMOKE_DETECTOR_ALARM = "smoke_detector_alarm"
ATTR_WATERLEVELDETECTED = "water level detected"
ATTR_SMOKEDETECTORALARM = "smoke detector alarm"
ATTR_TODAY_SUNSHINE_DURATION = "today_sunshine_duration_in_minutes" ATTR_TODAY_SUNSHINE_DURATION = "today_sunshine_duration_in_minutes"
ATTR_WATER_LEVEL_DETECTED = "water_level_detected"
ATTR_WINDOW_STATE = "window_state"
GROUP_ATTRIBUTES = {
"lowBat": ATTR_LOW_BATTERY,
"modelType": ATTR_MODEL_TYPE,
"moistureDetected": ATTR_MOISTURE_DETECTED,
"motionDetected": ATTR_MOTION_DETECTED,
"powerMainsFailure": ATTR_POWER_MAINS_FAILURE,
"presenceDetected": ATTR_PRESENCE_DETECTED,
"unreach": ATTR_GROUP_MEMBER_UNREACHABLE,
"waterlevelDetected": ATTR_WATER_LEVEL_DETECTED,
}
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None): async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
@ -118,8 +129,6 @@ class HomematicipContactInterface(HomematicipGenericDevice, BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if the contact interface is on/open.""" """Return true if the contact interface is on/open."""
if hasattr(self._device, "sabotage") and self._device.sabotage:
return True
if self._device.windowState is None: if self._device.windowState is None:
return None return None
return self._device.windowState != WindowState.CLOSED return self._device.windowState != WindowState.CLOSED
@ -136,8 +145,6 @@ class HomematicipShutterContact(HomematicipGenericDevice, BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if the shutter contact is on/open.""" """Return true if the shutter contact is on/open."""
if hasattr(self._device, "sabotage") and self._device.sabotage:
return True
if self._device.windowState is None: if self._device.windowState is None:
return None return None
return self._device.windowState != WindowState.CLOSED return self._device.windowState != WindowState.CLOSED
@ -154,8 +161,6 @@ class HomematicipMotionDetector(HomematicipGenericDevice, BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if motion is detected.""" """Return true if motion is detected."""
if hasattr(self._device, "sabotage") and self._device.sabotage:
return True
return self._device.motionDetected return self._device.motionDetected
@ -170,8 +175,6 @@ class HomematicipPresenceDetector(HomematicipGenericDevice, BinarySensorDevice):
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if presence is detected.""" """Return true if presence is detected."""
if hasattr(self._device, "sabotage") and self._device.sabotage:
return True
return self._device.presenceDetected return self._device.presenceDetected
@ -259,13 +262,13 @@ class HomematicipSunshineSensor(HomematicipGenericDevice, BinarySensorDevice):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the illuminance sensor.""" """Return the state attributes of the illuminance sensor."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if (
hasattr(self._device, "todaySunshineDuration") today_sunshine_duration = getattr(self._device, "todaySunshineDuration", None)
and self._device.todaySunshineDuration if today_sunshine_duration:
): state_attr[ATTR_TODAY_SUNSHINE_DURATION] = today_sunshine_duration
attr[ATTR_TODAY_SUNSHINE_DURATION] = self._device.todaySunshineDuration
return attr return state_attr
class HomematicipBatterySensor(HomematicipGenericDevice, BinarySensorDevice): class HomematicipBatterySensor(HomematicipGenericDevice, BinarySensorDevice):
@ -309,21 +312,18 @@ class HomematicipSecurityZoneSensorGroup(HomematicipGenericDevice, BinarySensorD
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the security zone group.""" """Return the state attributes of the security zone group."""
attr = {ATTR_MODEL_TYPE: self._device.modelType} state_attr = {ATTR_MODEL_TYPE: self._device.modelType}
if self._device.motionDetected: for attr, attr_key in GROUP_ATTRIBUTES.items():
attr[ATTR_MOTIONDETECTED] = True attr_value = getattr(self._device, attr, None)
if self._device.presenceDetected: if attr_value:
attr[ATTR_PRESENCEDETECTED] = True state_attr[attr_key] = attr_value
if ( window_state = getattr(self._device, "windowState", None)
self._device.windowState is not None if window_state and window_state != WindowState.CLOSED:
and self._device.windowState != WindowState.CLOSED state_attr[ATTR_WINDOW_STATE] = str(window_state)
):
attr[ATTR_WINDOWSTATE] = str(self._device.windowState) return state_attr
if self._device.unreach:
attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True
return attr
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -356,23 +356,13 @@ class HomematicipSecuritySensorGroup(
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the security group.""" """Return the state attributes of the security group."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if self._device.powerMainsFailure: smoke_detector_at = getattr(self._device, "smokeDetectorAlarmType", None)
attr[ATTR_POWERMAINSFAILURE] = True if smoke_detector_at and smoke_detector_at != SmokeDetectorAlarmType.IDLE_OFF:
if self._device.moistureDetected: state_attr[ATTR_SMOKE_DETECTOR_ALARM] = str(smoke_detector_at)
attr[ATTR_MOISTUREDETECTED] = True
if self._device.waterlevelDetected:
attr[ATTR_WATERLEVELDETECTED] = True
if self._device.lowBat:
attr[ATTR_LOW_BATTERY] = True
if (
self._device.smokeDetectorAlarmType is not None
and self._device.smokeDetectorAlarmType != SmokeDetectorAlarmType.IDLE_OFF
):
attr[ATTR_SMOKEDETECTORALARM] = str(self._device.smokeDetectorAlarmType)
return attr return state_attr
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:

View File

@ -93,13 +93,15 @@ class HomematicipLightMeasuring(HomematicipLight):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the generic device.""" """Return the state attributes of the generic device."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if self._device.currentPowerConsumption > 0.05:
attr[ATTR_POWER_CONSUMPTION] = round( current_power_consumption = self._device.currentPowerConsumption
self._device.currentPowerConsumption, 2 if current_power_consumption > 0.05:
) state_attr[ATTR_POWER_CONSUMPTION] = round(current_power_consumption, 2)
attr[ATTR_ENERGY_COUNTER] = round(self._device.energyCounter, 2)
return attr state_attr[ATTR_ENERGY_COUNTER] = round(self._device.energyCounter, 2)
return state_attr
class HomematicipDimmer(HomematicipGenericDevice, Light): class HomematicipDimmer(HomematicipGenericDevice, Light):
@ -187,15 +189,17 @@ class HomematicipNotificationLight(HomematicipGenericDevice, Light):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the generic device.""" """Return the state attributes of the generic device."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if self.is_on: if self.is_on:
attr[ATTR_COLOR_NAME] = self._func_channel.simpleRGBColorState state_attr[ATTR_COLOR_NAME] = self._func_channel.simpleRGBColorState
return attr
return state_attr
@property @property
def name(self) -> str: def name(self) -> str:
"""Return the name of the generic device.""" """Return the name of the generic device."""
return "{} {}".format(super().name, "Notification") return f"{super().name} Notification"
@property @property
def supported_features(self) -> int: def supported_features(self) -> int:

View File

@ -229,13 +229,13 @@ class HomematicipTemperatureSensor(HomematicipGenericDevice):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the windspeed sensor.""" """Return the state attributes of the windspeed sensor."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if (
hasattr(self._device, "temperatureOffset") temperature_offset = getattr(self._device, "temperatureOffset", None)
and self._device.temperatureOffset if temperature_offset:
): state_attr[ATTR_TEMPERATURE_OFFSET] = temperature_offset
attr[ATTR_TEMPERATURE_OFFSET] = self._device.temperatureOffset
return attr return state_attr
class HomematicipIlluminanceSensor(HomematicipGenericDevice): class HomematicipIlluminanceSensor(HomematicipGenericDevice):
@ -307,15 +307,17 @@ class HomematicipWindspeedSensor(HomematicipGenericDevice):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the wind speed sensor.""" """Return the state attributes of the wind speed sensor."""
attr = super().device_state_attributes state_attr = super().device_state_attributes
if hasattr(self._device, "windDirection") and self._device.windDirection:
attr[ATTR_WIND_DIRECTION] = _get_wind_direction(self._device.windDirection) wind_direction = getattr(self._device, "windDirection", None)
if ( if wind_direction:
hasattr(self._device, "windDirectionVariation") state_attr[ATTR_WIND_DIRECTION] = _get_wind_direction(wind_direction)
and self._device.windDirectionVariation
): wind_direction_variation = getattr(self._device, "windDirectionVariation", None)
attr[ATTR_WIND_DIRECTION_VARIATION] = self._device.windDirectionVariation if wind_direction_variation:
return attr state_attr[ATTR_WIND_DIRECTION_VARIATION] = wind_direction_variation
return state_attr
class HomematicipTodayRainSensor(HomematicipGenericDevice): class HomematicipTodayRainSensor(HomematicipGenericDevice):

View File

@ -113,10 +113,10 @@ class HomematicipGroupSwitch(HomematicipGenericDevice, SwitchDevice):
@property @property
def device_state_attributes(self): def device_state_attributes(self):
"""Return the state attributes of the switch-group.""" """Return the state attributes of the switch-group."""
attr = {} state_attr = {}
if self._device.unreach: if self._device.unreach:
attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True state_attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True
return attr return state_attr
async def async_turn_on(self, **kwargs): async def async_turn_on(self, **kwargs):
"""Turn the group on.""" """Turn the group on."""

View File

@ -79,7 +79,7 @@ class HomematicipWeatherSensor(HomematicipGenericDevice, WeatherEntity):
@property @property
def condition(self) -> str: def condition(self) -> str:
"""Return the current condition.""" """Return the current condition."""
if hasattr(self._device, "raining") and self._device.raining: if getattr(self._device, "raining", None):
return "rainy" return "rainy"
if self._device.storm: if self._device.storm:
return "windy" return "windy"