From b5c2c9ec9b5faea35ce334576351d437fd83146b Mon Sep 17 00:00:00 2001 From: SukramJ Date: Fri, 28 Aug 2020 07:08:37 +0200 Subject: [PATCH] Add device attribute for homematicip_cloud rotary handle (#39144) --- .../homematicip_cloud/binary_sensor.py | 26 ++++++++++++++++--- .../homematicip_cloud/test_binary_sensor.py | 8 ++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/homematicip_cloud/binary_sensor.py b/homeassistant/components/homematicip_cloud/binary_sensor.py index aae7f881be0..0867fa7002a 100644 --- a/homeassistant/components/homematicip_cloud/binary_sensor.py +++ b/homeassistant/components/homematicip_cloud/binary_sensor.py @@ -87,11 +87,10 @@ async def async_setup_entry( entities.append(HomematicipAccelerationSensor(hap, device)) if isinstance(device, (AsyncContactInterface, AsyncFullFlushContactInterface)): entities.append(HomematicipContactInterface(hap, device)) - if isinstance( - device, - (AsyncShutterContact, AsyncShutterContactMagnetic, AsyncRotaryHandleSensor), - ): + if isinstance(device, (AsyncShutterContact, AsyncShutterContactMagnetic),): entities.append(HomematicipShutterContact(hap, device)) + if isinstance(device, AsyncRotaryHandleSensor): + entities.append(HomematicipShutterContact(hap, device, True)) if isinstance( device, ( @@ -176,6 +175,13 @@ class HomematicipContactInterface(HomematicipGenericEntity, BinarySensorEntity): class HomematicipShutterContact(HomematicipGenericEntity, BinarySensorEntity): """Representation of the HomematicIP shutter contact.""" + def __init__( + self, hap: HomematicipHAP, device, has_additional_state: bool = False + ) -> None: + """Initialize the shutter contact.""" + super().__init__(hap, device) + self.has_additional_state = has_additional_state + @property def device_class(self) -> str: """Return the class of this sensor.""" @@ -188,6 +194,18 @@ class HomematicipShutterContact(HomematicipGenericEntity, BinarySensorEntity): return None return self._device.windowState != WindowState.CLOSED + @property + def device_state_attributes(self) -> Dict[str, Any]: + """Return the state attributes of the Shutter Contact.""" + state_attr = super().device_state_attributes + + if self.has_additional_state: + window_state = getattr(self._device, "windowState", None) + if window_state and window_state != WindowState.CLOSED: + state_attr[ATTR_WINDOW_STATE] = window_state + + return state_attr + class HomematicipMotionDetector(HomematicipGenericEntity, BinarySensorEntity): """Representation of the HomematicIP motion detector.""" diff --git a/tests/components/homematicip_cloud/test_binary_sensor.py b/tests/components/homematicip_cloud/test_binary_sensor.py index 6be0036a737..06b19ae0805 100644 --- a/tests/components/homematicip_cloud/test_binary_sensor.py +++ b/tests/components/homematicip_cloud/test_binary_sensor.py @@ -112,11 +112,19 @@ async def test_hmip_shutter_contact(hass, default_mock_hap_factory): ) assert ha_state.state == STATE_ON + assert ha_state.attributes[ATTR_WINDOW_STATE] == WindowState.TILTED + + await async_manipulate_test_data(hass, hmip_device, "windowState", WindowState.OPEN) + ha_state = hass.states.get(entity_id) + assert ha_state.state == STATE_ON + assert ha_state.attributes[ATTR_WINDOW_STATE] == WindowState.OPEN + await async_manipulate_test_data( hass, hmip_device, "windowState", WindowState.CLOSED ) ha_state = hass.states.get(entity_id) assert ha_state.state == STATE_OFF + assert not ha_state.attributes.get(ATTR_WINDOW_STATE) await async_manipulate_test_data(hass, hmip_device, "windowState", None) ha_state = hass.states.get(entity_id)