33The HAPServer is the point of contact to and from the world.
44"""
55
6+ import asyncio
67import logging
78import time
9+ from typing import TYPE_CHECKING , Dict , Optional , Tuple
810
911from .hap_protocol import HAPServerProtocol
1012from .util import callback
1113
14+ if TYPE_CHECKING :
15+ from .accessory_driver import AccessoryDriver
16+
1217logger = logging .getLogger (__name__ )
1318
1419IDLE_CONNECTION_CHECK_INTERVAL_SECONDS = 120
@@ -28,17 +33,18 @@ class HAPServer:
2833 implements exclusive access to the send methods.
2934 """
3035
31- def __init__ (self , addr_port , accessory_handler ):
36+ def __init__ (
37+ self , addr_port : Tuple [str , int ], accessory_handler : "AccessoryDriver"
38+ ) -> None :
3239 """Create a HAP Server."""
3340 self ._addr_port = addr_port
34- self .connections = {} # (address, port): socket
41+ self .connections : Dict [ Tuple [ str , int ], HAPServerProtocol ] = {}
3542 self .accessory_handler = accessory_handler
36- self .server = None
37- self ._serve_task = None
38- self ._connection_cleanup = None
39- self .loop = None
43+ self .server : Optional [asyncio .Server ] = None
44+ self ._connection_cleanup : Optional [asyncio .TimerHandle ] = None
45+ self .loop : Optional [asyncio .AbstractEventLoop ] = None
4046
41- async def async_start (self , loop ) :
47+ async def async_start (self , loop : asyncio . AbstractEventLoop ) -> None :
4248 """Start the http-hap server."""
4349 self .loop = loop
4450 self .server = await loop .create_server (
@@ -49,7 +55,7 @@ async def async_start(self, loop):
4955 self .async_cleanup_connections ()
5056
5157 @callback
52- def async_cleanup_connections (self ):
58+ def async_cleanup_connections (self ) -> None :
5359 """Cleanup stale connections."""
5460 now = time .time ()
5561 for hap_proto in list (self .connections .values ()):
@@ -59,7 +65,7 @@ def async_cleanup_connections(self):
5965 )
6066
6167 @callback
62- def async_stop (self ):
68+ def async_stop (self ) -> None :
6369 """Stop the server.
6470
6571 This method must be run in the event loop.
@@ -70,10 +76,12 @@ def async_stop(self):
7076 self .server .close ()
7177 self .connections .clear ()
7278
73- def push_event (self , data , client_addr , immediate = False ):
79+ def push_event (
80+ self , data : bytes , client_addr : Tuple [str , int ], immediate : bool = False
81+ ) -> bool :
7482 """Queue an event to the current connection with the provided data.
7583
76- :param data: The charateristic changes
84+ :param data: The characteristic changes
7785 :type data: dict
7886
7987 :param client_addr: A client (address, port) tuple to which to send the data.
0 commit comments