async def test_dynamically_handle_segments( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test if a new/deleted segment is dynamically added/removed.""" await init_integration(hass, aioclient_mock) assert hass.states.get("light.wled_rgb_light_master") assert hass.states.get("light.wled_rgb_light_segment_0") assert hass.states.get("light.wled_rgb_light_segment_1") data = json.loads(load_fixture("wled/rgb_single_segment.json")) device = WLEDDevice(data) # Test removal if segment went missing, including the master entity with patch( "homeassistant.components.wled.WLED.update", return_value=device, ): async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() assert hass.states.get("light.wled_rgb_light_segment_0") assert not hass.states.get("light.wled_rgb_light_segment_1") assert not hass.states.get("light.wled_rgb_light_master") # Test adding if segment shows up again, including the master entity async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() assert hass.states.get("light.wled_rgb_light_master") assert hass.states.get("light.wled_rgb_light_segment_0") assert hass.states.get("light.wled_rgb_light_segment_1")
async def test_dynamically_handle_segments( hass: HomeAssistant, init_integration: MockConfigEntry, mock_wled: MagicMock, ) -> None: """Test if a new/deleted segment is dynamically added/removed.""" assert hass.states.get("light.wled_rgb_light_master") assert hass.states.get("light.wled_rgb_light_segment_0") assert hass.states.get("light.wled_rgb_light_segment_1") return_value = mock_wled.update.return_value mock_wled.update.return_value = WLEDDevice( json.loads(load_fixture("wled/rgb_single_segment.json"))) async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() assert hass.states.get("light.wled_rgb_light_segment_0") assert not hass.states.get("light.wled_rgb_light_segment_1") assert not hass.states.get("light.wled_rgb_light_master") # Test adding if segment shows up again, including the master entity mock_wled.update.return_value = return_value async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() assert hass.states.get("light.wled_rgb_light_master") assert hass.states.get("light.wled_rgb_light_segment_0") assert hass.states.get("light.wled_rgb_light_segment_1")
def mock_wled_config_flow( request: pytest.FixtureRequest, ) -> Generator[None, MagicMock, None]: """Return a mocked WLED client.""" with patch("homeassistant.components.wled.config_flow.WLED", autospec=True) as wled_mock: wled = wled_mock.return_value wled.update.return_value = WLEDDevice( json.loads(load_fixture("wled/rgb.json"))) yield wled
def mock_wled( request: pytest.FixtureRequest) -> Generator[None, MagicMock, None]: """Return a mocked WLED client.""" fixture: str = "wled/rgb.json" if hasattr(request, "param") and request.param: fixture = request.param device = WLEDDevice(json.loads(load_fixture(fixture))) with patch("homeassistant.components.wled.coordinator.WLED", autospec=True) as wled_mock: wled = wled_mock.return_value wled.update.return_value = device wled.connected = False yield wled
async def test_color_palette_dynamically_handle_segments( hass: HomeAssistant, enable_all: None, init_integration: MockConfigEntry, mock_wled: MagicMock, ) -> None: """Test if a new/deleted segment is dynamically added/removed.""" segment0 = hass.states.get("select.wled_rgb_light_color_palette") segment1 = hass.states.get("select.wled_rgb_light_segment_1_color_palette") assert segment0 assert segment0.state == "Default" assert not segment1 return_value = mock_wled.update.return_value mock_wled.update.return_value = WLEDDevice( json.loads(load_fixture("wled/rgb.json")) ) async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() segment0 = hass.states.get("select.wled_rgb_light_color_palette") segment1 = hass.states.get("select.wled_rgb_light_segment_1_color_palette") assert segment0 assert segment0.state == "Default" assert segment1 assert segment1.state == "Random Cycle" # Test adding if segment shows up again, including the master entity mock_wled.update.return_value = return_value async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() segment0 = hass.states.get("select.wled_rgb_light_color_palette") segment1 = hass.states.get("select.wled_rgb_light_segment_1_color_palette") assert segment0 assert segment0.state == "Default" assert segment1 assert segment1.state == STATE_UNAVAILABLE
async def test_switch_dynamically_handle_segments( hass: HomeAssistant, init_integration: MockConfigEntry, mock_wled: MagicMock, ) -> None: """Test if a new/deleted segment is dynamically added/removed.""" segment0 = hass.states.get("switch.wled_rgb_light_reverse") segment1 = hass.states.get("switch.wled_rgb_light_segment_1_reverse") assert segment0 assert segment0.state == STATE_OFF assert not segment1 # Test adding a segment dynamically... return_value = mock_wled.update.return_value mock_wled.update.return_value = WLEDDevice( json.loads(load_fixture("wled/rgb.json")) ) async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() segment0 = hass.states.get("switch.wled_rgb_light_reverse") segment1 = hass.states.get("switch.wled_rgb_light_segment_1_reverse") assert segment0 assert segment0.state == STATE_OFF assert segment1 assert segment1.state == STATE_ON # Test remove segment again... mock_wled.update.return_value = return_value async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() segment0 = hass.states.get("switch.wled_rgb_light_reverse") segment1 = hass.states.get("switch.wled_rgb_light_segment_1_reverse") assert segment0 assert segment0.state == STATE_OFF assert segment1 assert segment1.state == STATE_UNAVAILABLE
async def test_single_segment_behavior(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog) -> None: """Test the behavior of the integration with a single segment.""" await init_integration(hass, aioclient_mock) data = json.loads(load_fixture("wled/rgb_single_segment.json")) device = WLEDDevice(data) # Test absent master with patch( "homeassistant.components.wled.WLED.update", return_value=device, ): async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() assert not hass.states.get("light.wled_rgb_light_master") state = hass.states.get("light.wled_rgb_light_segment_0") assert state assert state.state == STATE_ON # Test segment brightness takes master into account device.state.brightness = 100 device.state.segments[0].brightness = 255 with patch( "homeassistant.components.wled.WLED.update", return_value=device, ): async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() state = hass.states.get("light.wled_rgb_light_segment_0") assert state assert state.attributes.get(ATTR_BRIGHTNESS) == 100 # Test segment is off when master is off device.state.on = False with patch( "homeassistant.components.wled.WLED.update", return_value=device, ): async_fire_time_changed(hass, dt_util.utcnow() + SCAN_INTERVAL) await hass.async_block_till_done() state = hass.states.get("light.wled_rgb_light_segment_0") assert state assert state.state == STATE_OFF # Test master is turned off when turning off a single segment with patch("wled.WLED.master") as master_mock: await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_OFF, { ATTR_ENTITY_ID: "light.wled_rgb_light_segment_0", ATTR_TRANSITION: 5 }, blocking=True, ) await hass.async_block_till_done() master_mock.assert_called_once_with( on=False, transition=50, ) # Test master is turned on when turning on a single segment, and segment # brightness is set to 255. with patch("wled.WLED.master") as master_mock, patch( "wled.WLED.segment") as segment_mock: await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, { ATTR_ENTITY_ID: "light.wled_rgb_light_segment_0", ATTR_TRANSITION: 5, ATTR_BRIGHTNESS: 42, }, blocking=True, ) await hass.async_block_till_done() master_mock.assert_called_once_with(on=True, transition=50, brightness=42) segment_mock.assert_called_once_with(on=True, segment_id=0, brightness=255)