diff --git a/homeassistant/components/homematicip_cloud/hap.py b/homeassistant/components/homematicip_cloud/hap.py index 86630c2896c..f3681a89110 100644 --- a/homeassistant/components/homematicip_cloud/hap.py +++ b/homeassistant/components/homematicip_cloud/hap.py @@ -112,6 +112,7 @@ class HomematicipHAP: self.config_entry = config_entry self._ws_close_requested = False + self._ws_connection_closed = asyncio.Event() self._retry_task: asyncio.Task | None = None self._tries = 0 self._accesspoint_connected = True @@ -218,6 +219,8 @@ class HomematicipHAP: try: await self.home.get_current_state_async() hmip_events = self.home.enable_events() + self.home.set_on_connected_handler(self.ws_connected_handler) + self.home.set_on_disconnected_handler(self.ws_disconnected_handler) tries = 0 await hmip_events except HmipConnectionError: @@ -267,6 +270,18 @@ class HomematicipHAP: "Reset connection to access point id %s", self.config_entry.unique_id ) + async def ws_connected_handler(self) -> None: + """Handle websocket connected.""" + _LOGGER.debug("WebSocket connection to HomematicIP established") + if self._ws_connection_closed.is_set(): + await self.get_state() + self._ws_connection_closed.clear() + + async def ws_disconnected_handler(self) -> None: + """Handle websocket disconnection.""" + _LOGGER.warning("WebSocket connection to HomematicIP closed") + self._ws_connection_closed.set() + async def get_hap( self, hass: HomeAssistant, @@ -290,6 +305,7 @@ class HomematicipHAP: raise HmipcConnectionError from err home.on_update(self.async_update) home.on_create(self.async_create_entity) + hass.loop.create_task(self.async_connect()) return home diff --git a/homeassistant/components/homematicip_cloud/light.py b/homeassistant/components/homematicip_cloud/light.py index 855f5851d73..d5175e6e647 100644 --- a/homeassistant/components/homematicip_cloud/light.py +++ b/homeassistant/components/homematicip_cloud/light.py @@ -4,16 +4,16 @@ from __future__ import annotations from typing import Any -from homematicip.base.enums import OpticalSignalBehaviour, RGBColorState +from homematicip.base.enums import DeviceType, OpticalSignalBehaviour, RGBColorState from homematicip.base.functionalChannels import NotificationLightChannel from homematicip.device import ( BrandDimmer, - BrandSwitchMeasuring, BrandSwitchNotificationLight, Dimmer, DinRailDimmer3, FullFlushDimmer, PluggableDimmer, + SwitchMeasuring, WiredDimmer3, ) from packaging.version import Version @@ -44,9 +44,12 @@ async def async_setup_entry( hap = config_entry.runtime_data entities: list[HomematicipGenericEntity] = [] for device in hap.home.devices: - if isinstance(device, BrandSwitchMeasuring): + if ( + isinstance(device, SwitchMeasuring) + and getattr(device, "deviceType", None) == DeviceType.BRAND_SWITCH_MEASURING + ): entities.append(HomematicipLightMeasuring(hap, device)) - elif isinstance(device, BrandSwitchNotificationLight): + if isinstance(device, BrandSwitchNotificationLight): device_version = Version(device.firmwareVersion) entities.append(HomematicipLight(hap, device)) diff --git a/homeassistant/components/homematicip_cloud/manifest.json b/homeassistant/components/homematicip_cloud/manifest.json index 15bc24c110f..fc4a1cb831f 100644 --- a/homeassistant/components/homematicip_cloud/manifest.json +++ b/homeassistant/components/homematicip_cloud/manifest.json @@ -6,5 +6,5 @@ "documentation": "https://www.home-assistant.io/integrations/homematicip_cloud", "iot_class": "cloud_push", "loggers": ["homematicip"], - "requirements": ["homematicip==2.0.1.1"] + "requirements": ["homematicip==2.0.4"] } diff --git a/homeassistant/components/homematicip_cloud/sensor.py b/homeassistant/components/homematicip_cloud/sensor.py index 4f43e6d6ca7..13f3694de7a 100644 --- a/homeassistant/components/homematicip_cloud/sensor.py +++ b/homeassistant/components/homematicip_cloud/sensor.py @@ -11,12 +11,10 @@ from homematicip.base.functionalChannels import ( FunctionalChannel, ) from homematicip.device import ( - BrandSwitchMeasuring, EnergySensorsInterface, FloorTerminalBlock6, FloorTerminalBlock10, FloorTerminalBlock12, - FullFlushSwitchMeasuring, HeatingThermostat, HeatingThermostatCompact, HeatingThermostatEvo, @@ -26,9 +24,9 @@ from homematicip.device import ( MotionDetectorOutdoor, MotionDetectorPushButton, PassageDetector, - PlugableSwitchMeasuring, PresenceDetectorIndoor, RoomControlDeviceAnalog, + SwitchMeasuring, TemperatureDifferenceSensor2, TemperatureHumiditySensorDisplay, TemperatureHumiditySensorOutdoor, @@ -143,14 +141,7 @@ async def async_setup_entry( ), ): entities.append(HomematicipIlluminanceSensor(hap, device)) - if isinstance( - device, - ( - PlugableSwitchMeasuring, - BrandSwitchMeasuring, - FullFlushSwitchMeasuring, - ), - ): + if isinstance(device, SwitchMeasuring): entities.append(HomematicipPowerSensor(hap, device)) entities.append(HomematicipEnergySensor(hap, device)) if isinstance(device, (WeatherSensor, WeatherSensorPlus, WeatherSensorPro)): diff --git a/homeassistant/components/homematicip_cloud/switch.py b/homeassistant/components/homematicip_cloud/switch.py index 4927d9a32df..66a40229c7e 100644 --- a/homeassistant/components/homematicip_cloud/switch.py +++ b/homeassistant/components/homematicip_cloud/switch.py @@ -4,20 +4,19 @@ from __future__ import annotations from typing import Any +from homematicip.base.enums import DeviceType from homematicip.device import ( BrandSwitch2, - BrandSwitchMeasuring, DinRailSwitch, DinRailSwitch4, FullFlushInputSwitch, - FullFlushSwitchMeasuring, HeatingSwitch2, MultiIOBox, OpenCollector8Module, PlugableSwitch, - PlugableSwitchMeasuring, PrintedCircuitBoardSwitch2, PrintedCircuitBoardSwitchBattery, + SwitchMeasuring, WiredSwitch8, ) from homematicip.group import ExtendedLinkedSwitchingGroup, SwitchingGroup @@ -43,12 +42,10 @@ async def async_setup_entry( if isinstance(group, (ExtendedLinkedSwitchingGroup, SwitchingGroup)) ] for device in hap.home.devices: - if isinstance(device, BrandSwitchMeasuring): - # BrandSwitchMeasuring inherits PlugableSwitchMeasuring - # This entity is implemented in the light platform and will - # not be added in the switch platform - pass - elif isinstance(device, (PlugableSwitchMeasuring, FullFlushSwitchMeasuring)): + if ( + isinstance(device, SwitchMeasuring) + and getattr(device, "deviceType", None) != DeviceType.BRAND_SWITCH_MEASURING + ): entities.append(HomematicipSwitchMeasuring(hap, device)) elif isinstance(device, WiredSwitch8): entities.extend( diff --git a/requirements_all.txt b/requirements_all.txt index 942ba2322b4..8bfcd164e7a 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -1170,7 +1170,7 @@ home-assistant-frontend==20250531.0 home-assistant-intents==2025.5.28 # homeassistant.components.homematicip_cloud -homematicip==2.0.1.1 +homematicip==2.0.4 # homeassistant.components.horizon horimote==0.4.1 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 6f79a6ff68c..f6923f8bbb1 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1016,7 +1016,7 @@ home-assistant-frontend==20250531.0 home-assistant-intents==2025.5.28 # homeassistant.components.homematicip_cloud -homematicip==2.0.1.1 +homematicip==2.0.4 # homeassistant.components.remember_the_milk httplib2==0.20.4 diff --git a/tests/components/homematicip_cloud/test_hap.py b/tests/components/homematicip_cloud/test_hap.py index 2cd41161dde..a8aab422eb9 100644 --- a/tests/components/homematicip_cloud/test_hap.py +++ b/tests/components/homematicip_cloud/test_hap.py @@ -231,3 +231,23 @@ async def test_auth_create_exception(hass: HomeAssistant, simple_mock_auth) -> N ), ): assert not await hmip_auth.get_auth(hass, HAPID, HAPPIN) + + +async def test_get_state_after_disconnect( + hass: HomeAssistant, hmip_config_entry: MockConfigEntry, simple_mock_home +) -> None: + """Test get state after disconnect.""" + hass.config.components.add(DOMAIN) + hap = HomematicipHAP(hass, hmip_config_entry) + assert hap + + with patch.object(hap, "get_state") as mock_get_state: + assert not hap._ws_connection_closed.is_set() + + await hap.ws_connected_handler() + mock_get_state.assert_not_called() + + await hap.ws_disconnected_handler() + assert hap._ws_connection_closed.is_set() + await hap.ws_connected_handler() + mock_get_state.assert_called_once()