From 74bec58bfa71764707fa527dc60048db36dccf0f Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Thu, 7 Apr 2022 18:02:14 -0400 Subject: [PATCH] Fix ZHA group creation (#69629) --- homeassistant/components/zha/api.py | 6 +++--- homeassistant/components/zha/core/device.py | 10 ++++++++-- homeassistant/components/zha/core/group.py | 10 ++++++---- 3 files changed, 17 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/zha/api.py b/homeassistant/components/zha/api.py index c42384682da..faf8ccc5053 100644 --- a/homeassistant/components/zha/api.py +++ b/homeassistant/components/zha/api.py @@ -232,7 +232,7 @@ GROUP_MEMBER_SCHEMA = vol.All( vol.Schema( { vol.Required(ATTR_IEEE): IEEE_SCHEMA, - vol.Required(ATTR_ENDPOINT_ID): int, + vol.Required(ATTR_ENDPOINT_ID): vol.Coerce(int), } ), _cv_group_member, @@ -244,8 +244,8 @@ CLUSTER_BINDING_SCHEMA = vol.All( { vol.Required(ATTR_NAME): cv.string, vol.Required(ATTR_TYPE): cv.string, - vol.Required(ATTR_ID): int, - vol.Required(ATTR_ENDPOINT_ID): int, + vol.Required(ATTR_ID): vol.Coerce(int), + vol.Required(ATTR_ENDPOINT_ID): vol.Coerce(int), } ), _cv_cluster_binding, diff --git a/homeassistant/components/zha/core/device.py b/homeassistant/components/zha/core/device.py index e80a0725cc1..41d90b48869 100644 --- a/homeassistant/components/zha/core/device.py +++ b/homeassistant/components/zha/core/device.py @@ -661,7 +661,11 @@ class ZHADevice(LogMixin): async def async_add_to_group(self, group_id: int) -> None: """Add this device to the provided zigbee group.""" try: - await self._zigpy_device.add_to_group(group_id) + # A group name is required. However, the spec also explicitly states that + # the group name can be ignored by the receiving device if a device cannot + # store it, so we cannot rely on it existing after being written. This is + # only done to make the ZCL command valid. + await self._zigpy_device.add_to_group(group_id, name=f"0x{group_id:04X}") except (zigpy.exceptions.ZigbeeException, asyncio.TimeoutError) as ex: self.debug( "Failed to add device '%s' to group: 0x%04x ex: %s", @@ -687,7 +691,9 @@ class ZHADevice(LogMixin): ) -> None: """Add the device endpoint to the provided zigbee group.""" try: - await self._zigpy_device.endpoints[endpoint_id].add_to_group(group_id) + await self._zigpy_device.endpoints[endpoint_id].add_to_group( + group_id, name=f"0x{group_id:04X}" + ) except (zigpy.exceptions.ZigbeeException, asyncio.TimeoutError) as ex: self.debug( "Failed to add endpoint: %s for device: '%s' to group: 0x%04x ex: %s", diff --git a/homeassistant/components/zha/core/group.py b/homeassistant/components/zha/core/group.py index af17f28e622..1392041c4d4 100644 --- a/homeassistant/components/zha/core/group.py +++ b/homeassistant/components/zha/core/group.py @@ -2,7 +2,6 @@ from __future__ import annotations import asyncio -import collections import logging from typing import TYPE_CHECKING, Any, NamedTuple @@ -30,9 +29,12 @@ class GroupMember(NamedTuple): endpoint_id: int -GroupEntityReference = collections.namedtuple( - "GroupEntityReference", "name original_name entity_id" -) +class GroupEntityReference(NamedTuple): + """Reference to a group entity.""" + + name: str + original_name: str + entity_id: int class ZHAGroupMember(LogMixin):