From 9ad10510db620824ae9c42fd4c26be1a883e475e Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Sun, 12 Jan 2025 16:58:32 -0700 Subject: [PATCH 1/4] fix: handle websocket disconnects better --- custom_components/openevse/__init__.py | 15 +++++++++++---- custom_components/openevse/manifest.json | 2 +- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/custom_components/openevse/__init__.py b/custom_components/openevse/__init__.py index 1754a80..cc4e516 100644 --- a/custom_components/openevse/__init__.py +++ b/custom_components/openevse/__init__.py @@ -181,9 +181,6 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b ) await coordinator.async_refresh() - # Start the websocket listener - manager.ws_start() - await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS) services = OpenEVSEServices(hass, config_entry) @@ -321,7 +318,17 @@ async def update_sensors(self) -> dict: "Error updating sensors [%s]: %s", type(error).__name__, error ) raise UpdateFailed(error) from error - + + if self._manager.websocket.state != "connected": + _LOGGER.info("Connecting to websocket...") + try: + await self._manager.ws_start() + except RuntimeError: + pass + except Exception as error: + _LOGGER.debug("Error connecting to websocket [%s]: %s", type(error).__name__, error) + raise UpdateFailed(error) from error + self.parse_sensors() return self._data diff --git a/custom_components/openevse/manifest.json b/custom_components/openevse/manifest.json index ba5b0c4..297b749 100644 --- a/custom_components/openevse/manifest.json +++ b/custom_components/openevse/manifest.json @@ -9,7 +9,7 @@ "iot_class": "local_push", "issue_tracker": "https://github.com/firstof9/openevse/issues", "loggers": ["openevsehttp"], - "requirements": ["python-openevse-http==0.1.63"], + "requirements": ["python_openevse_http==0.1.70"], "version": "0.0.0-dev", "zeroconf": ["_openevse._tcp.local."] } From a1a457a0810f9ff5e986ea2ebec885178e0dea98 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Sun, 12 Jan 2025 17:03:43 -0700 Subject: [PATCH 2/4] formatting --- custom_components/openevse/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/openevse/__init__.py b/custom_components/openevse/__init__.py index cc4e516..e378e83 100644 --- a/custom_components/openevse/__init__.py +++ b/custom_components/openevse/__init__.py @@ -318,7 +318,7 @@ async def update_sensors(self) -> dict: "Error updating sensors [%s]: %s", type(error).__name__, error ) raise UpdateFailed(error) from error - + if self._manager.websocket.state != "connected": _LOGGER.info("Connecting to websocket...") try: @@ -326,9 +326,13 @@ async def update_sensors(self) -> dict: except RuntimeError: pass except Exception as error: - _LOGGER.debug("Error connecting to websocket [%s]: %s", type(error).__name__, error) + _LOGGER.debug( + "Error connecting to websocket [%s]: %s", + type(error).__name__, + error, + ) raise UpdateFailed(error) from error - + self.parse_sensors() return self._data From 80ad698432da8baad8708d34c9b0f316371bcc40 Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Sun, 12 Jan 2025 18:33:27 -0700 Subject: [PATCH 3/4] fix ws_start() call and fix some tests --- custom_components/openevse/__init__.py | 3 ++- tests/conftest.py | 6 +++--- tests/test_services.py | 4 ++-- 3 files changed, 7 insertions(+), 6 deletions(-) diff --git a/custom_components/openevse/__init__.py b/custom_components/openevse/__init__.py index e378e83..5d635ed 100644 --- a/custom_components/openevse/__init__.py +++ b/custom_components/openevse/__init__.py @@ -319,10 +319,11 @@ async def update_sensors(self) -> dict: ) raise UpdateFailed(error) from error + assert self._manager.websocket if self._manager.websocket.state != "connected": _LOGGER.info("Connecting to websocket...") try: - await self._manager.ws_start() + self._manager.ws_start() except RuntimeError: pass except Exception as error: diff --git a/tests/conftest.py b/tests/conftest.py index 5aa374b..161ec34 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -96,7 +96,7 @@ def test_charger(mock_aioclient): ) mock_aioclient.get( TEST_URL_WS, - status=200, + status=101, body=load_fixture("status.json"), repeat=True, ) @@ -132,7 +132,7 @@ def test_charger_bad_serial(mock_aioclient): ) mock_aioclient.get( TEST_URL_WS, - status=200, + status=101, body=load_fixture("status.json"), repeat=True, ) @@ -167,7 +167,7 @@ def test_charger_bad_post(mock_aioclient): ) mock_aioclient.get( TEST_URL_WS, - status=200, + status=101, body=load_fixture("status.json"), repeat=True, ) diff --git a/tests/test_services.py b/tests/test_services.py index e122ff1..da3e31d 100644 --- a/tests/test_services.py +++ b/tests/test_services.py @@ -108,7 +108,7 @@ async def test_make_claim( data=CONFIG_DATA, ) mock_aioclient.post( - f"{TEST_URL_CLAIMS}/4", + f"{TEST_URL_CLAIMS}/20", status=200, body='[{"msg":"done"}]', repeat=True, @@ -150,7 +150,7 @@ async def test_release_claim( data=CONFIG_DATA, ) mock_aioclient.delete( - f"{TEST_URL_CLAIMS}/4", + f"{TEST_URL_CLAIMS}/20", status=200, body='[{"msg":"done"}]', repeat=True, From e34802a10fc27e3822278459caa9ef98a9c1a9eb Mon Sep 17 00:00:00 2001 From: "firstof9@gmail.com" Date: Wed, 15 Jan 2025 13:32:46 -0700 Subject: [PATCH 4/4] catch assertion error if websocket isn't setup --- custom_components/openevse/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/custom_components/openevse/__init__.py b/custom_components/openevse/__init__.py index e6e4c1f..5615c43 100644 --- a/custom_components/openevse/__init__.py +++ b/custom_components/openevse/__init__.py @@ -320,7 +320,12 @@ async def update_sensors(self) -> dict: ) raise UpdateFailed(error) from error - assert self._manager.websocket + try: + assert self._manager.websocket + except AssertionError as error: + _LOGGER.debug("Websocket not setup.") + raise UpdateFailed(error) from error + if self._manager.websocket.state != "connected": _LOGGER.info("Connecting to websocket...") try: