Skip to content

Commit dfc9516

Browse files
authored
Merge pull request #6 from wbyoung/fix-override-reset
Fix reset eia `lampie.override`
2 parents 1b29008 + aac580a commit dfc9516

File tree

4 files changed

+245
-12
lines changed

4 files changed

+245
-12
lines changed

custom_components/lampie/orchestrator.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -365,32 +365,34 @@ async def dismiss_notification(
365365
async def override_switch(
366366
self,
367367
switch_id: SwitchId,
368-
source: LEDConfigSource,
369-
config: tuple[LEDConfig, ...],
368+
*,
369+
led_config_source: LEDConfigSource,
370+
led_config: tuple[LEDConfig, ...] | None,
370371
) -> None:
371372
"""Entrypoint for services to override & update a switch.
372373
373374
The switch state will be stored and the physical switch will be updated
374375
to show the specified configuration.
375376
"""
376377
from_state = self.switch_info(switch_id)
378+
is_reset = led_config is None
377379

378380
self.store_switch_info(
379381
switch_id,
380-
led_config_source=source,
381-
led_config=config,
382382
expiration=self._schedule_fallback_expiration(
383383
from_state.expiration,
384-
config,
384+
led_config or (),
385385
partial(self._async_handle_switch_override_expired, switch_id),
386-
log_context=f"{source}",
386+
log_context=f"{led_config_source}",
387387
),
388388
)
389389

390-
await self._transition_switch(
390+
await self._switch_apply_notification_or_override(
391391
switch_id,
392-
from_config=from_state.led_config,
393-
to_config=config,
392+
led_config_source=led_config_source,
393+
led_config=led_config,
394+
exclude={LEDConfigSourceType.SERVICE} if is_reset else set(),
395+
log_context="override-switch",
394396
)
395397

396398
async def _switch_apply_notification_or_override(

custom_components/lampie/services.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,6 @@ async def _override(
108108
led_config_source = LEDConfigSource(name, LEDConfigSourceType.SERVICE)
109109

110110
if led_config is None: # none indicates a reset
111-
led_config = ()
112111
led_config_source = LEDConfigSource(None)
113112
elif not led_config: # convert empty led config to clear config
114113
led_config = (LEDConfig(Color.BLUE, Effect.CLEAR),)
@@ -118,8 +117,8 @@ async def _override(
118117
for switch_id in switch_ids:
119118
await orchestrator.override_switch( # waiting in loop to avoid flooding the network
120119
switch_id,
121-
led_config_source,
122-
led_config,
120+
led_config_source=led_config_source,
121+
led_config=led_config,
123122
)
124123

125124

tests/snapshots/test_init.ambr

Lines changed: 199 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3735,6 +3735,205 @@
37353735
}),
37363736
])
37373737
# ---
3738+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][notification_info]
3739+
dict({
3740+
'expiration': ExpirationInfoSnapshot({
3741+
'cancel_listener': None,
3742+
'expires_at': None,
3743+
'started_at': HAFakeDatetime(2025, 5, 20, 3, 51, 32, 3245, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')),
3744+
}),
3745+
'led_config_override': None,
3746+
'notification_on': True,
3747+
})
3748+
# ---
3749+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][sensor.kitchen_effect_brightness]
3750+
StateSnapshot({
3751+
'attributes': ReadOnlyDict({
3752+
'friendly_name': 'Kitchen Effect brightness',
3753+
'icon': 'mdi:timer',
3754+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
3755+
'unit_of_measurement': '%',
3756+
}),
3757+
'context': <ANY>,
3758+
'entity_id': 'sensor.kitchen_effect_brightness',
3759+
'last_changed': <ANY>,
3760+
'last_reported': <ANY>,
3761+
'last_updated': <ANY>,
3762+
'state': '100.0',
3763+
})
3764+
# ---
3765+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][sensor.kitchen_effect_color]
3766+
StateSnapshot({
3767+
'attributes': ReadOnlyDict({
3768+
'friendly_name': 'Kitchen Effect color',
3769+
'icon': 'mdi:palette',
3770+
}),
3771+
'context': <ANY>,
3772+
'entity_id': 'sensor.kitchen_effect_color',
3773+
'last_changed': <ANY>,
3774+
'last_reported': <ANY>,
3775+
'last_updated': <ANY>,
3776+
'state': 'red',
3777+
})
3778+
# ---
3779+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][sensor.kitchen_effect_duration]
3780+
StateSnapshot({
3781+
'attributes': ReadOnlyDict({
3782+
'friendly_name': 'Kitchen Effect duration',
3783+
'icon': 'mdi:timer',
3784+
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
3785+
'unit_of_measurement': <UnitOfTime.SECONDS: 's'>,
3786+
}),
3787+
'context': <ANY>,
3788+
'entity_id': 'sensor.kitchen_effect_duration',
3789+
'last_changed': <ANY>,
3790+
'last_reported': <ANY>,
3791+
'last_updated': <ANY>,
3792+
'state': 'unknown',
3793+
})
3794+
# ---
3795+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][sensor.kitchen_effect_type]
3796+
StateSnapshot({
3797+
'attributes': ReadOnlyDict({
3798+
'friendly_name': 'Kitchen Effect type',
3799+
'icon': 'mdi:lightning-bolt',
3800+
}),
3801+
'context': <ANY>,
3802+
'entity_id': 'sensor.kitchen_effect_type',
3803+
'last_changed': <ANY>,
3804+
'last_reported': <ANY>,
3805+
'last_updated': <ANY>,
3806+
'state': 'open_close',
3807+
})
3808+
# ---
3809+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][sensor.kitchen_notification]
3810+
StateSnapshot({
3811+
'attributes': ReadOnlyDict({
3812+
'friendly_name': 'Kitchen Notification',
3813+
'icon': 'mdi:circle-box',
3814+
}),
3815+
'context': <ANY>,
3816+
'entity_id': 'sensor.kitchen_notification',
3817+
'last_changed': <ANY>,
3818+
'last_reported': <ANY>,
3819+
'last_updated': <ANY>,
3820+
'state': 'doors_open',
3821+
})
3822+
# ---
3823+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][service_calls]
3824+
list([
3825+
])
3826+
# ---
3827+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][switch.doors_open_notification]
3828+
StateSnapshot({
3829+
'attributes': ReadOnlyDict({
3830+
'friendly_name': 'Doors Open Notification',
3831+
'icon': 'mdi:circle-box',
3832+
}),
3833+
'context': <ANY>,
3834+
'entity_id': 'switch.doors_open_notification',
3835+
'last_changed': <ANY>,
3836+
'last_reported': <ANY>,
3837+
'last_updated': <ANY>,
3838+
'state': 'on',
3839+
})
3840+
# ---
3841+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][switch_info]
3842+
dict({
3843+
'disable_clear_notification_id': 'switch.kitchen_disable_config_2x_tap_to_clear_notifications',
3844+
'expiration': ExpirationInfoSnapshot({
3845+
'cancel_listener': None,
3846+
'expires_at': None,
3847+
'started_at': HAFakeDatetime(2025, 5, 20, 3, 51, 32, 3245, tzinfo=zoneinfo.ZoneInfo(key='US/Pacific')),
3848+
}),
3849+
'led_config': tuple(
3850+
dict({
3851+
'brightness': 100.0,
3852+
'color': <Color.RED: 0>,
3853+
'duration': None,
3854+
'effect': <Effect.OPEN_CLOSE: 6>,
3855+
}),
3856+
),
3857+
'led_config_source': dict({
3858+
'type': <LEDConfigSourceType.NOTIFICATION: 'notification'>,
3859+
'value': 'doors_open',
3860+
}),
3861+
'local_protetction_id': 'switch.kitchen_local_protection',
3862+
'priorities': tuple(
3863+
'doors_open',
3864+
),
3865+
})
3866+
# ---
3867+
# name: test_toggle_notifications[doors_open_on,kitchen_override_leds_reset][zha_cluster_commands]
3868+
list([
3869+
ServiceCallSnapshot({
3870+
'context': <ANY>,
3871+
'data': ReadOnlyDict({
3872+
'cluster_id': 64561,
3873+
'cluster_type': 'in',
3874+
'command': 1,
3875+
'command_type': 'server',
3876+
'endpoint_id': 1,
3877+
'ieee': 'mock-ieee:kitchen',
3878+
'manufacturer': 4655,
3879+
'params': dict({
3880+
'led_color': 0,
3881+
'led_duration': 255,
3882+
'led_effect': 6,
3883+
'led_level': 100,
3884+
}),
3885+
}),
3886+
'domain': 'zha',
3887+
'hass': <ANY>,
3888+
'return_response': False,
3889+
'service': 'issue_zigbee_cluster_command',
3890+
}),
3891+
ServiceCallSnapshot({
3892+
'context': <ANY>,
3893+
'data': ReadOnlyDict({
3894+
'cluster_id': 64561,
3895+
'cluster_type': 'in',
3896+
'command': 1,
3897+
'command_type': 'server',
3898+
'endpoint_id': 1,
3899+
'ieee': 'mock-ieee:kitchen',
3900+
'manufacturer': 4655,
3901+
'params': dict({
3902+
'led_color': 90,
3903+
'led_duration': 255,
3904+
'led_effect': 1,
3905+
'led_level': 100,
3906+
}),
3907+
}),
3908+
'domain': 'zha',
3909+
'hass': <ANY>,
3910+
'return_response': False,
3911+
'service': 'issue_zigbee_cluster_command',
3912+
}),
3913+
ServiceCallSnapshot({
3914+
'context': <ANY>,
3915+
'data': ReadOnlyDict({
3916+
'cluster_id': 64561,
3917+
'cluster_type': 'in',
3918+
'command': 1,
3919+
'command_type': 'server',
3920+
'endpoint_id': 1,
3921+
'ieee': 'mock-ieee:kitchen',
3922+
'manufacturer': 4655,
3923+
'params': dict({
3924+
'led_color': 0,
3925+
'led_duration': 255,
3926+
'led_effect': 6,
3927+
'led_level': 100,
3928+
}),
3929+
}),
3930+
'domain': 'zha',
3931+
'hass': <ANY>,
3932+
'return_response': False,
3933+
'service': 'issue_zigbee_cluster_command',
3934+
}),
3935+
])
3936+
# ---
37383937
# name: test_toggle_notifications[doors_open_various_firmware_durations_expired][events]
37393938
list([
37403939
EventSnapshot({

tests/test_init.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,39 @@ def _response_script(name: str, response: dict[str, Any]) -> dict[str, Any]:
530530
"expected_zha_calls": 1,
531531
},
532532
),
533+
Scenario(
534+
"doors_open_on,kitchen_override_leds_reset",
535+
{
536+
"configs": {},
537+
"steps": [
538+
{
539+
"action": f"{SWITCH_DOMAIN}.{SERVICE_TURN_ON}",
540+
"target": "switch.doors_open_notification",
541+
},
542+
{
543+
"action": f"{DOMAIN}.{SERVICE_NAME_OVERRIDE}",
544+
"target": "light.kitchen",
545+
"data": {
546+
ATTR_LED_CONFIG: [
547+
{ATTR_COLOR: "green"},
548+
],
549+
},
550+
},
551+
{
552+
"action": f"{DOMAIN}.{SERVICE_NAME_OVERRIDE}",
553+
"target": "light.kitchen",
554+
"data": {
555+
ATTR_LED_CONFIG: None,
556+
},
557+
},
558+
],
559+
"expected_notification_state": "on",
560+
"expected_notifiation_timer": False,
561+
"expected_switch_timer": False,
562+
"expected_events": 0,
563+
"expected_zha_calls": 3,
564+
},
565+
),
533566
Scenario(
534567
"doors_open_on,kitchen_override_leds_dismissed",
535568
{

0 commit comments

Comments
 (0)