From 23f2302b14fdf356edca68a434a363841fb48c0e Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 7 Oct 2026 07:37:53 +0200 Subject: [PATCH] Move modern_forms service registration to services module (#184603) Co-authored-by: Claude Opus 5.5 --- .../components/modern_forms/__init__.py | 12 ++++ homeassistant/components/modern_forms/fan.py | 31 +-------- .../components/modern_forms/light.py | 31 +-------- .../components/modern_forms/services.py | 66 +++++++++++++++++++ 4 files changed, 80 insertions(+), 60 deletions(-) create mode 100644 homeassistant/components/modern_forms/services.py diff --git a/homeassistant/components/modern_forms/__init__.py b/homeassistant/components/modern_forms/__init__.py index 4c0e013a850a..98bcf7018288 100644 --- a/homeassistant/components/modern_forms/__init__.py +++ b/homeassistant/components/modern_forms/__init__.py @@ -8,10 +8,13 @@ from aiomodernforms import ModernFormsConnectionError, ModernFormsError from homeassistant.const import Platform from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError +from homeassistant.helpers import config_validation as cv +from homeassistant.helpers.typing import ConfigType from .const import DOMAIN from .coordinator import ModernFormsConfigEntry, ModernFormsDataUpdateCoordinator from .entity import ModernFormsDeviceEntity +from .services import async_setup_services PLATFORMS = [ Platform.BINARY_SENSOR, @@ -24,6 +27,15 @@ PLATFORMS = [ ] +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Modern Forms integration.""" + async_setup_services(hass) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: ModernFormsConfigEntry) -> bool: """Set up a Modern Forms device from a config entry.""" diff --git a/homeassistant/components/modern_forms/fan.py b/homeassistant/components/modern_forms/fan.py index 7677d0b9cd2b..ea88fa62730f 100644 --- a/homeassistant/components/modern_forms/fan.py +++ b/homeassistant/components/modern_forms/fan.py @@ -3,12 +3,10 @@ from typing import Any, override from aiomodernforms.const import FAN_POWER_OFF, FAN_POWER_ON -import probatio from homeassistant.components.fan import FanEntity, FanEntityFeature from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util.percentage import ( percentage_to_ranged_value, @@ -17,16 +15,7 @@ from homeassistant.util.percentage import ( from homeassistant.util.scaling import int_states_in_range from . import modernforms_exception_handler -from .const import ( - ATTR_SLEEP_TIME, - CLEAR_TIMER, - DOMAIN, - OPT_ON, - OPT_SPEED, - OPT_WIND, - SERVICE_CLEAR_FAN_SLEEP_TIMER, - SERVICE_SET_FAN_SLEEP_TIMER, -) +from .const import CLEAR_TIMER, DOMAIN, OPT_ON, OPT_SPEED, OPT_WIND from .coordinator import ModernFormsConfigEntry, ModernFormsDataUpdateCoordinator from .entity import ModernFormsDeviceEntity @@ -43,24 +32,6 @@ async def async_setup_entry( coordinator = config_entry.runtime_data - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service( - SERVICE_SET_FAN_SLEEP_TIMER, - { - probatio.Required(ATTR_SLEEP_TIME): probatio.All( - probatio.Coerce(int), probatio.Range(min=1, max=1440) - ), - }, - "async_set_fan_sleep_timer", - ) - - platform.async_register_entity_service( - SERVICE_CLEAR_FAN_SLEEP_TIMER, - None, - "async_clear_fan_sleep_timer", - ) - async_add_entities( [ModernFormsFanEntity(entry_id=config_entry.entry_id, coordinator=coordinator)] ) diff --git a/homeassistant/components/modern_forms/light.py b/homeassistant/components/modern_forms/light.py index 981c367ecccc..1049829519da 100644 --- a/homeassistant/components/modern_forms/light.py +++ b/homeassistant/components/modern_forms/light.py @@ -4,7 +4,6 @@ from typing import Any, override from aiomodernforms.const import LIGHT_POWER_OFF, LIGHT_POWER_ON from aiomodernforms.models import Light -import probatio from homeassistant.components.light import ( ATTR_BRIGHTNESS, @@ -14,7 +13,6 @@ from homeassistant.components.light import ( ) from homeassistant.core import HomeAssistant from homeassistant.exceptions import HomeAssistantError -from homeassistant.helpers import entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback from homeassistant.util.percentage import ( percentage_to_ranged_value, @@ -22,16 +20,7 @@ from homeassistant.util.percentage import ( ) from . import modernforms_exception_handler -from .const import ( - ATTR_SLEEP_TIME, - CLEAR_TIMER, - DOMAIN, - OPT_BRIGHTNESS, - OPT_COLOR_TEMP_KELVIN, - OPT_ON, - SERVICE_CLEAR_LIGHT_SLEEP_TIMER, - SERVICE_SET_LIGHT_SLEEP_TIMER, -) +from .const import CLEAR_TIMER, DOMAIN, OPT_BRIGHTNESS, OPT_COLOR_TEMP_KELVIN, OPT_ON from .coordinator import ModernFormsConfigEntry, ModernFormsDataUpdateCoordinator from .entity import ModernFormsDeviceEntity, strip_device_name_prefix @@ -51,24 +40,6 @@ async def async_setup_entry( if not coordinator.data.info.light_type: return - platform = entity_platform.async_get_current_platform() - - platform.async_register_entity_service( - SERVICE_SET_LIGHT_SLEEP_TIMER, - { - probatio.Required(ATTR_SLEEP_TIME): probatio.All( - probatio.Coerce(int), probatio.Range(min=1, max=1440) - ), - }, - "async_set_light_sleep_timer", - ) - - platform.async_register_entity_service( - SERVICE_CLEAR_LIGHT_SLEEP_TIMER, - None, - "async_clear_light_sleep_timer", - ) - async_add_entities( ModernFormsLightEntity( entry_id=config_entry.entry_id, diff --git a/homeassistant/components/modern_forms/services.py b/homeassistant/components/modern_forms/services.py new file mode 100644 index 000000000000..c2a5effd08cd --- /dev/null +++ b/homeassistant/components/modern_forms/services.py @@ -0,0 +1,66 @@ +"""Services for the Modern Forms integration.""" + +import probatio + +from homeassistant.components.fan import DOMAIN as FAN_DOMAIN +from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import service + +from .const import ( + ATTR_SLEEP_TIME, + DOMAIN, + SERVICE_CLEAR_FAN_SLEEP_TIMER, + SERVICE_CLEAR_LIGHT_SLEEP_TIMER, + SERVICE_SET_FAN_SLEEP_TIMER, + SERVICE_SET_LIGHT_SLEEP_TIMER, +) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Modern Forms integration.""" + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_FAN_SLEEP_TIMER, + entity_domain=FAN_DOMAIN, + schema={ + probatio.Required(ATTR_SLEEP_TIME): probatio.All( + probatio.Coerce(int), probatio.Range(min=1, max=1440) + ) + }, + func="async_set_fan_sleep_timer", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_CLEAR_FAN_SLEEP_TIMER, + entity_domain=FAN_DOMAIN, + schema=None, + func="async_clear_fan_sleep_timer", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_SET_LIGHT_SLEEP_TIMER, + entity_domain=LIGHT_DOMAIN, + schema={ + probatio.Required(ATTR_SLEEP_TIME): probatio.All( + probatio.Coerce(int), probatio.Range(min=1, max=1440) + ) + }, + func="async_set_light_sleep_timer", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_CLEAR_LIGHT_SLEEP_TIMER, + entity_domain=LIGHT_DOMAIN, + schema=None, + func="async_clear_light_sleep_timer", + )