Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle websocket disconnects better #420

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions custom_components/openevse/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,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)
Expand Down Expand Up @@ -323,6 +320,26 @@ async def update_sensors(self) -> dict:
)
raise UpdateFailed(error) from error

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:
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()
await self.async_parse_sensors()
_LOGGER.debug("Coordinator data: %s", self._data)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/openevse/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"iot_class": "local_push",
"issue_tracker": "https://github.com/firstof9/openevse/issues",
"loggers": ["openevsehttp"],
"requirements": ["python-openevse-http==0.1.65"],
"requirements": ["python_openevse_http==0.1.70"],
"version": "0.0.0-dev",
"zeroconf": ["_openevse._tcp.local."]
}
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def test_charger(mock_aioclient):
)
mock_aioclient.get(
TEST_URL_WS,
status=200,
status=101,
body=load_fixture("status.json"),
repeat=True,
)
Expand Down Expand Up @@ -140,7 +140,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,
)
Expand Down Expand Up @@ -181,7 +181,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,
)
Expand Down
4 changes: 2 additions & 2 deletions tests/test_services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
Loading