Skip to content

[BUG] #1354

Description

@chbndrhnns

Bug report

Description

WXKG11LMSensorSwitchLightController.get_zha_action() misidentifies plain on_off attribute reports as a "single" click, because it doesn't filter by attribute_id before applying its click-count mapping, and Python's True == 1 causes the boolean on_off value to match the 1: "single" mapping entry.

Expected behaviour: only the device's dedicated click-count attribute (attribute_id: 32768) should determine the action (single/double/triple/quadruple).

Observed behaviour: the device also fires plain on_off attribute-report events (attribute_id: 0, value: True) as a side effect of the physical button contact, on every press regardless of click count. get_zha_action reads data["args"]["value"] from these events too, without checking attribute_id:

# apps/controllerx/cx_devices/aqara.py
def get_zha_action(self, data: EventData) -> str:
    mapping = {
        1: "single",
        2: "double",
        3: "triple",
        4: "quadruple",
    }
    clicks = data["args"]["value"]
    return mapping.get(clicks, "")

Since True == 1 in Python, mapping.get(True, "") matches key 1 and returns "single". This spurious "single" action fires alongside (and races against) the real click-count action from the attribute_id: 32768 event, so a double- or triple-click intermittently ends up executing the single action's result instead, depending on which event's action handler finishes last.

ZHAIntegration.event_callback calls get_zha_action for every zha_event from the device (filtered only by device_ieee), so nothing upstream of this function filters out the unrelated on_off events either:

# apps/controllerx/cx_core/integration/zha.py
async def event_callback(self, event_name, data, kwargs):
    action = self.controller.get_zha_action(data)
    ...

Steps to reproduce

  1. Pair a lumi.sensor_switch.aq2 (Aqara WXKG11LM) via ZHA.
  2. Configure WXKG11LMSensorSwitchLightController with distinct actions mapped to single and double/triple/quadruple.
  3. In Developer Tools → Events, listen to zha_event and physically double- or triple-click the switch.
  4. Observe both an attribute_id: 0 (on_off) event and an attribute_id: 32768 event firing for the same physical action.
  5. Observe the light lands in the state associated with the single action, even on a double/triple click.

Suggested fix

def get_zha_action(self, data: EventData) -> str:
    args = data.get("args", {})
    if args.get("attribute_id") != 32768:
        return ""
    mapping = {
        1: "single",
        2: "double",
        3: "triple",
        4: "quadruple",
    }
    return mapping.get(args.get("value"), "")

(A more general guard against the True == 1 issue — if not isinstance(clicks, int) or isinstance(clicks, bool): return "" — would also help any other controller with a similarly-structured mapping.)

Additional information

  • Devices involved:
    • Model: Aqara WXKG11LM (lumi.sensor_switch.aq2, ZHA quirk zhaquirks.xiaomi.aqara.switch_aq2:SwitchAQ2) as Controller
    • Model: generic dimmable light as Light
  • Integration: zha
  • AppDaemon version: 0.18.5
  • ControllerX version: v5.2.3
  • HACS version (if installed

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions