diff --git a/homeassistant/components/motioneye/switch.py b/homeassistant/components/motioneye/switch.py index 2f5d4e5c2b0..08d58a9f6a1 100644 --- a/homeassistant/components/motioneye/switch.py +++ b/homeassistant/components/motioneye/switch.py @@ -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() diff --git a/tests/components/motioneye/test_switch.py b/tests/components/motioneye/test_switch.py index f406fe212b7..05de2f0bbcf 100644 --- a/tests/components/motioneye/test_switch.py +++ b/tests/components/motioneye/test_switch.py @@ -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()