Skip to content

Commit

Permalink
Make entities unavailable on disconnect. Fix #407
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Sep 6, 2022
1 parent 76ff3d7 commit 8898303
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 4 deletions.
6 changes: 4 additions & 2 deletions custom_components/browser_mod/browser.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,15 +172,17 @@ def connection(self):
"""The current websocket connections for this Browser."""
return self._connections

def open_connection(self, connection, cid):
def open_connection(self, hass, connection, cid):
"""Add a websocket connection."""
self._connections.append((connection, cid))
self.update(hass, {"connected": True})

def close_connection(self, connection):
def close_connection(self, hass, connection):
"""Close a websocket connection."""
self._connections = list(
filter(lambda v: v[0] != connection, self._connections)
)
self.update(hass, {"connected": False})


def getBrowser(hass, browserID, *, create=True):
Expand Down
4 changes: 2 additions & 2 deletions custom_components/browser_mod/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def close_connection():
store_listener()
dev = getBrowser(hass, browserID, create=False)
if dev:
dev.close_connection(connection)
dev.close_connection(hass, connection)

connection.subscriptions[msg["id"]] = close_connection
connection.send_result(msg["id"])

if store.get_browser(browserID).registered:
dev = getBrowser(hass, browserID)
dev.update_settings(hass, store.get_browser(browserID).asdict())
dev.open_connection(connection, msg["id"])
dev.open_connection(hass, connection, msg["id"])
await store.set_browser(
browserID,
last_seen=datetime.now(tz=timezone.utc).isoformat(),
Expand Down
4 changes: 4 additions & 0 deletions custom_components/browser_mod/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ def extra_state_attributes(self):
"browserID": self.browserID,
}

@property
def available(self):
return self._data.get("connected", False)

@property
def name(self):
return self._name
Expand Down

0 comments on commit 8898303

Please sign in to comment.