Skip to content

Commit

Permalink
trigger device update callbacks when connection state changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Christopher Hoch committed Dec 19, 2023
1 parent e1547ea commit 22020ff
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
5 changes: 4 additions & 1 deletion pyvlx/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,13 @@ def connection_lost(self, exc: object) -> None:
class Connection:
"""Class for handling TCP connection."""

def __init__(self, loop: asyncio.AbstractEventLoop, config: Config):
def __init__(self, loop: asyncio.AbstractEventLoop, config: Config, connection_closed_cb: CallbackType = None):
"""Init TCP connection."""
self.loop = loop
self.config = config
self.transport: Optional[asyncio.Transport] = None
self.frame_received_cbs: List[CallbackType] = []
self.connection_closed_cb: CallbackType = None
self.connected = False
self.connection_counter = 0

Expand All @@ -95,6 +96,8 @@ def disconnect(self) -> None:
self.transport.close()
self.transport = None
self.connected = False
if self.connection_closed_cb is not None:
self.loop.create_task(self.connection_closed_cb())

async def connect(self) -> None:
"""Connect to gateway via SSL."""
Expand Down
10 changes: 9 additions & 1 deletion pyvlx/pyvlx.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def __init__(
"""Initialize PyVLX class."""
self.loop = loop or asyncio.get_event_loop()
self.config = Config(self, path, host, password)
self.connection = Connection(loop=self.loop, config=self.config)
self.connection = Connection(loop=self.loop, config=self.config, connection_closed_cb=self.connection_closed_cb)
self.heartbeat = Heartbeat(
pyvlx=self,
interval=heartbeat_interval,
Expand Down Expand Up @@ -72,6 +72,9 @@ async def connect(self) -> None:
await self.klf200.house_status_monitor_enable(pyvlx=self)
self.heartbeat.start()

for node in self.nodes:
await self.loop.create_task(node.after_update())

async def reboot_gateway(self) -> None:
"""For Compatibility: Reboot the KLF 200."""
if not self.is_connected():
Expand Down Expand Up @@ -116,3 +119,8 @@ async def get_limitation(self, node_id: int) -> None:
"""Return limitation."""
limit = get_limitation.GetLimitation(self, node_id)
await limit.do_api_call()

async def connection_closed_cb(self) -> None:
"""Callback when connection to KLF 200 is closed."""
for node in self.nodes:
await self.loop.create_task(node.after_update())

0 comments on commit 22020ff

Please sign in to comment.