From 55e10d552e28f9c8414a2c432b95f482dec46899 Mon Sep 17 00:00:00 2001 From: SukramJ Date: Tue, 8 Oct 2019 19:52:43 +0200 Subject: [PATCH] Cleanup handling of attributes for HomematicIP Cloud (#27331) * Cleanup handling of attributes for HomematicIP Cloud * Remove special climate handling --- .../components/homematicip_cloud/binary_sensor.py | 5 ++--- .../components/homematicip_cloud/device.py | 15 ++++++++++++++- .../components/homematicip_cloud/sensor.py | 10 +++++++--- .../components/homematicip_cloud/switch.py | 6 ++++-- .../components/homematicip_cloud/weather.py | 2 +- 5 files changed, 28 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index b3a21ba0b5c..1114f10b622 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -40,7 +40,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice -from .device import ATTR_GROUP_MEMBER_UNREACHABLE, ATTR_IS_GROUP, ATTR_MODEL_TYPE +from .device import ATTR_GROUP_MEMBER_UNREACHABLE _LOGGER = logging.getLogger(__name__) @@ -60,7 +60,6 @@ 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, @@ -353,7 +352,7 @@ class HomematicipSecurityZoneSensorGroup(HomematicipGenericDevice, BinarySensorD @property def device_state_attributes(self): """Return the state attributes of the security zone group.""" - state_attr = {ATTR_MODEL_TYPE: self._device.modelType, ATTR_IS_GROUP: True} + state_attr = super().device_state_attributes for attr, attr_key in GROUP_ATTRIBUTES.items(): attr_value = getattr(self._device, attr, None) diff --git a/homeassistant/components/homematicip_cloud/device.py b/homeassistant/components/homematicip_cloud/device.py index 1273278189d..0389e0b9935 100644 --- a/homeassistant/components/homematicip_cloud/device.py +++ b/homeassistant/components/homematicip_cloud/device.py @@ -3,6 +3,7 @@ import logging from typing import Optional from homematicip.aio.device import AsyncDevice +from homematicip.aio.group import AsyncGroup from homematicip.aio.home import AsyncHome from homeassistant.components import homematicip_cloud @@ -13,6 +14,7 @@ from homeassistant.helpers.entity import Entity _LOGGER = logging.getLogger(__name__) ATTR_MODEL_TYPE = "model_type" +ATTR_GROUP_ID = "group_id" ATTR_ID = "id" ATTR_IS_GROUP = "is_group" # RSSI HAP -> Device @@ -35,15 +37,17 @@ DEVICE_ATTRIBUTE_ICONS = { DEVICE_ATTRIBUTES = { "modelType": ATTR_MODEL_TYPE, - "id": ATTR_ID, "sabotage": ATTR_SABOTAGE, "rssiDeviceValue": ATTR_RSSI_DEVICE, "rssiPeerValue": ATTR_RSSI_PEER, "deviceOverheated": ATTR_DEVICE_OVERHEATED, "deviceOverloaded": ATTR_DEVICE_OVERLOADED, "deviceUndervoltage": ATTR_DEVICE_UNTERVOLTAGE, + "id": ATTR_ID, } +GROUP_ATTRIBUTES = {"modelType": ATTR_MODEL_TYPE, "id": ATTR_GROUP_ID} + class HomematicipGenericDevice(Entity): """Representation of an HomematicIP generic device.""" @@ -173,6 +177,7 @@ class HomematicipGenericDevice(Entity): def device_state_attributes(self): """Return the state attributes of the generic device.""" state_attr = {} + if isinstance(self._device, AsyncDevice): for attr, attr_key in DEVICE_ATTRIBUTES.items(): attr_value = getattr(self._device, attr, None) @@ -181,4 +186,12 @@ class HomematicipGenericDevice(Entity): state_attr[ATTR_IS_GROUP] = False + if isinstance(self._device, AsyncGroup): + for attr, attr_key in GROUP_ATTRIBUTES.items(): + attr_value = getattr(self._device, attr, None) + if attr_value: + state_attr[attr_key] = attr_value + + state_attr[ATTR_IS_GROUP] = True + return state_attr diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index 30e910cc33a..ceb7fc39fd7 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -115,7 +115,6 @@ class HomematicipAccesspointStatus(HomematicipGenericDevice): def __init__(self, home: AsyncHome) -> None: """Initialize access point device.""" - home.modelType = "HmIP-HAP" super().__init__(home, home) @property @@ -152,7 +151,12 @@ class HomematicipAccesspointStatus(HomematicipGenericDevice): @property def device_state_attributes(self): """Return the state attributes of the access point.""" - return {ATTR_MODEL_TYPE: self._device.modelType, ATTR_IS_GROUP: False} + state_attr = super().device_state_attributes + + state_attr[ATTR_MODEL_TYPE] = "HmIP-HAP" + state_attr[ATTR_IS_GROUP] = False + + return state_attr class HomematicipHeatingThermostat(HomematicipGenericDevice): @@ -316,7 +320,7 @@ class HomematicipWindspeedSensor(HomematicipGenericDevice): state_attr = super().device_state_attributes wind_direction = getattr(self._device, "windDirection", None) - if wind_direction: + if wind_direction is not None: state_attr[ATTR_WIND_DIRECTION] = _get_wind_direction(wind_direction) wind_direction_variation = getattr(self._device, "windDirectionVariation", None) diff --git a/homeassistant/components/homematicip_cloud/switch.py b/homeassistant/components/homematicip_cloud/switch.py index ababf793f0c..7994aa446b8 100644 --- a/homeassistant/components/homematicip_cloud/switch.py +++ b/homeassistant/components/homematicip_cloud/switch.py @@ -19,7 +19,7 @@ from homeassistant.config_entries import ConfigEntry from homeassistant.core import HomeAssistant from . import DOMAIN as HMIPC_DOMAIN, HMIPC_HAPID, HomematicipGenericDevice -from .device import ATTR_GROUP_MEMBER_UNREACHABLE, ATTR_IS_GROUP +from .device import ATTR_GROUP_MEMBER_UNREACHABLE _LOGGER = logging.getLogger(__name__) @@ -113,9 +113,11 @@ class HomematicipGroupSwitch(HomematicipGenericDevice, SwitchDevice): @property def device_state_attributes(self): """Return the state attributes of the switch-group.""" - state_attr = {ATTR_IS_GROUP: True} + state_attr = super().device_state_attributes + if self._device.unreach: state_attr[ATTR_GROUP_MEMBER_UNREACHABLE] = True + return state_attr async def async_turn_on(self, **kwargs): diff --git a/homeassistant/components/homematicip_cloud/weather.py b/homeassistant/components/homematicip_cloud/weather.py index ed9098559a3..0d020312fe9 100644 --- a/homeassistant/components/homematicip_cloud/weather.py +++ b/homeassistant/components/homematicip_cloud/weather.py @@ -123,7 +123,7 @@ class HomematicipHomeWeather(HomematicipGenericDevice, WeatherEntity): def __init__(self, home: AsyncHome) -> None: """Initialize the home weather.""" - home.weather.modelType = "HmIP-Home-Weather" + home.modelType = "HmIP-Home-Weather" super().__init__(home, home) @property