Skip to content

Commit 91e315c

Browse files
committed
fix: persist execute switch state across HA restarts
SmartThingsExecuteSwitch used `_assumed_on = True` in __init__, so beep and display_lighting always showed ON after HA restart even when they were turned OFF before. ARTIK051 doesn't report OCF execute state, so `_get_ocf_options()` returns None. Fix: Use `_attr_assumed_on` as a class-level default and restore the last known state from HA's state registry in `async_added_to_hass()` via `async_get_last_state()`. If no previous state exists, defaults to ON (beep is enabled by default on Samsung ACs).
1 parent 49508a9 commit 91e315c

2 files changed

Lines changed: 13 additions & 5 deletions

File tree

custom_components/smartthings/manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@
1010
"codeowners": ["@blka"],
1111
"iot_class": "cloud_push",
1212
"loggers": ["pysmartthings"],
13-
"version": "1.1.0"
13+
"version": "1.1.1"
1414
}

custom_components/smartthings/switch.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,7 @@ class SmartThingsExecuteSwitch(SmartThingsEntity, SwitchEntity):
645645
"""Define a SmartThings OCF execute switch for Samsung AC devices."""
646646

647647
entity_description: SmartThingsExecuteSwitchEntityDescription
648+
_attr_assumed_on: bool = True
648649

649650
def __init__(
650651
self,
@@ -658,7 +659,14 @@ def __init__(
658659
self._attr_unique_id = (
659660
f"{device.device.device_id}_{MAIN}_execute_{entity_description.key}"
660661
)
661-
self._assumed_on = True
662+
663+
async def async_added_to_hass(self) -> None:
664+
"""Restore last known state after HA restart."""
665+
await super().async_added_to_hass()
666+
last_state = await self.async_get_last_state()
667+
if last_state is not None:
668+
self._attr_assumed_on = last_state.state == "on"
669+
self.async_write_ha_state()
662670

663671
def _get_ocf_options(self) -> list[str] | None:
664672
"""Extract OCF options list from execute capability data attribute."""
@@ -699,7 +707,7 @@ def is_on(self) -> bool:
699707
options = self._get_ocf_options()
700708
if options is not None:
701709
return self.entity_description.off_option not in options
702-
return self._assumed_on
710+
return self._attr_assumed_on
703711

704712
async def async_turn_on(self, **kwargs: Any) -> None:
705713
"""Turn the switch on."""
@@ -708,7 +716,7 @@ async def async_turn_on(self, **kwargs: Any) -> None:
708716
Command.EXECUTE,
709717
[OCF_MODE_HREF, {OCF_OPTIONS_KEY: [self.entity_description.on_option]}],
710718
)
711-
self._assumed_on = True
719+
self._attr_assumed_on = True
712720
self.async_write_ha_state()
713721

714722
async def async_turn_off(self, **kwargs: Any) -> None:
@@ -718,5 +726,5 @@ async def async_turn_off(self, **kwargs: Any) -> None:
718726
Command.EXECUTE,
719727
[OCF_MODE_HREF, {OCF_OPTIONS_KEY: [self.entity_description.off_option]}],
720728
)
721-
self._assumed_on = False
729+
self._attr_assumed_on = False
722730
self.async_write_ha_state()

0 commit comments

Comments
 (0)