From 2cb2e02c6830e42109e1fae34a29f80bdbbcc53d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Modzelewski?= Date: Tue, 17 Sep 2024 12:53:11 +0200 Subject: [PATCH 1/4] Pass zha_gw_hass to utils.get_hass to avoid attribute error fixes: #258 --- custom_components/zha_toolkit/__init__.py | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/custom_components/zha_toolkit/__init__.py b/custom_components/zha_toolkit/__init__.py index 6a59ac4..7d05f37 100644 --- a/custom_components/zha_toolkit/__init__.py +++ b/custom_components/zha_toolkit/__init__.py @@ -806,15 +806,19 @@ async def toolkit_service(service): LOGGER.debug( "Fire %s -> %s", params[p.EVT_SUCCESS], event_data ) - u.get_hass(zha_gw).bus.fire(params[p.EVT_SUCCESS], event_data) + u.get_hass(zha_gw_hass).bus.fire( + params[p.EVT_SUCCESS], event_data + ) else: if params[p.EVT_FAIL] is not None: LOGGER.debug("Fire %s -> %s", params[p.EVT_FAIL], event_data) - u.get_hass(zha_gw).bus.fire(params[p.EVT_FAIL], event_data) + u.get_hass(zha_gw_hass).bus.fire( + params[p.EVT_FAIL], event_data + ) if params[p.EVT_DONE] is not None: LOGGER.debug("Fire %s -> %s", params[p.EVT_DONE], event_data) - u.get_hass(zha_gw).bus.fire(params[p.EVT_DONE], event_data) + u.get_hass(zha_gw_hass).bus.fire(params[p.EVT_DONE], event_data) if handler_exception is not None: LOGGER.error( From f27fa190bae773b510f310c59367aedd42d58a34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Modzelewski?= Date: Tue, 17 Sep 2024 13:06:30 +0200 Subject: [PATCH 2/4] Migrate from DATA_TYPES to DataType fixes: #261 --- custom_components/zha_toolkit/scan_device.py | 9 ++++++--- custom_components/zha_toolkit/utils.py | 6 +++--- custom_components/zha_toolkit/zcl_attr.py | 4 ++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/custom_components/zha_toolkit/scan_device.py b/custom_components/zha_toolkit/scan_device.py index d9fce9b..3525685 100644 --- a/custom_components/zha_toolkit/scan_device.py +++ b/custom_components/zha_toolkit/scan_device.py @@ -216,7 +216,10 @@ async def discover_attributes_extended(cluster, manufacturer=None, tries=3): attr_name = attr_def.name else: attr_name = attr_def[0] - attr_type = foundation.DATA_TYPES.get(attr_rec.datatype) + try: + attr_type = foundation.DataType.from_type_id(attr_rec.datatype) + except KeyError: + attr_type = None access_acl = t.uint8_t(attr_rec.acl) # Note: reading back Array type was fixed in zigpy 0.58.1 . @@ -229,8 +232,8 @@ async def discover_attributes_extended(cluster, manufacturer=None, tries=3): if attr_type: attr_type = [ attr_type_hex, - attr_type[1].__name__, - attr_type[2].__name__, + attr_type.python_type.__name__, + attr_type.type_class.__name__, ] else: attr_type = attr_type_hex diff --git a/custom_components/zha_toolkit/utils.py b/custom_components/zha_toolkit/utils.py index 866ccc0..20acab9 100644 --- a/custom_components/zha_toolkit/utils.py +++ b/custom_components/zha_toolkit/utils.py @@ -631,7 +631,7 @@ def record_read_data( attr_name = params[p.CSV_LABEL] else: python_type = type(read_resp[0][attr_id]) - attr_type = f.DATA_TYPES.pytype_to_datatype_id(python_type) + attr_type = f.DataType.from_python_type(python_type).type_id try: attr_def = cluster.attributes.get( @@ -690,7 +690,7 @@ def get_attr_type(cluster, attr_id): else: attr_type = attr_def[1] - return f.DATA_TYPES.pytype_to_datatype_id(attr_type) + return f.DataType.from_python_type(attr_type).type_id except Exception: # nosec LOGGER.debug("Could not find type for %s in %r", attr_id, cluster) @@ -800,7 +800,7 @@ def attr_encode(attr_val_in, attr_type): # noqa C901 else: # Try to apply conversion using foundation DATA_TYPES table # Note: this is not perfect and specific conversions may be needed. - data_type = f.DATA_TYPES[attr_type][1] + data_type = f.DataType.from_type_id(attr_type).python_type LOGGER.debug(f"Data type '{data_type}' for attr type {attr_type}") if isinstance(attr_val_in, list): # Without length byte after serialisation: diff --git a/custom_components/zha_toolkit/zcl_attr.py b/custom_components/zha_toolkit/zcl_attr.py index 62e1c96..596506c 100644 --- a/custom_components/zha_toolkit/zcl_attr.py +++ b/custom_components/zha_toolkit/zcl_attr.py @@ -382,9 +382,9 @@ async def attr_write( # noqa: C901 # Try to get attribute type if success and (attr_id in result_read[0]): python_type = type(result_read[0][attr_id]) - found_attr_type = f.DATA_TYPES.pytype_to_datatype_id( + found_attr_type = f.DataType.from_python_type( python_type - ) + ).type_id LOGGER.debug( "Type determined from read: 0x%02x", found_attr_type ) From 55f440af55f3b28cb37ed7acb396e14bfa5e72c0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Modzelewski?= Date: Tue, 17 Sep 2024 13:09:25 +0200 Subject: [PATCH 3/4] Require 2024.9.0 --- hacs.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hacs.json b/hacs.json index b09835a..f43d65d 100644 --- a/hacs.json +++ b/hacs.json @@ -5,5 +5,5 @@ "filename": "zha-toolkit.zip", "render_readme": true, "persistent_directory": "local", - "homeassistant": "2024.8.0" + "homeassistant": "2024.9.0" } From 4f0b657138399e1a567f0dd8e9888d6f538fd15a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Modzelewski?= Date: Wed, 30 Oct 2024 13:01:29 +0100 Subject: [PATCH 4/4] fixup! Migrate from DATA_TYPES to DataType --- custom_components/zha_toolkit/scan_device.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/custom_components/zha_toolkit/scan_device.py b/custom_components/zha_toolkit/scan_device.py index 3525685..5ab4ad4 100644 --- a/custom_components/zha_toolkit/scan_device.py +++ b/custom_components/zha_toolkit/scan_device.py @@ -233,7 +233,7 @@ async def discover_attributes_extended(cluster, manufacturer=None, tries=3): attr_type = [ attr_type_hex, attr_type.python_type.__name__, - attr_type.type_class.__name__, + attr_type.type_class.name, ] else: attr_type = attr_type_hex