Skip to content

Commit

Permalink
Merge pull request #124 from mdeweerd/dev
Browse files Browse the repository at this point in the history
Enable "tries" parameter for scan_device .
  • Loading branch information
mdeweerd authored Jan 14, 2023
2 parents 7cfa6d8 + a3cede2 commit fa23839
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 31 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,8 @@ data:
# endpoint: 1
# Optional: endpoints to scan, when missing: all known endpoints
endpoint: [1, 2]
# Optional: tries Default:3 higher is useful for sleepy devices
tries: 100
```

Scan using the entity name:
Expand Down
2 changes: 1 addition & 1 deletion custom_components/zha_toolkit/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"domain": "zha_toolkit",
"iot_class": "local_polling",
"issue_tracker": "https://github.com/mdeweerd/zha-toolkit/issues",
"name": "\ud83e\uddf0 ZHA Toolkit Service",
"name": "\ud83e\uddf0 ZHA Toolkit",
"requirements": [
"packaging>=20.8",
"pytz"
Expand Down
54 changes: 38 additions & 16 deletions custom_components/zha_toolkit/scan_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def wrapper(cmd, *args, **kwargs):
return await cmd(*args, **kwargs)


async def scan_results(device, endpoints=None, manufacturer=None):
async def scan_results(device, endpoints=None, manufacturer=None, tries=3):
"""Construct scan results from information available in device"""
result: dict[str, str | list | None] = {
"ieee": str(device.ieee),
Expand Down Expand Up @@ -84,23 +84,27 @@ async def scan_results(device, endpoints=None, manufacturer=None):
LOGGER.debug(
"Scanning endpoint #%i with manf '%r'", epid, manufacturer
)
endpoint.update(await scan_endpoint(ep, manufacturer))
endpoint.update(
await scan_endpoint(ep, manufacturer, tries=tries)
)
if not u.isManf(manufacturer) and u.isManf(ep.manufacturer_id):
LOGGER.debug(
"Scanning endpoint #%i with manf '%r'",
epid,
ep.manufacturer_id,
)
endpoint.update(
await scan_endpoint(ep, ep.manufacturer_id)
await scan_endpoint(
ep, ep.manufacturer_id, tries=tries
)
)
ep_result.append(endpoint)

result["endpoints"] = ep_result
return result


async def scan_endpoint(ep, manufacturer=None):
async def scan_endpoint(ep, manufacturer=None, tries=3):
result = {}
clusters = {}
for cluster in ep.in_clusters.values():
Expand All @@ -111,7 +115,7 @@ async def scan_endpoint(ep, manufacturer=None):
)
key = f"0x{cluster.cluster_id:04x}"
clusters[key] = await scan_cluster(
cluster, is_server=True, manufacturer=manufacturer
cluster, is_server=True, manufacturer=manufacturer, tries=tries
)
result["in_clusters"] = dict(sorted(clusters.items(), key=lambda k: k[0]))

Expand All @@ -124,20 +128,20 @@ async def scan_endpoint(ep, manufacturer=None):
)
key = f"0x{cluster.cluster_id:04x}"
clusters[key] = await scan_cluster(
cluster, is_server=True, manufacturer=manufacturer
cluster, is_server=True, manufacturer=manufacturer, tries=tries
)
result["out_clusters"] = dict(sorted(clusters.items(), key=lambda k: k[0]))
return result


async def scan_cluster(cluster, is_server=True, manufacturer=None):
async def scan_cluster(cluster, is_server=True, manufacturer=None, tries=3):
if is_server:
cmds_gen = "commands_generated"
cmds_rec = "commands_received"
else:
cmds_rec = "commands_generated"
cmds_gen = "commands_received"
attributes = await discover_attributes_extended(cluster, None)
attributes = await discover_attributes_extended(cluster, None, tries=tries)
LOGGER.debug("scan_cluster attributes (none): %s", attributes)
if u.isManf(manufacturer):
LOGGER.debug(
Expand All @@ -146,7 +150,9 @@ async def scan_cluster(cluster, is_server=True, manufacturer=None):
attributes,
)
attributes.update(
await discover_attributes_extended(cluster, manufacturer)
await discover_attributes_extended(
cluster, manufacturer, tries=tries
)
)

# LOGGER.debug("scan_cluster attributes: %s", attributes)
Expand All @@ -156,12 +162,16 @@ async def scan_cluster(cluster, is_server=True, manufacturer=None):
"title": cluster.name,
"name": cluster.ep_attribute,
"attributes": attributes,
cmds_rec: await discover_commands_received(cluster, is_server),
cmds_gen: await discover_commands_generated(cluster, is_server),
cmds_rec: await discover_commands_received(
cluster, is_server, tries=tries
),
cmds_gen: await discover_commands_generated(
cluster, is_server, tries=tries
),
}


async def discover_attributes_extended(cluster, manufacturer=None):
async def discover_attributes_extended(cluster, manufacturer=None, tries=3):
from zigpy.zcl import foundation

LOGGER.debug("Discovering attributes extended")
Expand All @@ -177,6 +187,7 @@ async def discover_attributes_extended(cluster, manufacturer=None):
attr_id, # Start attribute identifier
16, # Number of attributes to discover in this request
manufacturer=manufacturer,
tries=tries,
)
await asyncio.sleep(0.2)
except (ValueError, DeliveryError, asyncio.TimeoutError) as ex:
Expand Down Expand Up @@ -248,7 +259,9 @@ async def discover_attributes_extended(cluster, manufacturer=None):
while chunk:
try:
chunk = sorted(chunk)
success, failed = await read_attr(cluster, chunk, manufacturer)
success, failed = await read_attr(
cluster, chunk, manufacturer, tries=tries
)
LOGGER.debug(
"Reading attr success: %s, failed %s", success, failed
)
Expand All @@ -272,7 +285,9 @@ async def discover_attributes_extended(cluster, manufacturer=None):
return {f"0x{a_id:04x}": result[a_id] for a_id in sorted(result)}


async def discover_commands_received(cluster, is_server, manufacturer=None):
async def discover_commands_received(
cluster, is_server, manufacturer=None, tries=3
):
from zigpy.zcl.foundation import Status

LOGGER.debug("Discovering commands received")
Expand All @@ -288,6 +303,7 @@ async def discover_commands_received(cluster, is_server, manufacturer=None):
cmd_id, # Start index of commands to discover
16, # Number of commands to discover
manufacturer=manufacturer,
tries=tries,
)
await asyncio.sleep(0.2)
except (ValueError, DeliveryError, asyncio.TimeoutError) as ex:
Expand Down Expand Up @@ -327,7 +343,9 @@ async def discover_commands_received(cluster, is_server, manufacturer=None):
return dict(sorted(result.items(), key=lambda k: k[0]))


async def discover_commands_generated(cluster, is_server, manufacturer=None):
async def discover_commands_generated(
cluster, is_server, manufacturer=None, tries=3
):
from zigpy.zcl.foundation import Status

LOGGER.debug("Discovering commands generated")
Expand All @@ -343,6 +361,7 @@ async def discover_commands_generated(cluster, is_server, manufacturer=None):
cmd_id, # Start index of commands to discover
16, # Number of commands to discover this run
manufacturer=manufacturer,
tries=tries,
)
await asyncio.sleep(0.2)
except (ValueError, DeliveryError, asyncio.TimeoutError) as ex:
Expand Down Expand Up @@ -393,6 +412,7 @@ async def scan_device(

endpoints = params[p.EP_ID]
manf = params[p.MANF]
tries = params[p.TRIES]

if endpoints is None:
endpoints = []
Expand All @@ -403,7 +423,9 @@ async def scan_device(

endpoints = sorted(set(endpoints)) # Uniqify and sort

scan = await scan_results(device, endpoints, manufacturer=manf)
scan = await scan_results(
device, endpoints, manufacturer=manf, tries=tries
)

event_data["scan"] = scan

Expand Down
35 changes: 21 additions & 14 deletions custom_components/zha_toolkit/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1015,7 +1015,7 @@ conf_report:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1094,7 +1094,7 @@ conf_report_read:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1179,7 +1179,7 @@ get_groups:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1256,7 +1256,7 @@ get_zll_groups:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1293,7 +1293,7 @@ ha_set_state:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
state_id:
Expand Down Expand Up @@ -1439,7 +1439,7 @@ rejoin:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1485,7 +1485,7 @@ leave:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1548,7 +1548,7 @@ misc_settime:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1612,7 +1612,7 @@ ota_notify:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1667,7 +1667,7 @@ remove_all_groups:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1723,7 +1723,7 @@ remove_from_group:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1780,7 +1780,7 @@ remove_group:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1825,6 +1825,13 @@ scan_device:
min: 1
max: 255
mode: box
tries:
description: Number of times a zigbee packet is repeated when no response
selector:
number:
min: 1
max: 255
mode: box
event_success:
description: Event name in case of success
example: my_read_success_trigger_event
Expand Down Expand Up @@ -1878,7 +1885,7 @@ unbind_coordinator:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down Expand Up @@ -1924,7 +1931,7 @@ tuya_magic:
description: Number of times the zigbee packet should be attempted
selector:
number:
min: 0
min: 1
max: 255
mode: box
event_success:
Expand Down

0 comments on commit fa23839

Please sign in to comment.