Move foscam service registration to services module (#184622)

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
epenet
2026-10-07 08:00:03 +02:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 38cc1d9dd6
commit 87f6552a67
4 changed files with 100 additions and 50 deletions
+13 -1
View File
@@ -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."""
+13 -49
View File
@@ -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
+9
View File
@@ -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"
@@ -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",
)