From afe15a2e6bfd41fc081bcb89123c87a866ffd839 Mon Sep 17 00:00:00 2001 From: Paulus Schoutsen Date: Thu, 23 Apr 2020 15:52:36 -0700 Subject: [PATCH] Fix UVC doing I/O in event loop (#34610) --- homeassistant/components/uvc/camera.py | 29 ++++++++++++++++---------- tests/components/uvc/test_camera.py | 1 + 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/homeassistant/components/uvc/camera.py b/homeassistant/components/uvc/camera.py index 431785f7c6a..05937cc3ee9 100644 --- a/homeassistant/components/uvc/camera.py +++ b/homeassistant/components/uvc/camera.py @@ -66,7 +66,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None): [ UnifiVideoCamera(nvrconn, camera[identifier], camera["name"], password) for camera in cameras - ] + ], + True, ) return True @@ -85,17 +86,22 @@ class UnifiVideoCamera(Camera): self._connect_addr = None self._camera = None self._motion_status = False + self._caminfo = None @property def name(self): """Return the name of this camera.""" return self._name + @property + def should_poll(self): + """If this entity should be polled.""" + return True + @property def supported_features(self): """Return supported features.""" - caminfo = self._nvr.get_camera(self._uuid) - channels = caminfo["channels"] + channels = self._caminfo["channels"] for channel in channels: if channel["isRtspEnabled"]: return SUPPORT_STREAM @@ -105,14 +111,12 @@ class UnifiVideoCamera(Camera): @property def is_recording(self): """Return true if the camera is recording.""" - caminfo = self._nvr.get_camera(self._uuid) - return caminfo["recordingSettings"]["fullTimeRecordEnabled"] + return self._caminfo["recordingSettings"]["fullTimeRecordEnabled"] @property def motion_detection_enabled(self): """Camera Motion Detection Status.""" - caminfo = self._nvr.get_camera(self._uuid) - return caminfo["recordingSettings"]["motionRecordEnabled"] + return self._caminfo["recordingSettings"]["motionRecordEnabled"] @property def brand(self): @@ -122,13 +126,11 @@ class UnifiVideoCamera(Camera): @property def model(self): """Return the model of this camera.""" - caminfo = self._nvr.get_camera(self._uuid) - return caminfo["model"] + return self._caminfo["model"] def _login(self): """Login to the camera.""" - - caminfo = self._nvr.get_camera(self._uuid) + caminfo = self._caminfo if self._connect_addr: addrs = [self._connect_addr] else: @@ -164,6 +166,7 @@ class UnifiVideoCamera(Camera): return None self._camera = camera + self._caminfo = caminfo return True def camera_image(self): @@ -219,3 +222,7 @@ class UnifiVideoCamera(Camera): return channel["rtspUris"][0] return None + + def update(self): + """Update the info.""" + self._caminfo = self._nvr.get_camera(self._uuid) diff --git a/tests/components/uvc/test_camera.py b/tests/components/uvc/test_camera.py index 2ca7fdf46e3..2b1e9c7782e 100644 --- a/tests/components/uvc/test_camera.py +++ b/tests/components/uvc/test_camera.py @@ -213,6 +213,7 @@ class TestUVC(unittest.TestCase): ], } self.nvr.server_version = (3, 2, 0) + self.uvc.update() def test_properties(self): """Test the properties."""