From cb0452d80edc7cf970982cc647f7db7dab73027f Mon Sep 17 00:00:00 2001 From: cgtobi Date: Tue, 15 Sep 2020 00:32:20 +0200 Subject: [PATCH] Fix netatmo media browser of outdoor events (#40079) * Fix outdoor events * Fix test data * Increase coverage --- homeassistant/components/netatmo/camera.py | 6 +-- .../components/netatmo/media_source.py | 16 ++++++- tests/components/netatmo/test_media_source.py | 42 ++++++++++++++++++- 3 files changed, 58 insertions(+), 6 deletions(-) diff --git a/homeassistant/components/netatmo/camera.py b/homeassistant/components/netatmo/camera.py index 3f9720f3adb..dff6013c7c6 100644 --- a/homeassistant/components/netatmo/camera.py +++ b/homeassistant/components/netatmo/camera.py @@ -284,9 +284,9 @@ class NetatmoCamera(NetatmoBase, Camera): self._data.events.get(self._id, {}) ) elif self._model == "NOC": # Smart Outdoor Camera - self.hass.data[DOMAIN][DATA_EVENTS][ - self._id - ] = self._data.outdoor_events.get(self._id, {}) + self.hass.data[DOMAIN][DATA_EVENTS][self._id] = self.process_events( + self._data.outdoor_events.get(self._id, {}) + ) def process_events(self, events): """Add meta data to events.""" diff --git a/homeassistant/components/netatmo/media_source.py b/homeassistant/components/netatmo/media_source.py index 76527677224..6375c46d394 100644 --- a/homeassistant/components/netatmo/media_source.py +++ b/homeassistant/components/netatmo/media_source.py @@ -80,8 +80,20 @@ class NetatmoSource(MediaSource): ) -> BrowseMediaSource: if event_id and event_id in self.events[camera_id]: created = dt.datetime.fromtimestamp(event_id) - thumbnail = self.events[camera_id][event_id].get("snapshot", {}).get("url") - message = remove_html_tags(self.events[camera_id][event_id]["message"]) + if self.events[camera_id][event_id]["type"] == "outdoor": + thumbnail = ( + self.events[camera_id][event_id]["event_list"][0] + .get("snapshot", {}) + .get("url") + ) + message = remove_html_tags( + self.events[camera_id][event_id]["event_list"][0]["message"] + ) + else: + thumbnail = ( + self.events[camera_id][event_id].get("snapshot", {}).get("url") + ) + message = remove_html_tags(self.events[camera_id][event_id]["message"]) title = f"{created} - {message}" else: title = self.hass.data[DOMAIN][DATA_CAMERAS].get(camera_id, MANUFACTURER) diff --git a/tests/components/netatmo/test_media_source.py b/tests/components/netatmo/test_media_source.py index 0405317f03e..1773c0d83da 100644 --- a/tests/components/netatmo/test_media_source.py +++ b/tests/components/netatmo/test_media_source.py @@ -18,6 +18,7 @@ async def test_async_browse_media(hass): "12:34:56:78:90:ab": { 1599152672: { "id": "12345", + "type": "person", "time": 1599152672, "camera_id": "12:34:56:78:90:ab", "snapshot": { @@ -30,6 +31,7 @@ async def test_async_browse_media(hass): }, 1599152673: { "id": "12346", + "type": "person", "time": 1599152673, "camera_id": "12:34:56:78:90:ab", "snapshot": { @@ -37,9 +39,47 @@ async def test_async_browse_media(hass): }, "message": "Tobias seen", }, + 1599152674: { + "id": "12347", + "type": "outdoor", + "time": 1599152674, + "camera_id": "12:34:56:78:90:ac", + "snapshot": { + "url": "https://netatmocameraimage", + }, + "video_id": "98766", + "video_status": "available", + "event_list": [ + { + "type": "vehicle", + "time": 1599152674, + "id": "12347-0", + "offset": 0, + "message": "Vehicle detected", + "snapshot": { + "url": "https://netatmocameraimage", + }, + }, + { + "type": "human", + "time": 1599152674, + "id": "12347-1", + "offset": 8, + "message": "Person detected", + "snapshot": { + "url": "https://netatmocameraimage", + }, + }, + ], + "media_url": "http:///files/high/index.m3u8", + }, } } - hass.data[DOMAIN][DATA_CAMERAS] = {"12:34:56:78:90:ab": "MyCamera"} + + hass.data[DOMAIN][DATA_CAMERAS] = { + "12:34:56:78:90:ab": "MyCamera", + "12:34:56:78:90:ac": "MyOutdoorCamera", + } assert await async_setup_component(hass, const.DOMAIN, {}) await hass.async_block_till_done()