Skip to content

Commit dd0c18f

Browse files
authored
Merge pull request #17 from wbyoung/pass-leds-to-start-action
Pass current LED config to start action scripts
2 parents 57f80c6 + 67a26bd commit dd0c18f

File tree

4 files changed

+222
-3
lines changed

4 files changed

+222
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ A script that will be called when the notification is activated. This can occur
115115
116116
- Input fields:
117117
- `notification`: The slug of the notification being activated.
118+
- `leds`: The current configuration of the LEDs for the notification. If being activated, this is how they're configured by default. If the notification is active and being re-activated, this is the current LED configuration (respecting any override used in [`lampie.activate`](#lampieactivate)).
118119
- Response variables _all are optional_:
119120
- `leds` _default_ » `null`: An override LED configuration to use or `null` to use the already configured value. See: [`examples/start-action.yaml`](examples/start-action.yaml).
120121
- `block_activation` _default_ » `False`: Allow blocking of activation (including marking the notification as `on`).

custom_components/lampie/orchestrator.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,12 @@ async def activate_notification(
266266
entry = coordinator.config_entry
267267
switches = entry.data[CONF_SWITCH_ENTITIES]
268268

269-
start_action_response = await self._invoke_start_action(slug)
269+
start_action_response = await self._invoke_start_action(
270+
slug,
271+
led_config
272+
or self.notification_info(slug).led_config_override
273+
or coordinator.led_config,
274+
)
270275
block_activation = start_action_response.block_activation
271276

272277
if block_activation:
@@ -529,14 +534,19 @@ def _primary_for_switch(
529534
coordinator = self._coordinators.get(priorities[0]) if priorities else None
530535
return coordinator or _LampieUnmanagedSwitchCoordinator()
531536

532-
async def _invoke_start_action(self, slug: Slug) -> _StartScriptResult:
537+
async def _invoke_start_action(
538+
self, slug: Slug, led_config: tuple[LEDConfig, ...]
539+
) -> _StartScriptResult:
533540
coordinator = self._coordinators[slug]
534541
entry = coordinator.config_entry
535542
start_action = entry.data.get(CONF_START_ACTION)
536543
response = {}
537544

538545
if start_action:
539-
args = {"notification": slug}
546+
args = {
547+
"notification": slug,
548+
"leds": [item.to_dict() for item in led_config],
549+
}
540550

541551
_LOGGER.debug(
542552
"executing start action %r for notification %r (%r)",

tests/snapshots/test_scenarios.ambr

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6241,6 +6241,14 @@
62416241
'script',
62426242
'block_activation',
62436243
dict({
6244+
'leds': list([
6245+
dict({
6246+
'brightness': 100.0,
6247+
'color': 'red',
6248+
'duration': None,
6249+
'effect': 'open_close',
6250+
}),
6251+
]),
62446252
'notification': 'doors_open',
62456253
}),
62466254
),
@@ -6516,6 +6524,14 @@
65166524
'script',
65176525
'color_override',
65186526
dict({
6527+
'leds': list([
6528+
dict({
6529+
'brightness': 100.0,
6530+
'color': 'red',
6531+
'duration': None,
6532+
'effect': 'open_close',
6533+
}),
6534+
]),
65196535
'notification': 'doors_open',
65206536
}),
65216537
),
@@ -6561,6 +6577,154 @@
65616577
}),
65626578
])
65636579
# ---
6580+
# name: test_toggle_notification_with_actions[color_override_reactivated][sensor.kitchen_effect_brightness]
6581+
StateSnapshot({
6582+
'attributes': ReadOnlyDict({
6583+
'friendly_name': 'Kitchen Effect brightness',
6584+
'icon': 'mdi:brightness-percent',
6585+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
6586+
'unit_of_measurement': '%',
6587+
}),
6588+
'context': <ANY>,
6589+
'entity_id': 'sensor.kitchen_effect_brightness',
6590+
'last_changed': <ANY>,
6591+
'last_reported': <ANY>,
6592+
'last_updated': <ANY>,
6593+
'state': '100.0',
6594+
})
6595+
# ---
6596+
# name: test_toggle_notification_with_actions[color_override_reactivated][sensor.kitchen_effect_color]
6597+
StateSnapshot({
6598+
'attributes': ReadOnlyDict({
6599+
'friendly_name': 'Kitchen Effect color',
6600+
'icon': 'mdi:palette',
6601+
}),
6602+
'context': <ANY>,
6603+
'entity_id': 'sensor.kitchen_effect_color',
6604+
'last_changed': <ANY>,
6605+
'last_reported': <ANY>,
6606+
'last_updated': <ANY>,
6607+
'state': 'cyan',
6608+
})
6609+
# ---
6610+
# name: test_toggle_notification_with_actions[color_override_reactivated][sensor.kitchen_effect_duration]
6611+
StateSnapshot({
6612+
'attributes': ReadOnlyDict({
6613+
'friendly_name': 'Kitchen Effect duration',
6614+
'icon': 'mdi:timer',
6615+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
6616+
'unit_of_measurement': <UnitOfTime.SECONDS: 's'>,
6617+
}),
6618+
'context': <ANY>,
6619+
'entity_id': 'sensor.kitchen_effect_duration',
6620+
'last_changed': <ANY>,
6621+
'last_reported': <ANY>,
6622+
'last_updated': <ANY>,
6623+
'state': 'unknown',
6624+
})
6625+
# ---
6626+
# name: test_toggle_notification_with_actions[color_override_reactivated][sensor.kitchen_effect_type]
6627+
StateSnapshot({
6628+
'attributes': ReadOnlyDict({
6629+
'friendly_name': 'Kitchen Effect type',
6630+
'icon': 'mdi:star-four-points-box',
6631+
}),
6632+
'context': <ANY>,
6633+
'entity_id': 'sensor.kitchen_effect_type',
6634+
'last_changed': <ANY>,
6635+
'last_reported': <ANY>,
6636+
'last_updated': <ANY>,
6637+
'state': 'solid',
6638+
})
6639+
# ---
6640+
# name: test_toggle_notification_with_actions[color_override_reactivated][sensor.kitchen_notification]
6641+
StateSnapshot({
6642+
'attributes': ReadOnlyDict({
6643+
'friendly_name': 'Kitchen Notification',
6644+
'icon': 'mdi:circle-box',
6645+
}),
6646+
'context': <ANY>,
6647+
'entity_id': 'sensor.kitchen_notification',
6648+
'last_changed': <ANY>,
6649+
'last_reported': <ANY>,
6650+
'last_updated': <ANY>,
6651+
'state': 'doors_open',
6652+
})
6653+
# ---
6654+
# name: test_toggle_notification_with_actions[color_override_reactivated][service_calls]
6655+
list([
6656+
tuple(
6657+
'script',
6658+
'color_override',
6659+
dict({
6660+
'leds': list([
6661+
dict({
6662+
'brightness': 100.0,
6663+
'color': 'red',
6664+
'duration': None,
6665+
'effect': 'open_close',
6666+
}),
6667+
]),
6668+
'notification': 'doors_open',
6669+
}),
6670+
),
6671+
tuple(
6672+
'script',
6673+
'color_override',
6674+
dict({
6675+
'leds': list([
6676+
dict({
6677+
'brightness': 100.0,
6678+
'color': 'cyan',
6679+
'duration': None,
6680+
'effect': 'solid',
6681+
}),
6682+
]),
6683+
'notification': 'doors_open',
6684+
}),
6685+
),
6686+
])
6687+
# ---
6688+
# name: test_toggle_notification_with_actions[color_override_reactivated][switch.doors_open_notification]
6689+
StateSnapshot({
6690+
'attributes': ReadOnlyDict({
6691+
'friendly_name': 'Doors Open Notification',
6692+
'icon': 'mdi:circle-box',
6693+
}),
6694+
'context': <ANY>,
6695+
'entity_id': 'switch.doors_open_notification',
6696+
'last_changed': <ANY>,
6697+
'last_reported': <ANY>,
6698+
'last_updated': <ANY>,
6699+
'state': 'on',
6700+
})
6701+
# ---
6702+
# name: test_toggle_notification_with_actions[color_override_reactivated][zha_cluster_commands]
6703+
list([
6704+
ServiceCallSnapshot({
6705+
'context': <ANY>,
6706+
'data': ReadOnlyDict({
6707+
'cluster_id': 64561,
6708+
'cluster_type': 'in',
6709+
'command': 1,
6710+
'command_type': 'server',
6711+
'endpoint_id': 1,
6712+
'ieee': 'mock-ieee:kitchen',
6713+
'manufacturer': 4655,
6714+
'params': dict({
6715+
'led_color': 130,
6716+
'led_duration': 255,
6717+
'led_effect': 1,
6718+
'led_level': 100,
6719+
}),
6720+
}),
6721+
'domain': 'zha',
6722+
'hass': <ANY>,
6723+
'return_response': False,
6724+
'service': 'issue_zigbee_cluster_command',
6725+
}),
6726+
])
6727+
# ---
65646728
# name: test_toggle_notification_with_actions[single_led_change][sensor.kitchen_effect_brightness]
65656729
StateSnapshot({
65666730
'attributes': ReadOnlyDict({
@@ -6677,13 +6841,29 @@
66776841
'script',
66786842
'color_from_input',
66796843
dict({
6844+
'leds': list([
6845+
dict({
6846+
'brightness': 100.0,
6847+
'color': 'orange',
6848+
'duration': None,
6849+
'effect': 'slow_blink',
6850+
}),
6851+
]),
66806852
'notification': 'windows_open',
66816853
}),
66826854
),
66836855
tuple(
66846856
'script',
66856857
'color_from_input',
66866858
dict({
6859+
'leds': list([
6860+
dict({
6861+
'brightness': 100.0,
6862+
'color': 'red',
6863+
'duration': None,
6864+
'effect': 'open_close',
6865+
}),
6866+
]),
66876867
'notification': 'doors_open',
66886868
}),
66896869
),

tests/test_scenarios.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1607,6 +1607,34 @@ async def test_toggle_notifications_with_shared_switches(
16071607
"expected_events": None, # disabled for all (via none/absent)
16081608
},
16091609
),
1610+
Scenario(
1611+
"color_override_reactivated",
1612+
{
1613+
**_TOGGLE_NOTIFICATION_WITH_ACTIONS_BASE,
1614+
"configs": {
1615+
**_TOGGLE_NOTIFICATION_WITH_ACTIONS_CONFIGS,
1616+
"doors_open": {
1617+
**_TOGGLE_NOTIFICATION_WITH_ACTIONS_DOORS_OPEN,
1618+
CONF_START_ACTION: "script.color_override",
1619+
},
1620+
},
1621+
"scripts": _response_script(
1622+
"color_override", {"leds": [{"color": "cyan"}]}
1623+
),
1624+
"steps": [
1625+
{
1626+
"target": "switch.doors_open_notification",
1627+
"action": f"{SWITCH_DOMAIN}.{SERVICE_TURN_ON}",
1628+
},
1629+
{
1630+
"action": f"{DOMAIN}.{SERVICE_NAME_ACTIVATE}",
1631+
"data": {
1632+
"notification": "doors_open",
1633+
},
1634+
},
1635+
],
1636+
},
1637+
),
16101638
Scenario(
16111639
"single_led_change",
16121640
{

0 commit comments

Comments
 (0)