Skip to content

Commit ee7dd32

Browse files
GalorhallenCopilot
andauthored
Update and fix govee light local (home-assistant#166454)
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 00cd077 commit ee7dd32

3 files changed

Lines changed: 75 additions & 3 deletions

File tree

homeassistant/components/govee_light_local/coordinator.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ async def set_temperature(self, device: GoveeDevice, temperature: int) -> None:
9898
"""Set light color in kelvin."""
9999
await device.set_temperature(temperature)
100100

101-
async def set_scene(self, device: GoveeController, scene: str) -> None:
101+
async def set_scene(self, device: GoveeDevice, scene: str) -> None:
102102
"""Set light scene."""
103103
await device.set_scene(scene)
104104

homeassistant/components/govee_light_local/light.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,6 @@ def __init__(
8080

8181
super().__init__(coordinator)
8282
self._device = device
83-
device.set_update_callback(self._update_callback)
8483

8584
self._attr_unique_id = device.fingerprint
8685

@@ -194,9 +193,20 @@ async def async_turn_off(self, **kwargs: Any) -> None:
194193
await self.coordinator.turn_off(self._device)
195194
self.async_write_ha_state()
196195

196+
async def async_added_to_hass(self) -> None:
197+
"""Register update callback when entity is added."""
198+
await super().async_added_to_hass()
199+
self._device.set_update_callback(self._update_callback)
200+
201+
async def async_will_remove_from_hass(self) -> None:
202+
"""Unregister update callback when entity is removed."""
203+
self._device.set_update_callback(None)
204+
await super().async_will_remove_from_hass()
205+
197206
@callback
198207
def _update_callback(self, device: GoveeDevice) -> None:
199-
self.async_write_ha_state()
208+
if self.hass:
209+
self.async_write_ha_state()
200210

201211
def _save_last_color_state(self) -> None:
202212
color_mode = self.color_mode

tests/components/govee_light_local/test_light.py

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,68 @@ async def test_scene_restore_temperature(
631631
assert light.attributes["color_temp_kelvin"] == initial_color
632632

633633

634+
async def test_update_callback_registered_and_triggers_state_update(
635+
hass: HomeAssistant, mock_govee_api: MagicMock
636+
) -> None:
637+
"""Test that update callback is registered and triggers state update."""
638+
device = GoveeDevice(
639+
controller=mock_govee_api,
640+
ip="192.168.1.100",
641+
fingerprint="asdawdqwdqwd",
642+
sku="H615A",
643+
capabilities=DEFAULT_CAPABILITIES,
644+
)
645+
mock_govee_api.devices = [device]
646+
647+
entry = MockConfigEntry(domain=DOMAIN)
648+
entry.add_to_hass(hass)
649+
650+
assert await hass.config_entries.async_setup(entry.entry_id)
651+
await hass.async_block_till_done()
652+
653+
assert device.update_callback is not None
654+
655+
light = hass.states.get("light.H615A")
656+
assert light is not None
657+
assert light.state == "off"
658+
659+
# Mutate device state and fire callback
660+
await device.turn_on()
661+
device.update_callback(device)
662+
await hass.async_block_till_done()
663+
664+
light = hass.states.get("light.H615A")
665+
assert light is not None
666+
assert light.state == "on"
667+
668+
669+
async def test_update_callback_cleared_on_remove(
670+
hass: HomeAssistant, mock_govee_api: MagicMock
671+
) -> None:
672+
"""Test that update callback is cleared when entity is removed."""
673+
device = GoveeDevice(
674+
controller=mock_govee_api,
675+
ip="192.168.1.100",
676+
fingerprint="asdawdqwdqwd",
677+
sku="H615A",
678+
capabilities=DEFAULT_CAPABILITIES,
679+
)
680+
mock_govee_api.devices = [device]
681+
682+
entry = MockConfigEntry(domain=DOMAIN)
683+
entry.add_to_hass(hass)
684+
685+
assert await hass.config_entries.async_setup(entry.entry_id)
686+
await hass.async_block_till_done()
687+
688+
assert device.update_callback is not None
689+
690+
assert await hass.config_entries.async_remove(entry.entry_id)
691+
await hass.async_block_till_done()
692+
693+
assert device.update_callback is None
694+
695+
634696
async def test_scene_none(hass: HomeAssistant, mock_govee_api: MagicMock) -> None:
635697
"""Test turn on 'none' scene."""
636698

0 commit comments

Comments
 (0)