diff --git a/homeassistant/components/onvif/camera.py b/homeassistant/components/onvif/camera.py index 4d39c95c3cd..570b99bfe3a 100644 --- a/homeassistant/components/onvif/camera.py +++ b/homeassistant/components/onvif/camera.py @@ -106,11 +106,6 @@ class ONVIFCameraEntity(ONVIFBaseEntity, Camera): async def stream_source(self): """Return the stream source.""" - if self._stream_uri is None: - uri_no_auth = await self.device.async_get_stream_uri(self.profile) - self._stream_uri = uri_no_auth.replace( - "rtsp://", f"rtsp://{self.device.username}:{self.device.password}@", 1 - ) return self._stream_uri async def async_camera_image(self): @@ -118,11 +113,6 @@ class ONVIFCameraEntity(ONVIFBaseEntity, Camera): image = None if self.device.capabilities.snapshot: - if self._snapshot_uri is None: - self._snapshot_uri = await self.device.async_get_snapshot_uri( - self.profile - ) - auth = None if self.device.username and self.device.password: auth = HTTPDigestAuth(self.device.username, self.device.password) @@ -181,6 +171,16 @@ class ONVIFCameraEntity(ONVIFBaseEntity, Camera): finally: await stream.close() + async def async_added_to_hass(self): + """Run when entity about to be added to hass.""" + uri_no_auth = await self.device.async_get_stream_uri(self.profile) + self._stream_uri = uri_no_auth.replace( + "rtsp://", f"rtsp://{self.device.username}:{self.device.password}@", 1 + ) + + if self.device.capabilities.snapshot: + self._snapshot_uri = await self.device.async_get_snapshot_uri(self.profile) + async def async_perform_ptz( self, distance, diff --git a/homeassistant/components/onvif/device.py b/homeassistant/components/onvif/device.py index 8e69e148da3..0a35dadec26 100644 --- a/homeassistant/components/onvif/device.py +++ b/homeassistant/components/onvif/device.py @@ -223,7 +223,7 @@ class ONVIFDevice: try: media_service = self.device.create_media_service() media_capabilities = await media_service.GetServiceCapabilities() - snapshot = media_capabilities.SnapshotUri + snapshot = media_capabilities and media_capabilities.SnapshotUri except (ONVIFError, Fault): pass @@ -231,7 +231,7 @@ class ONVIFDevice: try: event_service = self.device.create_events_service() event_capabilities = await event_service.GetServiceCapabilities() - pullpoint = event_capabilities.WSPullPointSupport + pullpoint = event_capabilities and event_capabilities.WSPullPointSupport except (ONVIFError, Fault): pass