diff --git a/homeassistant/components/switcher_kis/__init__.py b/homeassistant/components/switcher_kis/__init__.py index 68daa96a61f7..b7fa6aa1a361 100644 --- a/homeassistant/components/switcher_kis/__init__.py +++ b/homeassistant/components/switcher_kis/__init__.py @@ -8,10 +8,12 @@ from aioswitcher.device import SwitcherBase from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_TOKEN, EVENT_HOMEASSISTANT_STOP, Platform from homeassistant.core import Event, HomeAssistant, callback -from homeassistant.helpers import device_registry as dr +from homeassistant.helpers import config_validation as cv, device_registry as dr +from homeassistant.helpers.typing import ConfigType from .const import DOMAIN from .coordinator import SwitcherDataUpdateCoordinator +from .services import async_setup_services PLATFORMS = [ Platform.BUTTON, @@ -28,6 +30,15 @@ _LOGGER = logging.getLogger(__name__) type SwitcherConfigEntry = ConfigEntry[dict[str, SwitcherDataUpdateCoordinator]] +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Switcher integration.""" + async_setup_services(hass) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: SwitcherConfigEntry) -> bool: """Set up Switcher from a config entry.""" diff --git a/homeassistant/components/switcher_kis/services.py b/homeassistant/components/switcher_kis/services.py new file mode 100644 index 000000000000..f3bc9fb7b5e1 --- /dev/null +++ b/homeassistant/components/switcher_kis/services.py @@ -0,0 +1,50 @@ +"""Services for the Switcher integration.""" + +import probatio + +from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchDeviceClass +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv, service +from homeassistant.helpers.typing import VolDictType + +from .const import ( + CONF_AUTO_OFF, + CONF_TIMER_MINUTES, + DOMAIN, + SERVICE_SET_AUTO_OFF_NAME, + SERVICE_TURN_ON_WITH_TIMER_NAME, +) + +SERVICE_SET_AUTO_OFF_SCHEMA: VolDictType = { + probatio.Required(CONF_AUTO_OFF): cv.time_period_str, +} +SERVICE_TURN_ON_WITH_TIMER_SCHEMA: VolDictType = { + probatio.Required(CONF_TIMER_MINUTES): probatio.All( + cv.positive_int, probatio.Range(min=1, max=150) + ), +} + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Switcher integration.""" + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_AUTO_OFF_NAME, + entity_domain=SWITCH_DOMAIN, + schema=SERVICE_SET_AUTO_OFF_SCHEMA, + func="async_set_auto_off_service", + entity_device_classes=(SwitchDeviceClass.SWITCH,), + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_TURN_ON_WITH_TIMER_NAME, + entity_domain=SWITCH_DOMAIN, + schema=SERVICE_TURN_ON_WITH_TIMER_SCHEMA, + func="async_turn_on_with_timer_service", + entity_device_classes=(SwitchDeviceClass.SWITCH,), + ) diff --git a/homeassistant/components/switcher_kis/switch.py b/homeassistant/components/switcher_kis/switch.py index c88bab6834cb..da9c25266642 100644 --- a/homeassistant/components/switcher_kis/switch.py +++ b/homeassistant/components/switcher_kis/switch.py @@ -10,24 +10,15 @@ from aioswitcher.device import ( ShutterChildLock, SwitcherShutter, ) -import probatio from homeassistant.components.switch import SwitchDeviceClass, SwitchEntity from homeassistant.const import EntityCategory from homeassistant.core import HomeAssistant, callback -from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from homeassistant.helpers.typing import VolDictType from . import SwitcherConfigEntry -from .const import ( - CONF_AUTO_OFF, - CONF_TIMER_MINUTES, - SERVICE_SET_AUTO_OFF_NAME, - SERVICE_TURN_ON_WITH_TIMER_NAME, - SIGNAL_DEVICE_ADD, -) +from .const import SIGNAL_DEVICE_ADD from .coordinator import SwitcherDataUpdateCoordinator from .entity import SwitcherEntity @@ -37,16 +28,6 @@ API_CONTROL_DEVICE = "control_device" API_SET_AUTO_SHUTDOWN = "set_auto_shutdown" API_SET_CHILD_LOCK = "set_shutter_child_lock" -SERVICE_SET_AUTO_OFF_SCHEMA: VolDictType = { - probatio.Required(CONF_AUTO_OFF): cv.time_period_str, -} - -SERVICE_TURN_ON_WITH_TIMER_SCHEMA: VolDictType = { - probatio.Required(CONF_TIMER_MINUTES): probatio.All( - cv.positive_int, probatio.Range(min=1, max=150) - ), -} - async def async_setup_entry( hass: HomeAssistant, @@ -54,21 +35,6 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Set up Switcher switch from config entry.""" - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service( - SERVICE_SET_AUTO_OFF_NAME, - SERVICE_SET_AUTO_OFF_SCHEMA, - "async_set_auto_off_service", - entity_device_classes=(SwitchDeviceClass.SWITCH,), - ) - - platform.async_register_entity_service( - SERVICE_TURN_ON_WITH_TIMER_NAME, - SERVICE_TURN_ON_WITH_TIMER_SCHEMA, - "async_turn_on_with_timer_service", - entity_device_classes=(SwitchDeviceClass.SWITCH,), - ) @callback def async_add_switch(coordinator: SwitcherDataUpdateCoordinator) -> None: