diff --git a/homeassistant/components/onvif/config_flow.py b/homeassistant/components/onvif/config_flow.py index ceb861fc7dde0..98b5543ad5134 100644 --- a/homeassistant/components/onvif/config_flow.py +++ b/homeassistant/components/onvif/config_flow.py @@ -1,4 +1,5 @@ """Config flow for ONVIF.""" +import asyncio from pprint import pformat from typing import List from urllib.parse import urlparse @@ -191,13 +192,12 @@ async def async_step_profiles(self, user_input=None): self.onvif_config[CONF_USERNAME], self.onvif_config[CONF_PASSWORD], ) - await device.update_xaddrs() - try: # Get the MAC address to use as the unique ID for the config flow if not self.device_id: devicemgmt = device.create_devicemgmt_service() + await asyncio.sleep(0.1) network_interfaces = await devicemgmt.GetNetworkInterfaces() for interface in network_interfaces: if interface.Enabled: @@ -217,6 +217,7 @@ async def async_step_profiles(self, user_input=None): # Verify there is an H264 profile media_service = device.create_media_service() + await asyncio.sleep(0.1) profiles = await media_service.GetProfiles() h264 = any( profile.VideoEncoderConfiguration.Encoding == "H264" diff --git a/homeassistant/components/onvif/device.py b/homeassistant/components/onvif/device.py index c15f4bf687712..8f594571000c4 100644 --- a/homeassistant/components/onvif/device.py +++ b/homeassistant/components/onvif/device.py @@ -4,7 +4,11 @@ import os from typing import List -from aiohttp.client_exceptions import ClientConnectionError, ServerDisconnectedError +from aiohttp.client_exceptions import ( + ClientConnectionError, + ClientPayloadError, + ServerDisconnectedError, +) import onvif from onvif import ONVIFCamera from onvif.exceptions import ONVIFError @@ -134,6 +138,7 @@ async def async_check_date_and_time(self) -> None: LOGGER.debug("Setting up the ONVIF device management service") devicemgmt = self.device.create_devicemgmt_service() + await asyncio.sleep(0.1) LOGGER.debug("Retrieving current device date/time") try: system_date = dt_util.utcnow() @@ -197,6 +202,7 @@ async def async_check_date_and_time(self) -> None: async def async_get_device_info(self) -> DeviceInfo: """Obtain information about this device.""" devicemgmt = self.device.create_devicemgmt_service() + await asyncio.sleep(0.1) device_info = await devicemgmt.GetDeviceInformation() return DeviceInfo( device_info.Manufacturer, @@ -210,6 +216,7 @@ async def async_get_capabilities(self): snapshot = False try: media_service = self.device.create_media_service() + await asyncio.sleep(0.1) media_capabilities = await media_service.GetServiceCapabilities() snapshot = media_capabilities.SnapshotUri except (ONVIFError, Fault): @@ -218,6 +225,7 @@ async def async_get_capabilities(self): pullpoint = False try: event_service = self.device.create_events_service() + await asyncio.sleep(0.1) event_capabilities = await event_service.GetServiceCapabilities() pullpoint = event_capabilities.WSPullPointSupport except (ONVIFError, Fault): @@ -235,6 +243,7 @@ async def async_get_capabilities(self): async def async_get_profiles(self) -> List[Profile]: """Obtain media profiles for this device.""" media_service = self.device.create_media_service() + await asyncio.sleep(0.1) result = await media_service.GetProfiles() profiles = [] for key, onvif_profile in enumerate(result): @@ -266,10 +275,15 @@ async def async_get_profiles(self) -> List[Profile]: is not None, ) - ptz_service = self.device.get_service("ptz") - presets = await ptz_service.GetPresets(profile.token) - profile.ptz.presets = [preset.token for preset in presets] - + try: + ptz_service = self.device.get_service("ptz") + await asyncio.sleep(0.1) + presets = await ptz_service.GetPresets(profile.token) + profile.ptz.presets = [preset.token for preset in presets] + except ClientPayloadError as err: + LOGGER.warning( + "Couldn't get PTZ presets for: %s Error: %s", self.name, err, + ) profiles.append(profile) return profiles