From 6ece6743c6b5e5197527ddffc3eb8f1325b11fda Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Fri, 18 Sep 2026 20:49:03 +0200 Subject: [PATCH] Move switch service registration to services module (#182565) --- homeassistant/components/switch/__init__.py | 16 +++++++++------- homeassistant/components/switch/const.py | 10 +++++++++- homeassistant/components/switch/services.py | 16 ++++++++++++++++ 3 files changed, 34 insertions(+), 8 deletions(-) create mode 100644 homeassistant/components/switch/services.py diff --git a/homeassistant/components/switch/__init__.py b/homeassistant/components/switch/__init__.py index 54ab6b2f577e..62c7a44fb2c3 100644 --- a/homeassistant/components/switch/__init__.py +++ b/homeassistant/components/switch/__init__.py @@ -7,7 +7,7 @@ from typing import override from propcache.api import cached_property from homeassistant.config_entries import ConfigEntry -from homeassistant.const import ( +from homeassistant.const import ( # noqa: F401 SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON, @@ -18,13 +18,17 @@ from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity import ToggleEntity, ToggleEntityDescription from homeassistant.helpers.entity_component import EntityComponent from homeassistant.helpers.typing import ConfigType -from homeassistant.util.hass_dict import HassKey -from .const import DEVICE_CLASSES_SCHEMA, DOMAIN, SwitchDeviceClass # noqa: F401 +from .const import ( # noqa: F401 + DATA_COMPONENT, + DEVICE_CLASSES_SCHEMA, + DOMAIN, + SwitchDeviceClass, +) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) -DATA_COMPONENT: HassKey[EntityComponent[SwitchEntity]] = HassKey(DOMAIN) ENTITY_ID_FORMAT = DOMAIN + ".{}" PLATFORM_SCHEMA = cv.PLATFORM_SCHEMA PLATFORM_SCHEMA_BASE = cv.PLATFORM_SCHEMA_BASE @@ -53,9 +57,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ) await component.async_setup(config) - component.async_register_entity_service(SERVICE_TURN_OFF, None, "async_turn_off") - component.async_register_entity_service(SERVICE_TURN_ON, None, "async_turn_on") - component.async_register_entity_service(SERVICE_TOGGLE, None, "async_toggle") + async_setup_services(hass) return True diff --git a/homeassistant/components/switch/const.py b/homeassistant/components/switch/const.py index 4410669bc952..222caf707eb3 100644 --- a/homeassistant/components/switch/const.py +++ b/homeassistant/components/switch/const.py @@ -1,11 +1,19 @@ """Constants for the Switch integration.""" from enum import StrEnum -from typing import Final +from typing import TYPE_CHECKING, Final import probatio +from homeassistant.util.hass_dict import HassKey + +if TYPE_CHECKING: + from homeassistant.helpers.entity_component import EntityComponent + + from . import SwitchEntity + DOMAIN: Final = "switch" +DATA_COMPONENT: HassKey[EntityComponent[SwitchEntity]] = HassKey(DOMAIN) class SwitchDeviceClass(StrEnum): diff --git a/homeassistant/components/switch/services.py b/homeassistant/components/switch/services.py new file mode 100644 index 000000000000..12a4b853f90a --- /dev/null +++ b/homeassistant/components/switch/services.py @@ -0,0 +1,16 @@ +"""Services for the Switch integration.""" + +from homeassistant.const import SERVICE_TOGGLE, SERVICE_TURN_OFF, SERVICE_TURN_ON +from homeassistant.core import HomeAssistant, callback + +from .const import DATA_COMPONENT + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Register the switch services.""" + component = hass.data[DATA_COMPONENT] + + component.async_register_entity_service(SERVICE_TURN_OFF, None, "async_turn_off") + component.async_register_entity_service(SERVICE_TURN_ON, None, "async_turn_on") + component.async_register_entity_service(SERVICE_TOGGLE, None, "async_toggle")