From 5433e49df3e735181e5c20e86ebe09cde6a16c13 Mon Sep 17 00:00:00 2001 From: Aaron Godfrey Date: Sun, 19 Dec 2021 15:44:54 -0800 Subject: [PATCH] Skip update if kodi is not connected. --- .../kodi_recently_added/entities.py | 4 ++++ tests/test_entities.py | 21 +++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/custom_components/kodi_recently_added/entities.py b/custom_components/kodi_recently_added/entities.py index fc0be78..77d17a3 100644 --- a/custom_components/kodi_recently_added/entities.py +++ b/custom_components/kodi_recently_added/entities.py @@ -40,6 +40,10 @@ def state(self) -> Optional[str]: return self._state async def async_update(self) -> None: + if not self.kodi._conn.connected: + _LOGGER.debug("Kodi is not connected, skipping update.") + return + result = None try: result = await self.kodi.call_method( diff --git a/tests/test_entities.py b/tests/test_entities.py index ed157c6..942832b 100644 --- a/tests/test_entities.py +++ b/tests/test_entities.py @@ -1,6 +1,8 @@ """Tests for entities.py.""" from unittest import mock +import pykodi + from custom_components.kodi_recently_added.entities import KodiMediaEntity @@ -74,3 +76,22 @@ def test_get_web_url_non_http(): path = "nfs://127.0.0.2/volume1/image.png" expected = "http://username:password@127.0.0.1:8080/image/image%3A%2F%2Fnfs%253A%252F%252F127.0.0.2%252Fvolume1%252Fimage.png" assert expected == entity.get_web_url(path) + + +@mock.patch("custom_components.kodi_recently_added.entities._LOGGER") +async def test_async_update_skips_if_not_connected(logger): + """Test we skip the update if kodi is not connected.""" + config = { + "host": "127.0.0.1", + "password": "password", + "port": 8080, + "ssl": False, + "username": "username", + } + kodi = mock.Mock(spec=pykodi.Kodi) + kodi._conn = mock.Mock(connected=False) + entity = KodiMediaEntity(kodi, config) + await entity.async_update() + assert kodi.call_method.called is False + expected_call = mock.call("Kodi is not connected, skipping update.") + assert expected_call == logger.debug.call_args