1
0
mirror of https://github.com/home-assistant/core.git synced 2026-04-02 00:20:30 +01:00

Update and fix govee light local (#166454)

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
This commit is contained in:
Galorhallen
2026-03-25 12:08:02 +01:00
committed by GitHub
parent 00cd07736e
commit ee7dd329f0
3 changed files with 75 additions and 3 deletions

View File

@@ -98,7 +98,7 @@ class GoveeLocalApiCoordinator(DataUpdateCoordinator[list[GoveeDevice]]):
"""Set light color in kelvin."""
await device.set_temperature(temperature)
async def set_scene(self, device: GoveeController, scene: str) -> None:
async def set_scene(self, device: GoveeDevice, scene: str) -> None:
"""Set light scene."""
await device.set_scene(scene)

View File

@@ -80,7 +80,6 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
super().__init__(coordinator)
self._device = device
device.set_update_callback(self._update_callback)
self._attr_unique_id = device.fingerprint
@@ -194,9 +193,20 @@ class GoveeLight(CoordinatorEntity[GoveeLocalApiCoordinator], LightEntity):
await self.coordinator.turn_off(self._device)
self.async_write_ha_state()
async def async_added_to_hass(self) -> None:
"""Register update callback when entity is added."""
await super().async_added_to_hass()
self._device.set_update_callback(self._update_callback)
async def async_will_remove_from_hass(self) -> None:
"""Unregister update callback when entity is removed."""
self._device.set_update_callback(None)
await super().async_will_remove_from_hass()
@callback
def _update_callback(self, device: GoveeDevice) -> None:
self.async_write_ha_state()
if self.hass:
self.async_write_ha_state()
def _save_last_color_state(self) -> None:
color_mode = self.color_mode

View File

@@ -631,6 +631,68 @@ async def test_scene_restore_temperature(
assert light.attributes["color_temp_kelvin"] == initial_color
async def test_update_callback_registered_and_triggers_state_update(
hass: HomeAssistant, mock_govee_api: MagicMock
) -> None:
"""Test that update callback is registered and triggers state update."""
device = GoveeDevice(
controller=mock_govee_api,
ip="192.168.1.100",
fingerprint="asdawdqwdqwd",
sku="H615A",
capabilities=DEFAULT_CAPABILITIES,
)
mock_govee_api.devices = [device]
entry = MockConfigEntry(domain=DOMAIN)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert device.update_callback is not None
light = hass.states.get("light.H615A")
assert light is not None
assert light.state == "off"
# Mutate device state and fire callback
await device.turn_on()
device.update_callback(device)
await hass.async_block_till_done()
light = hass.states.get("light.H615A")
assert light is not None
assert light.state == "on"
async def test_update_callback_cleared_on_remove(
hass: HomeAssistant, mock_govee_api: MagicMock
) -> None:
"""Test that update callback is cleared when entity is removed."""
device = GoveeDevice(
controller=mock_govee_api,
ip="192.168.1.100",
fingerprint="asdawdqwdqwd",
sku="H615A",
capabilities=DEFAULT_CAPABILITIES,
)
mock_govee_api.devices = [device]
entry = MockConfigEntry(domain=DOMAIN)
entry.add_to_hass(hass)
assert await hass.config_entries.async_setup(entry.entry_id)
await hass.async_block_till_done()
assert device.update_callback is not None
assert await hass.config_entries.async_remove(entry.entry_id)
await hass.async_block_till_done()
assert device.update_callback is None
async def test_scene_none(hass: HomeAssistant, mock_govee_api: MagicMock) -> None:
"""Test turn on 'none' scene."""