diff --git a/homeassistant/components/zoneminder/__init__.py b/homeassistant/components/zoneminder/__init__.py index c2e57b0448b..241c2729653 100644 --- a/homeassistant/components/zoneminder/__init__.py +++ b/homeassistant/components/zoneminder/__init__.py @@ -7,8 +7,6 @@ import voluptuous as vol from zoneminder.zm import ZoneMinder from homeassistant.const import ( - ATTR_ID, - ATTR_NAME, CONF_HOST, CONF_PASSWORD, CONF_PATH, @@ -17,11 +15,14 @@ from homeassistant.const import ( CONF_VERIFY_SSL, Platform, ) -from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.core import HomeAssistant from homeassistant.helpers import config_validation as cv from homeassistant.helpers.discovery import async_load_platform from homeassistant.helpers.typing import ConfigType +from .const import DOMAIN +from .services import register_services + _LOGGER = logging.getLogger(__name__) CONF_PATH_ZMS = "path_zms" @@ -31,7 +32,6 @@ DEFAULT_PATH_ZMS = "/zm/cgi-bin/nph-zms" DEFAULT_SSL = False DEFAULT_TIMEOUT = 10 DEFAULT_VERIFY_SSL = True -DOMAIN = "zoneminder" HOST_CONFIG_SCHEMA = vol.Schema( { @@ -49,11 +49,6 @@ CONFIG_SCHEMA = vol.Schema( {DOMAIN: vol.All(cv.ensure_list, [HOST_CONFIG_SCHEMA])}, extra=vol.ALLOW_EXTRA ) -SERVICE_SET_RUN_STATE = "set_run_state" -SET_RUN_STATE_SCHEMA = vol.Schema( - {vol.Required(ATTR_ID): cv.string, vol.Required(ATTR_NAME): cv.string} -) - async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the ZoneMinder component.""" @@ -86,22 +81,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: ex, ) - def set_active_state(call: ServiceCall) -> None: - """Set the ZoneMinder run state to the given state name.""" - zm_id = call.data[ATTR_ID] - state_name = call.data[ATTR_NAME] - if zm_id not in hass.data[DOMAIN]: - _LOGGER.error("Invalid ZoneMinder host provided: %s", zm_id) - if not hass.data[DOMAIN][zm_id].set_active_state(state_name): - _LOGGER.error( - "Unable to change ZoneMinder state. Host: %s, state: %s", - zm_id, - state_name, - ) - - hass.services.async_register( - DOMAIN, SERVICE_SET_RUN_STATE, set_active_state, schema=SET_RUN_STATE_SCHEMA - ) + register_services(hass) hass.async_create_task( async_load_platform(hass, Platform.BINARY_SENSOR, DOMAIN, {}, config) diff --git a/homeassistant/components/zoneminder/const.py b/homeassistant/components/zoneminder/const.py new file mode 100644 index 00000000000..82423adb790 --- /dev/null +++ b/homeassistant/components/zoneminder/const.py @@ -0,0 +1,3 @@ +"""Support for ZoneMinder.""" + +DOMAIN = "zoneminder" diff --git a/homeassistant/components/zoneminder/services.py b/homeassistant/components/zoneminder/services.py new file mode 100644 index 00000000000..14ce873ec14 --- /dev/null +++ b/homeassistant/components/zoneminder/services.py @@ -0,0 +1,40 @@ +"""Support for ZoneMinder.""" + +import logging + +import voluptuous as vol + +from homeassistant.const import ATTR_ID, ATTR_NAME +from homeassistant.core import HomeAssistant, ServiceCall +from homeassistant.helpers import config_validation as cv + +from .const import DOMAIN + +_LOGGER = logging.getLogger(__name__) + +SERVICE_SET_RUN_STATE = "set_run_state" +SET_RUN_STATE_SCHEMA = vol.Schema( + {vol.Required(ATTR_ID): cv.string, vol.Required(ATTR_NAME): cv.string} +) + + +def _set_active_state(call: ServiceCall) -> None: + """Set the ZoneMinder run state to the given state name.""" + zm_id = call.data[ATTR_ID] + state_name = call.data[ATTR_NAME] + if zm_id not in call.hass.data[DOMAIN]: + _LOGGER.error("Invalid ZoneMinder host provided: %s", zm_id) + if not call.hass.data[DOMAIN][zm_id].set_active_state(state_name): + _LOGGER.error( + "Unable to change ZoneMinder state. Host: %s, state: %s", + zm_id, + state_name, + ) + + +def register_services(hass: HomeAssistant) -> None: + """Register ZoneMinder services.""" + + hass.services.async_register( + DOMAIN, SERVICE_SET_RUN_STATE, _set_active_state, schema=SET_RUN_STATE_SCHEMA + )