Make homekit_controller a local push integration (#32213)
This commit is contained in:
5 files changed
+89
-2
No files matched your search
@@ -46,10 +46,12 @@ class HomeKitEntity(Entity):
|
||||
)
|
||||
|
||||
self._accessory.add_pollable_characteristics(self.pollable_characteristics)
|
||||
self._accessory.add_watchable_characteristics(self.watchable_characteristics)
|
||||
|
||||
async def async_will_remove_from_hass(self):
|
||||
"""Prepare to be removed from hass."""
|
||||
self._accessory.remove_pollable_characteristics(self._aid)
|
||||
self._accessory.remove_watchable_characteristics(self._aid)
|
||||
|
||||
for signal_remove in self._signals:
|
||||
signal_remove()
|
||||
@@ -71,6 +73,7 @@ class HomeKitEntity(Entity):
|
||||
characteristic_types = [get_uuid(c) for c in self.get_characteristic_types()]
|
||||
|
||||
self.pollable_characteristics = []
|
||||
self.watchable_characteristics = []
|
||||
self._chars = {}
|
||||
self._char_names = {}
|
||||
|
||||
@@ -98,6 +101,10 @@ class HomeKitEntity(Entity):
|
||||
if "pr" in char["perms"]:
|
||||
self.pollable_characteristics.append((self._aid, char["iid"]))
|
||||
|
||||
# Build up a list of (aid, iid) tuples to subscribe to
|
||||
if "ev" in char["perms"]:
|
||||
self.watchable_characteristics.append((self._aid, char["iid"]))
|
||||
|
||||
# Build a map of ctype -> iid
|
||||
short_name = CharacteristicsTypes.get_short(char["type"])
|
||||
self._chars[short_name] = char["iid"]
|
||||
|
||||
@@ -81,7 +81,7 @@ class HomekitControllerFlowHandler(config_entries.ConfigFlow):
|
||||
"""Handle a HomeKit config flow."""
|
||||
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL
|
||||
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_PUSH
|
||||
|
||||
def __init__(self):
|
||||
"""Initialize the homekit_controller flow."""
|
||||
|
||||
@@ -108,6 +108,10 @@ class HKDevice:
|
||||
self._polling_lock = asyncio.Lock()
|
||||
self._polling_lock_warned = False
|
||||
|
||||
self.watchable_characteristics = []
|
||||
|
||||
self.pairing.dispatcher_connect(self.process_new_events)
|
||||
|
||||
def add_pollable_characteristics(self, characteristics):
|
||||
"""Add (aid, iid) pairs that we need to poll."""
|
||||
self.pollable_characteristics.extend(characteristics)
|
||||
@@ -118,6 +122,17 @@ class HKDevice:
|
||||
char for char in self.pollable_characteristics if char[0] != accessory_id
|
||||
]
|
||||
|
||||
def add_watchable_characteristics(self, characteristics):
|
||||
"""Add (aid, iid) pairs that we need to poll."""
|
||||
self.watchable_characteristics.extend(characteristics)
|
||||
self.hass.add_job(self.pairing.subscribe(characteristics))
|
||||
|
||||
def remove_watchable_characteristics(self, accessory_id):
|
||||
"""Remove all pollable characteristics by accessory id."""
|
||||
self.watchable_characteristics = [
|
||||
char for char in self.watchable_characteristics if char[0] != accessory_id
|
||||
]
|
||||
|
||||
@callback
|
||||
def async_set_unavailable(self):
|
||||
"""Mark state of all entities on this connection as unavailable."""
|
||||
@@ -163,6 +178,9 @@ class HKDevice:
|
||||
|
||||
self.add_entities()
|
||||
|
||||
if self.watchable_characteristics:
|
||||
await self.pairing.subscribe(self.watchable_characteristics)
|
||||
|
||||
await self.async_update()
|
||||
|
||||
return True
|
||||
@@ -172,6 +190,8 @@ class HKDevice:
|
||||
if self._polling_interval_remover:
|
||||
self._polling_interval_remover()
|
||||
|
||||
await self.pairing.unsubscribe(self.watchable_characteristics)
|
||||
|
||||
unloads = []
|
||||
for platform in self.platforms:
|
||||
unloads.append(
|
||||
|
||||
Reference in new issue
Block a user