Small fix for device triggers and events on Hue integration (#61462)

pull/61468/head
Marcel van der Veldt 2021-12-11 00:11:34 +01:00 committed by GitHub
parent 6d54261322
commit 0d36b07d10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -40,6 +40,19 @@ TRIGGER_SCHEMA = DEVICE_TRIGGER_BASE_SCHEMA.extend(
}
)
DEFAULT_BUTTON_EVENT_TYPES = (
# all except `DOUBLE_SHORT_RELEASE`
ButtonEvent.INITIAL_PRESS,
ButtonEvent.REPEAT,
ButtonEvent.SHORT_RELEASE,
ButtonEvent.LONG_RELEASE,
)
DEVICE_SPECIFIC_EVENT_TYPES = {
# device specific overrides of specific supported button events
"Hue tap switch": (ButtonEvent.INITIAL_PRESS,),
}
async def async_validate_trigger_config(
bridge: "HueBridge",
@ -84,10 +97,13 @@ async def async_get_triggers(bridge: "HueBridge", device_entry: DeviceEntry):
hue_dev_id = get_hue_device_id(device_entry)
# extract triggers from all button resources of this Hue device
triggers = []
model_id = api.devices[hue_dev_id].product_data.product_name
for resource in api.devices.get_sensors(hue_dev_id):
if resource.type != ResourceTypes.BUTTON:
continue
for event_type in (x.value for x in ButtonEvent if x != ButtonEvent.UNKNOWN):
for event_type in DEVICE_SPECIFIC_EVENT_TYPES.get(
model_id, DEFAULT_BUTTON_EVENT_TYPES
):
triggers.append(
{
CONF_DEVICE_ID: device_entry.id,
@ -95,7 +111,7 @@ async def async_get_triggers(bridge: "HueBridge", device_entry: DeviceEntry):
CONF_PLATFORM: "device",
CONF_TYPE: event_type,
CONF_SUBTYPE: resource.metadata.control_id,
CONF_UNIQUE_ID: device_entry.id,
CONF_UNIQUE_ID: resource.id,
}
)
return triggers

View File

@ -70,12 +70,20 @@ async def test_get_triggers(hass, mock_bridge_v2, v2_resources_test_data, device
"platform": "device",
"domain": hue.DOMAIN,
"device_id": hue_wall_switch_device.id,
"unique_id": hue_wall_switch_device.id,
"unique_id": resource_id,
"type": event_type,
"subtype": control_id,
}
for event_type in (x.value for x in ButtonEvent if x != ButtonEvent.UNKNOWN)
for control_id in range(1, 3)
for event_type in (
ButtonEvent.INITIAL_PRESS,
ButtonEvent.LONG_RELEASE,
ButtonEvent.REPEAT,
ButtonEvent.SHORT_RELEASE,
)
for control_id, resource_id in (
(1, "c658d3d8-a013-4b81-8ac6-78b248537e70"),
(2, "be1eb834-bdf5-4d26-8fba-7b1feaa83a9d"),
)
),
]