Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty nodes on legacy bridge #46

Merged
merged 4 commits into from
May 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -53,6 +53,9 @@
# This will always be true for legacy devices
FORCE_NODE_ENUM = False

# Force always polling on legacy devices
FORCE_NODE_POLL = False

NODE_MEDIA_CAPABILITIES = [
"mediaVolume",
"mediaPlayPause",
Expand Down
18 changes: 9 additions & 9 deletions custom_components/nexa_bridge_x/nexa.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@
RECONNECT_SLEEP,
WS_PORT,
HTTP_BASIC_AUTH,
FORCE_NODE_ENUM
FORCE_NODE_ENUM,
FORCE_NODE_POLL
)
import dateutil.parser
import asyncio
Expand Down Expand Up @@ -81,10 +82,6 @@ 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 @@ -332,11 +329,13 @@ async def fetch_info(self) -> NexaInfoData:
"""Get information about bridge"""
return await self.request("get", "info")

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

if (FORCE_NODE_ENUM or self.legacy) and not skip_enum:
result = await self.request("get", "nodes")
if FORCE_NODE_ENUM or self.legacy:
new_result = []
for r in result:
try:
Expand Down Expand Up @@ -668,11 +667,12 @@ async def _async_update_data(self) -> None:
"""Update data by pulling in the background"""
try:
timeout = POLL_TIMEOUT if self.has_polled else DISCOVERY_TIMEOUT
skip = False if FORCE_NODE_POLL else self.has_polled

async with async_timeout.timeout(timeout):
results = await asyncio.gather(*[
self.api.fetch_info(),
self.api.fetch_nodes(self.has_polled),
self.api.fetch_nodes(skip),
self.api.fetch_energy(),
self.api.fetch_energy_nodes(),
])
Expand Down
Loading