From 87f6552a67c22a347af092c8850588745ec36b91 Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Wed, 7 Oct 2026 08:00:03 +0200 Subject: [PATCH] Move foscam service registration to services module (#184622) Co-authored-by: Claude Opus 5.5 --- homeassistant/components/foscam/__init__.py | 14 ++++- homeassistant/components/foscam/camera.py | 62 +++++--------------- homeassistant/components/foscam/const.py | 9 +++ homeassistant/components/foscam/services.py | 65 +++++++++++++++++++++ 4 files changed, 100 insertions(+), 50 deletions(-) create mode 100644 homeassistant/components/foscam/services.py diff --git a/homeassistant/components/foscam/__init__.py b/homeassistant/components/foscam/__init__.py index e9ad1e78cfc4..073fc4410ac6 100644 --- a/homeassistant/components/foscam/__init__.py +++ b/homeassistant/components/foscam/__init__.py @@ -10,15 +10,27 @@ from homeassistant.const import ( Platform, ) from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv from homeassistant.helpers.entity_registry import RegistryEntry, async_migrate_entries +from homeassistant.helpers.typing import ConfigType from .config_flow import DEFAULT_RTSP_PORT -from .const import CONF_RTSP_PORT, LOGGER +from .const import CONF_RTSP_PORT, DOMAIN, LOGGER from .coordinator import FoscamConfigEntry, FoscamCoordinator +from .services import async_setup_services PLATFORMS = [Platform.CAMERA, Platform.NUMBER, Platform.SWITCH] +CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) + + +async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: + """Set up the Foscam integration.""" + async_setup_services(hass) + return True + + async def async_setup_entry(hass: HomeAssistant, entry: FoscamConfigEntry) -> bool: """Set up foscam from a config entry.""" diff --git a/homeassistant/components/foscam/camera.py b/homeassistant/components/foscam/camera.py index 4afa6c195a37..0b04322fbf71 100644 --- a/homeassistant/components/foscam/camera.py +++ b/homeassistant/components/foscam/camera.py @@ -4,28 +4,27 @@ import asyncio from typing import override from urllib.parse import quote -import probatio - from homeassistant.components.camera import Camera, CameraEntityFeature from homeassistant.const import CONF_PASSWORD, CONF_USERNAME from homeassistant.core import HomeAssistant -from homeassistant.helpers import config_validation as cv, entity_platform from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback -from .const import CONF_RTSP_PORT, CONF_STREAM, LOGGER, SERVICE_PTZ, SERVICE_PTZ_PRESET +from .const import ( + CONF_RTSP_PORT, + CONF_STREAM, + DIR_BOTTOMLEFT, + DIR_BOTTOMRIGHT, + DIR_DOWN, + DIR_LEFT, + DIR_RIGHT, + DIR_TOPLEFT, + DIR_TOPRIGHT, + DIR_UP, + LOGGER, +) from .coordinator import FoscamConfigEntry, FoscamCoordinator from .entity import FoscamEntity -DIR_UP = "up" -DIR_DOWN = "down" -DIR_LEFT = "left" -DIR_RIGHT = "right" - -DIR_TOPLEFT = "top_left" -DIR_TOPRIGHT = "top_right" -DIR_BOTTOMLEFT = "bottom_left" -DIR_BOTTOMRIGHT = "bottom_right" - MOVEMENT_ATTRS = { DIR_UP: "ptz_move_up", DIR_DOWN: "ptz_move_down", @@ -37,11 +36,6 @@ MOVEMENT_ATTRS = { DIR_BOTTOMRIGHT: "ptz_move_bottom_right", } -DEFAULT_TRAVELTIME = 0.125 - -ATTR_MOVEMENT = "movement" -ATTR_TRAVELTIME = "travel_time" -ATTR_PRESET_NAME = "preset_name" PTZ_GOTO_PRESET_COMMAND = "ptz_goto_preset" @@ -52,36 +46,6 @@ async def async_setup_entry( async_add_entities: AddConfigEntryEntitiesCallback, ) -> None: """Add a Foscam IP camera from a config entry.""" - platform = entity_platform.async_get_current_platform() - platform.async_register_entity_service( - SERVICE_PTZ, - { - probatio.Required(ATTR_MOVEMENT): probatio.In( - [ - DIR_UP, - DIR_DOWN, - DIR_LEFT, - DIR_RIGHT, - DIR_TOPLEFT, - DIR_TOPRIGHT, - DIR_BOTTOMLEFT, - DIR_BOTTOMRIGHT, - ] - ), - probatio.Optional( - ATTR_TRAVELTIME, default=DEFAULT_TRAVELTIME - ): cv.small_float, - }, - "async_perform_ptz", - ) - - platform.async_register_entity_service( - SERVICE_PTZ_PRESET, - { - probatio.Required(ATTR_PRESET_NAME): cv.string, - }, - "async_perform_ptz_preset", - ) coordinator = config_entry.runtime_data diff --git a/homeassistant/components/foscam/const.py b/homeassistant/components/foscam/const.py index 33c1b31aeec2..ad493e81027e 100644 --- a/homeassistant/components/foscam/const.py +++ b/homeassistant/components/foscam/const.py @@ -24,3 +24,12 @@ SUPPORTED_SWITCHES = [ "hdr_switch", "wdr_switch", ] + +DIR_UP = "up" +DIR_DOWN = "down" +DIR_LEFT = "left" +DIR_RIGHT = "right" +DIR_TOPLEFT = "top_left" +DIR_TOPRIGHT = "top_right" +DIR_BOTTOMLEFT = "bottom_left" +DIR_BOTTOMRIGHT = "bottom_right" diff --git a/homeassistant/components/foscam/services.py b/homeassistant/components/foscam/services.py new file mode 100644 index 000000000000..3c07216c1e0e --- /dev/null +++ b/homeassistant/components/foscam/services.py @@ -0,0 +1,65 @@ +"""Services for the Foscam integration.""" + +import probatio + +from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN +from homeassistant.core import HomeAssistant, callback +from homeassistant.helpers import config_validation as cv, service + +from .const import ( + DIR_BOTTOMLEFT, + DIR_BOTTOMRIGHT, + DIR_DOWN, + DIR_LEFT, + DIR_RIGHT, + DIR_TOPLEFT, + DIR_TOPRIGHT, + DIR_UP, + DOMAIN, + SERVICE_PTZ, + SERVICE_PTZ_PRESET, +) + +DEFAULT_TRAVELTIME = 0.125 +ATTR_MOVEMENT = "movement" +ATTR_TRAVELTIME = "travel_time" +ATTR_PRESET_NAME = "preset_name" + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Foscam integration.""" + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_PTZ, + entity_domain=CAMERA_DOMAIN, + schema={ + probatio.Required(ATTR_MOVEMENT): probatio.In( + [ + DIR_UP, + DIR_DOWN, + DIR_LEFT, + DIR_RIGHT, + DIR_TOPLEFT, + DIR_TOPRIGHT, + DIR_BOTTOMLEFT, + DIR_BOTTOMRIGHT, + ] + ), + probatio.Optional( + ATTR_TRAVELTIME, default=DEFAULT_TRAVELTIME + ): cv.small_float, + }, + func="async_perform_ptz", + ) + + service.async_register_platform_entity_service( + hass, + DOMAIN, + SERVICE_PTZ_PRESET, + entity_domain=CAMERA_DOMAIN, + schema={probatio.Required(ATTR_PRESET_NAME): cv.string}, + func="async_perform_ptz_preset", + )