Move switch service registration to services module (#182565)

This commit is contained in:
epenet
2026-09-18 20:49:03 +02:00
committed by GitHub
parent 8620c85779
commit 6ece6743c6
3 changed files with 34 additions and 8 deletions
+9 -7
View File
@@ -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
+9 -1
View File
@@ -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):
@@ -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")