From b8edc86500f3208b48663c16fd274dc316d8dc8b Mon Sep 17 00:00:00 2001 From: puddly <32534428+puddly@users.noreply.github.com> Date: Wed, 26 Oct 2022 21:29:48 -0400 Subject: [PATCH] Handle sending ZCL commands with empty bitmap options (#81051) Handle sending commands with empty bitmaps --- homeassistant/components/zha/core/helpers.py | 30 +++++++------------- tests/components/zha/test_helpers.py | 14 +++++++++ 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/homeassistant/components/zha/core/helpers.py b/homeassistant/components/zha/core/helpers.py index 409d45789b5..1ea9a2a4c9b 100644 --- a/homeassistant/components/zha/core/helpers.py +++ b/homeassistant/components/zha/core/helpers.py @@ -14,7 +14,6 @@ import enum import functools import itertools import logging -import operator from random import uniform import re from typing import TYPE_CHECKING, Any, TypeVar @@ -163,25 +162,16 @@ def convert_to_zcl_values( if field.name not in fields: continue value = fields[field.name] - if issubclass(field.type, enum.Flag): - if isinstance(value, list): - value = field.type( - functools.reduce( - operator.ior, - [ - field.type[flag.replace(" ", "_")] - if isinstance(flag, str) - else field.type(flag) - for flag in value - ], - ) - ) - else: - value = ( - field.type[value.replace(" ", "_")] - if isinstance(value, str) - else field.type(value) - ) + if issubclass(field.type, enum.Flag) and isinstance(value, list): + new_value = 0 + + for flag in value: + if isinstance(flag, str): + new_value |= field.type[flag.replace(" ", "_")] + else: + new_value |= flag + + value = field.type(new_value) elif issubclass(field.type, enum.Enum): value = ( field.type[value.replace(" ", "_")] diff --git a/tests/components/zha/test_helpers.py b/tests/components/zha/test_helpers.py index f5fb5c4f5c0..64f8c732ca9 100644 --- a/tests/components/zha/test_helpers.py +++ b/tests/components/zha/test_helpers.py @@ -195,3 +195,17 @@ async def test_zcl_schema_conversions(hass, device_light): assert isinstance(converted_data["start_hue"], uint16_t) assert converted_data["start_hue"] == 196 + + # This time, the update flags bitmap is empty + raw_data = { + "update_flags": [], + "action": 0x02, + "direction": 0x01, + "time": 20, + "start_hue": 196, + } + + converted_data = convert_to_zcl_values(raw_data, command_schema) + + # No flags are passed through + assert converted_data["update_flags"] == 0