Skip to content

Commit

Permalink
Merge pull request #55 from mdeweerd/dev
Browse files Browse the repository at this point in the history
Improved handling absence of Manufacturer
  • Loading branch information
mdeweerd authored Apr 14, 2022
2 parents 6b02abe + d39c334 commit 953b875
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 20 deletions.
1 change: 1 addition & 0 deletions STATS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Badges showing number of downloads per version

- ![badge latest](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/latest/total.svg)
- ![badge v0.8.1](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v0.8.1/total.svg)
- ![badge v0.8.0](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v0.8.0/total.svg)
- ![badge v0.7.27](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v0.7.27/total.svg)
- ![badge v0.7.26](https://img.shields.io/github/downloads/mdeweerd/zha-toolkit/v0.7.26/total.svg)
Expand Down
1 change: 0 additions & 1 deletion custom_components/zha_toolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,6 @@ async def toolkit_service(service):

slickParams = params.copy()
for k in params.keys():
LOGGER.debug(f"Key {p}")
if slickParams[k] is None or slickParams[k] is False:
del slickParams[k]

Expand Down
4 changes: 1 addition & 3 deletions custom_components/zha_toolkit/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,7 @@ async def backup(app, listener, ieee, cmd, data, service, params, event_data):
params=params,
)
else:
raise Exception(
"Radio type %s not supported for backup" % (radio_type)
)
raise Exception(f"Radio type {radio_type} not supported for backup")


async def handle_join(
Expand Down
26 changes: 17 additions & 9 deletions custom_components/zha_toolkit/scan_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ async def scan_results(device, endpoints=None, manufacturer=None):
for epid in endpoints:
if epid == 0:
continue
LOGGER.debug("scanning endpoint #%i", epid)
if epid in device.endpoints:
LOGGER.debug("scanning endpoint #%i", epid)
ep = device.endpoints[epid]
result["model"] = ep.model
result["manufacturer"] = ep.manufacturer
if ep.manufacturer_id is not None:
if u.isManf(ep.manufacturer_id):
result["manufacturer_id"] = f"0x{ep.manufacturer_id}"
else:
result["manufacturer_id"] = None
Expand All @@ -75,8 +75,16 @@ async def scan_results(device, endpoints=None, manufacturer=None):
"profile": f"0x{ep.profile_id:04x}",
}
if epid != 242:
LOGGER.debug(
"Scanning endpoint #%i with manf '%r'", epid, manufacturer
)
endpoint.update(await scan_endpoint(ep, manufacturer))
if manufacturer is None and ep.manufacturer_id is not None:
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)
)
Expand Down Expand Up @@ -125,7 +133,7 @@ async def scan_cluster(cluster, is_server=True, manufacturer=None):
cmds_gen = "commands_received"
attributes = await discover_attributes_extended(cluster, None)
LOGGER.debug("scan_cluster attributes (none): %s", attributes)
if manufacturer is not None:
if u.isManf(manufacturer):
LOGGER.debug(
"scan_cluster attributes (none) with manf '%s': %s",
manufacturer,
Expand Down Expand Up @@ -169,8 +177,8 @@ async def discover_attributes_extended(cluster, manufacturer=None):
LOGGER.error(
(
"Failed 'discover_attributes_extended'"
+ " starting 0x%04x/0x%04x."
+ " Error: %s"
" starting 0x%04x/0x%04x."
" Error: %s"
),
cluster.cluster_id,
attr_id,
Expand Down Expand Up @@ -224,7 +232,7 @@ async def discover_attributes_extended(cluster, manufacturer=None):
"access": access,
"access_acl": access_acl,
}
if manufacturer is not None:
if u.isManf(manufacturer):
result[attr_id]["manf_id"] = manufacturer
attr_id += 1
await asyncio.sleep(0.2)
Expand Down Expand Up @@ -361,7 +369,7 @@ async def scan_device(
LOGGER.error("missing ieee")
raise Exception("missing ieee")

LOGGER.debug("running 'scan_device' command: %s", service)
LOGGER.debug("Running 'scan_device'")

device = app.get_device(ieee)

Expand Down Expand Up @@ -393,7 +401,7 @@ async def scan_device(

# Set a unique filename for each device, using the manf name and
# the variable part of the device mac address
if model is not None and manufacturer is not None:
if model is not None and u.isManf(manufacturer):
ieee_tail = "".join([f"{o:02x}" for o in ieee[4::-1]])
file_name = f"{model}_{manufacturer}_{ieee_tail}{postfix}"
else:
Expand Down
16 changes: 12 additions & 4 deletions custom_components/zha_toolkit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,14 @@ def attr_encode(attr_val_in, attr_type): # noqa C901
return attr_obj, msg, compare_val


def isManf(manf, includeNone=False):
if manf is None:
return includeNone
return not (isinstance(manf, str) and manf == "") or (
isinstance(manf, int) and (manf == 0 or manf < 0)
)


# Common method to extract and convert parameters.
#
# Most parameters are similar, this avoids repeating
Expand Down Expand Up @@ -615,16 +623,16 @@ def extractParams( # noqa: C901
params[p.MANF] = str2int(rawParams[P.MANF])

manf = params[p.MANF]
if (isinstance(manf, str) and manf == "") or (
isinstance(manf, int) and (manf == 0 or manf < 0)
):
if not isManf(manf, True):
LOGGER.debug("Got manf '%s'", manf)
if hasattr(f.ZCLHeader, "NO_MANUFACTURER_ID"):
manf = f.ZCLHeader.NO_MANUFACTURER_ID
params[p.MANF] = f.ZCLHeader.NO_MANUFACTURER_ID
else:
# Forcing b"" not ok in call cases # Not None, force empty manf
params[p.MANF] = b""

LOGGER.debug("Final manf '%r'", params[p.MANF])

# Get tries
if P.TRIES in rawParams:
params[p.TRIES] = str2int(rawParams[P.TRIES])
Expand Down
7 changes: 4 additions & 3 deletions custom_components/zha_toolkit/zcl_attr.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def my_read_reporting_configuration_multiple(
record = f.ReadReportingConfigRecord()
record.attrid = attrid
record.direction = direction
LOGGER.warn(f"Record {record.direction} {record.attrid}")
# LOGGER.warn(f"Record {record.direction} {record.attrid}")
cfg.append(record)
LOGGER.warn("Read reporting with %s", cfg)

Expand Down Expand Up @@ -151,7 +151,6 @@ async def conf_report_read(
params[p.ATTR_ID],
params[p.MANF],
)
LOGGER.debug("Before call")
result_conf = (
await cluster.my_read_reporting_configuration_multiple(
params[p.ATTR_ID],
Expand Down Expand Up @@ -465,7 +464,9 @@ async def attr_write( # noqa: C901
fields.append(cluster.endpoint.endpoint_id)
fields.append(str(cluster.endpoint.device.ieee))
fields.append(
("0x%04X" % (params[p.MANF])) if params[p.MANF] is not None else ""
("0x%04X" % (params[p.MANF]),)
if params[p.MANF] is not None
else ""
)
u.append_to_csvfile(
fields,
Expand Down

0 comments on commit 953b875

Please sign in to comment.