diff --git a/custom_components/adaptive_lighting/switch.py b/custom_components/adaptive_lighting/switch.py index 65c234af..3d4d4017 100644 --- a/custom_components/adaptive_lighting/switch.py +++ b/custom_components/adaptive_lighting/switch.py @@ -13,7 +13,6 @@ from typing import TYPE_CHECKING, Any import homeassistant.helpers.config_validation as cv import homeassistant.util.dt as dt_util import ulid_transform -import voluptuous as vol from homeassistant.components.light import ( ATTR_BRIGHTNESS, ATTR_COLOR_TEMP_KELVIN, @@ -55,10 +54,9 @@ from homeassistant.core import ( HomeAssistant, ServiceCall, State, - callback, ) from homeassistant.exceptions import ServiceValidationError -from homeassistant.helpers import entity_platform, entity_registry +from homeassistant.helpers import entity_registry from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo from homeassistant.helpers.entity_component import async_update_entity from homeassistant.helpers.event import ( @@ -138,7 +136,6 @@ from .const import ( ICON_COLOR_TEMP, ICON_MAIN, ICON_SLEEP, - SERVICE_CHANGE_SWITCH_SETTINGS, SLEEP_MODE_SWITCH, TURNING_OFF_DELAY, VALIDATION_TUPLES, @@ -161,7 +158,6 @@ if TYPE_CHECKING: from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.typing import NoEventData - _LOGGER = logging.getLogger(__name__) SCAN_INTERVAL = timedelta(seconds=10) @@ -306,15 +302,11 @@ def _switches_from_service_call( for entity_id in switch_entity_ids: ent_entry = ent_reg.async_get(entity_id) if ent_entry is None: - msg = ( - f"adaptive-lighting: Entity '{entity_id}' not found in registry." - ) + msg = f"adaptive-lighting: Entity '{entity_id}' not found in registry." raise ServiceValidationError(msg) config_id = ent_entry.config_entry_id if config_id not in hass.data[DOMAIN]: - msg = ( - f"adaptive-lighting: Entity '{entity_id}' does not belong to this integration or is not loaded." - ) + msg = f"adaptive-lighting: Entity '{entity_id}' does not belong to this integration or is not loaded." raise ServiceValidationError(msg) switches.append(hass.data[DOMAIN][config_id][SWITCH_DOMAIN]) return switches diff --git a/tests/test_switch.py b/tests/test_switch.py index 4b675388..1b3ef10a 100644 --- a/tests/test_switch.py +++ b/tests/test_switch.py @@ -112,7 +112,7 @@ from homeassistant.const import ( STATE_ON, ) from homeassistant.const import __version__ as ha_version -from homeassistant.core import Context, Event, HomeAssistant, State, callback +from homeassistant.core import Context, Event, HomeAssistant, State from homeassistant.exceptions import ServiceValidationError from homeassistant.helpers import area_registry as ar from homeassistant.helpers import entity_registry @@ -2916,10 +2916,11 @@ async def test_adapt_only_on_bare_turn_on_respects_pause_changed_mode(hass, inte f"as manually controlled, color_temp should still be adapted." ) + async def test_service_validation_error_invalid_entity(hass): """Test that ServiceValidationError is raised for invalid entities.""" await setup_lights_and_switch(hass) - + # Test change_switch_settings with non-existent entity with pytest.raises(ServiceValidationError, match="not found in registry"): await hass.services.async_call( @@ -2929,12 +2930,15 @@ async def test_service_validation_error_invalid_entity(hass): blocking=True, ) + async def test_service_validation_error_missing_input(hass): """Test that ServiceValidationError is raised for missing input.""" await setup_lights_and_switch(hass) - + # Test change_switch_settings with no entity and no lights - with pytest.raises(ServiceValidationError, match="Neither a switch nor a light was provided"): + with pytest.raises( + ServiceValidationError, match="Neither a switch nor a light was provided" + ): await hass.services.async_call( DOMAIN, SERVICE_CHANGE_SWITCH_SETTINGS, @@ -2942,35 +2946,44 @@ async def test_service_validation_error_missing_input(hass): blocking=True, ) + async def test_change_switch_settings_multiple_entities(hass): """Test change_switch_settings with multiple entities.""" # Setup two switches await setup_lights(hass) - _, switch1 = await setup_switch(hass, {CONF_LIGHTS: [ENTITY_LIGHT_1], CONF_NAME: "switch1"}) - _, switch2 = await setup_switch(hass, {CONF_LIGHTS: [ENTITY_LIGHT_2], CONF_NAME: "switch2"}) - + _, switch1 = await setup_switch( + hass, {CONF_LIGHTS: [ENTITY_LIGHT_1], CONF_NAME: "switch1"} + ) + _, switch2 = await setup_switch( + hass, {CONF_LIGHTS: [ENTITY_LIGHT_2], CONF_NAME: "switch2"} + ) + assert switch1._sun_light_settings.min_color_temp != 3000 assert switch2._sun_light_settings.min_color_temp != 3000 - + # Call service for both switches await hass.services.async_call( DOMAIN, SERVICE_CHANGE_SWITCH_SETTINGS, { - ATTR_ENTITY_ID: ["switch.adaptive_lighting_switch1", "switch.adaptive_lighting_switch2"], + ATTR_ENTITY_ID: [ + "switch.adaptive_lighting_switch1", + "switch.adaptive_lighting_switch2", + ], "min_color_temp": 3000, - "use_defaults": "configuration" # Required to set new value + "use_defaults": "configuration", # Required to set new value }, blocking=True, ) - + assert switch1._sun_light_settings.min_color_temp == 3000 assert switch2._sun_light_settings.min_color_temp == 3000 + async def test_apply_service_validation(hass): """Test validation for apply service.""" await setup_lights_and_switch(hass) - + with pytest.raises(ServiceValidationError, match="not found in registry"): await hass.services.async_call( DOMAIN, @@ -2979,10 +2992,11 @@ async def test_apply_service_validation(hass): blocking=True, ) + async def test_set_manual_control_validation(hass): """Test validation for set_manual_control service.""" await setup_lights_and_switch(hass) - + with pytest.raises(ServiceValidationError, match="not found in registry"): await hass.services.async_call( DOMAIN,