Skip to content

Commit a7e73bc

Browse files
committed
update function starts websocket listener
1 parent 2a40842 commit a7e73bc

File tree

2 files changed

+57
-19
lines changed

2 files changed

+57
-19
lines changed

openevsehttp/__init__.py

Lines changed: 56 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,9 @@ def __init__(
5656
"""Initialize a OpenEVSEWebsocket instance."""
5757
self.session = aiohttp.ClientSession()
5858
self.uri = self._get_uri(server)
59-
self._user = (user,)
60-
self._password = (password,)
59+
self._user = user
60+
self._password = password
6161
self.callback = callback
62-
self.subscriptions = ["message"]
6362
self._state = None
6463
self.failed_attempts = 0
6564
self._error_reason = None
@@ -105,14 +104,8 @@ async def running(self):
105104

106105
if message.type == aiohttp.WSMsgType.TEXT:
107106
msg = message.json()
108-
msgtype = msg["type"]
109-
110-
if msgtype in self.subscriptions:
111-
self.callback(msgtype, msg, None)
112-
113-
else:
114-
_LOGGER.debug("Ignoring: %s", msg)
115-
continue
107+
msgtype = "data"
108+
self.callback(msgtype, msg, None)
116109

117110
elif message.type == aiohttp.WSMsgType.CLOSED:
118111
_LOGGER.warning("Websocket connection closed")
@@ -176,6 +169,7 @@ def __init__(self, host: str, user: str = None, pwd: str = None) -> None:
176169
self._status = None
177170
self._config = None
178171
self._override = None
172+
self._ws_listening = False
179173

180174
def send_command(self, command: str) -> tuple | None:
181175
"""Send a RAPI command to the charger and parses the response."""
@@ -202,9 +196,33 @@ def send_command(self, command: str) -> tuple | None:
202196

203197
def update(self) -> None:
204198
"""Update the values."""
205-
urls = [f"{self.url}/status", f"{self.url}/config"]
206-
207-
for url in urls:
199+
if not self._ws_listening:
200+
urls = [f"{self.url}/status", f"{self.url}/config"]
201+
202+
for url in urls:
203+
_LOGGER.debug("Updating data from %s", url)
204+
if self._user is not None:
205+
value = requests.get(url, auth=(self._user, self._pwd))
206+
else:
207+
value = requests.get(url)
208+
209+
if value.status_code == 401:
210+
_LOGGER.debug("Authentication error: %s", value)
211+
raise AuthenticationError
212+
213+
if "/status" in url:
214+
self._status = value.json()
215+
else:
216+
self._config = value.json()
217+
218+
# Start Websocket listening
219+
websocket = OpenEVSEWebsocket(
220+
self.url, self._update_status, self._user, self._pwd
221+
)
222+
websocket.listen()
223+
self._ws_listening = True
224+
else:
225+
url = f"{self.url}/config"
208226
_LOGGER.debug("Updating data from %s", url)
209227
if self._user is not None:
210228
value = requests.get(url, auth=(self._user, self._pwd))
@@ -215,10 +233,30 @@ def update(self) -> None:
215233
_LOGGER.debug("Authentication error: %s", value)
216234
raise AuthenticationError
217235

218-
if "/status" in url:
219-
self._status = value.json()
220-
else:
221-
self._config = value.json()
236+
self._config = value.json()
237+
238+
def _update_status(self, msgtype, data, error) -> None:
239+
"""Update data from websocket listener."""
240+
if msgtype == SIGNAL_CONNECTION_STATE:
241+
if data == STATE_CONNECTED:
242+
_LOGGER.debug("Websocket to %s successful", self.url)
243+
elif data == STATE_DISCONNECTED:
244+
_LOGGER.debug(
245+
"Websocket to %s disconnected, retrying",
246+
self.url,
247+
)
248+
self._ws_listening = False
249+
# Stopped websockets without errors are expected during shutdown and ignored
250+
elif data == STATE_STOPPED and error:
251+
_LOGGER.error(
252+
"Websocket to %s failed, aborting [Error: %s]",
253+
self.url,
254+
error,
255+
)
256+
self._ws_listening = False
257+
258+
elif msgtype == "data":
259+
self._status = data.json()
222260

223261
def get_override(self) -> None:
224262
"""Get the manual override status."""

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
PROJECT_DIR = Path(__file__).parent.resolve()
77
README_FILE = PROJECT_DIR / "README.md"
8-
VERSION = "0.1.8-2"
8+
VERSION = "0.1.8-3"
99

1010

1111
setup(

0 commit comments

Comments
 (0)