Skip to content

Commit

Permalink
feat: alternative legacy poll mechanism (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
andersevenrud committed May 3, 2024
1 parent 179e2f9 commit 3f0af19
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions custom_components/nexa_bridge_x/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@
# How long to wait for a call to the bridge to respond
CALL_TIMEOUT = 30

# How long to wait for the initial poll request
DISCOVERY_TIMEOUT = 120

# How long for a websocket to reconnect after failure
RECONNECT_SLEEP = 5

Expand Down
30 changes: 23 additions & 7 deletions custom_components/nexa_bridge_x/nexa.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
NODE_MEDIA_CAPABILITIES,
POLL_INTERVAL,
POLL_TIMEOUT,
DISCOVERY_TIMEOUT,
CALL_TIMEOUT,
RECONNECT_SLEEP,
WS_PORT,
Expand Down Expand Up @@ -80,6 +81,10 @@ def values_from_events(node: NexaNodeData, legacy: bool) -> list[NexaNodeValue]:
data[prev_key],
data["time"]
))
else:
if legacy and "capabilities" in node:
for key in node["capabilities"]:
values.append(NexaNodeValue(key, None, None, "0"))

return values

Expand Down Expand Up @@ -327,14 +332,20 @@ async def fetch_info(self) -> NexaInfoData:
"""Get information about bridge"""
return await self.request("get", "info")

async def fetch_nodes(self) -> list[NexaNodeData]:
async def fetch_nodes(self, skip_enum: bool) -> list[NexaNodeData]:
"""Get all configured nodes"""
result = await self.request("get", "nodes")

if FORCE_NODE_ENUM or self.legacy:
return await asyncio.gather(*[
self.fetch_node(r["id"]) for r in result
])
if (FORCE_NODE_ENUM or self.legacy) and not skip_enum:
new_result = []
for r in result:
try:
data = await self.fetch_node(r["id"])
new_result.append(data)
except:
_LOGGER.error("Failed to enum node data: %s", r["id"])

return new_result

return result

Expand Down Expand Up @@ -604,6 +615,7 @@ def __init__(self, hass: HomeAssistant, api: NexaApi, legacy: bool):
self.api = api
self.legacy = legacy
self.hass = hass
self.has_polled = False

def get_node_by_id(self, node_id: str) -> NexaNode | None:
"""Gets node by id"""
Expand Down Expand Up @@ -655,10 +667,12 @@ async def update_node_from_message(self, data: NexaWebsocketData) -> None:
async def _async_update_data(self) -> None:
"""Update data by pulling in the background"""
try:
async with async_timeout.timeout(POLL_TIMEOUT):
timeout = POLL_TIMEOUT if self.has_polled else DISCOVERY_TIMEOUT

async with async_timeout.timeout(timeout):
results = await asyncio.gather(*[
self.api.fetch_info(),
self.api.fetch_nodes(),
self.api.fetch_nodes(self.has_polled),
self.api.fetch_energy(),
self.api.fetch_energy_nodes(),
])
Expand All @@ -671,6 +685,8 @@ async def _async_update_data(self) -> None:
NexaEnergy(energy, energy_nodes, self.legacy)
)

self.has_polled = True

if self.data:
self.update_nodes_from_data(data)
return self.data
Expand Down

0 comments on commit 3f0af19

Please sign in to comment.