Fix motionEye switch refresh bug (#53413)
This commit is contained in:
parent
4b393f215d
commit
ffa7962a37
2 changed files with 32 additions and 2 deletions
|
@ -20,7 +20,7 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant, callback
|
||||
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator
|
||||
|
||||
from . import MotionEyeEntity, listen_for_new_cameras
|
||||
from . import MotionEyeEntity, get_camera_from_cameras, listen_for_new_cameras
|
||||
from .const import CONF_CLIENT, CONF_COORDINATOR, DOMAIN, TYPE_MOTIONEYE_SWITCH_BASE
|
||||
|
||||
MOTIONEYE_SWITCHES = [
|
||||
|
@ -118,3 +118,9 @@ class MotionEyeSwitch(MotionEyeEntity, SwitchEntity):
|
|||
async def async_turn_off(self, **kwargs: Any) -> None:
|
||||
"""Turn off the switch."""
|
||||
await self._async_send_set_camera(False)
|
||||
|
||||
@callback
|
||||
def _handle_coordinator_update(self) -> None:
|
||||
"""Handle updated data from the coordinator."""
|
||||
self._camera = get_camera_from_cameras(self._camera_id, self.coordinator.data)
|
||||
super()._handle_coordinator_update()
|
||||
|
|
|
@ -24,6 +24,7 @@ import homeassistant.util.dt as dt_util
|
|||
from . import (
|
||||
TEST_CAMERA,
|
||||
TEST_CAMERA_ID,
|
||||
TEST_CAMERAS,
|
||||
TEST_SWITCH_ENTITY_ID_BASE,
|
||||
TEST_SWITCH_MOTION_DETECTION_ENTITY_ID,
|
||||
create_mock_motioneye_client,
|
||||
|
@ -38,7 +39,7 @@ async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
|
|||
client = create_mock_motioneye_client()
|
||||
await setup_mock_motioneye_config_entry(hass, client=client)
|
||||
|
||||
# Verify switch is on (as per TEST_COMPONENTS above).
|
||||
# Verify switch is on.
|
||||
entity_state = hass.states.get(TEST_SWITCH_MOTION_DETECTION_ENTITY_ID)
|
||||
assert entity_state
|
||||
assert entity_state.state == "on"
|
||||
|
@ -93,6 +94,29 @@ async def test_switch_turn_on_off(hass: HomeAssistant) -> None:
|
|||
assert entity_state.state == "on"
|
||||
|
||||
|
||||
async def test_switch_state_update_from_coordinator(hass: HomeAssistant) -> None:
|
||||
"""Test that coordinator data impacts state."""
|
||||
client = create_mock_motioneye_client()
|
||||
await setup_mock_motioneye_config_entry(hass, client=client)
|
||||
|
||||
# Verify switch is on.
|
||||
entity_state = hass.states.get(TEST_SWITCH_MOTION_DETECTION_ENTITY_ID)
|
||||
assert entity_state
|
||||
assert entity_state.state == "on"
|
||||
|
||||
updated_cameras = copy.deepcopy(TEST_CAMERAS)
|
||||
updated_cameras["cameras"][0][KEY_MOTION_DETECTION] = False
|
||||
client.async_get_cameras = AsyncMock(return_value=updated_cameras)
|
||||
|
||||
async_fire_time_changed(hass, dt_util.utcnow() + DEFAULT_SCAN_INTERVAL)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
# Verify the switch turns off.
|
||||
entity_state = hass.states.get(TEST_SWITCH_MOTION_DETECTION_ENTITY_ID)
|
||||
assert entity_state
|
||||
assert entity_state.state == "off"
|
||||
|
||||
|
||||
async def test_switch_has_correct_entities(hass: HomeAssistant) -> None:
|
||||
"""Test that the correct switch entities are created."""
|
||||
client = create_mock_motioneye_client()
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue