diff --git a/homeassistant/components/pushover/__init__.py b/homeassistant/components/pushover/__init__.py index 08afe497c9e1..02f80ce41444 100644 --- a/homeassistant/components/pushover/__init__.py +++ b/homeassistant/components/pushover/__init__.py @@ -3,30 +3,19 @@ from dataclasses import dataclass from typing import TYPE_CHECKING -import probatio from pushover_complete import BadAPIRequestError, PushoverAPI from requests.exceptions import RequestException from urllib3.exceptions import HTTPError from homeassistant.config_entries import ConfigEntry from homeassistant.const import CONF_API_KEY, CONF_NAME, Platform -from homeassistant.core import HomeAssistant, ServiceCall -from homeassistant.exceptions import ( - ConfigEntryAuthFailed, - ConfigEntryNotReady, - ServiceValidationError, -) +from homeassistant.core import HomeAssistant +from homeassistant.exceptions import ConfigEntryAuthFailed, ConfigEntryNotReady from homeassistant.helpers import config_validation as cv, discovery from homeassistant.helpers.typing import ConfigType -from .const import ( - ATTR_ENTRY_ID, - ATTR_TAG, - CONF_USER_KEY, - DATA_HASS_CONFIG, - DOMAIN, - SERVICE_CANCEL, -) +from .const import CONF_USER_KEY, DATA_HASS_CONFIG, DOMAIN +from .services import async_setup_services if TYPE_CHECKING: from .notify import PushoverNotificationService @@ -35,13 +24,6 @@ PLATFORMS = [Platform.NOTIFY] CONFIG_SCHEMA = cv.config_entry_only_config_schema(DOMAIN) -SERVICE_CANCEL_SCHEMA = probatio.Schema( - { - probatio.Required(ATTR_ENTRY_ID): cv.string, - probatio.Optional(ATTR_TAG): cv.string, - } -) - @dataclass class PushoverRuntimeData: @@ -59,32 +41,7 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: hass.data[DATA_HASS_CONFIG] = config - async def _async_cancel_service_handler(call: ServiceCall) -> None: - """Cancel emergency notifications for the targeted config entry.""" - entry_id: str = call.data[ATTR_ENTRY_ID] - entry: PushoverConfigEntry | None = hass.config_entries.async_get_entry( - entry_id - ) - if entry is None: - raise ServiceValidationError( - f"Pushover config entry {entry_id} does not exist" - ) - - notify_service = entry.runtime_data.notify_service - if notify_service is None: - raise ServiceValidationError( - f"Pushover config entry {entry_id} has no notify service set up" - ) - - tag: str = call.data.get(ATTR_TAG, "") - await hass.async_add_executor_job(notify_service.cancel_by_tag, tag) - - hass.services.async_register( - DOMAIN, - SERVICE_CANCEL, - _async_cancel_service_handler, - schema=SERVICE_CANCEL_SCHEMA, - ) + async_setup_services(hass) return True diff --git a/homeassistant/components/pushover/services.py b/homeassistant/components/pushover/services.py new file mode 100644 index 000000000000..0b19b800c9fd --- /dev/null +++ b/homeassistant/components/pushover/services.py @@ -0,0 +1,51 @@ +"""Services for the Pushover integration.""" + +from typing import TYPE_CHECKING + +import probatio + +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.exceptions import ServiceValidationError +from homeassistant.helpers import config_validation as cv + +from .const import ATTR_ENTRY_ID, ATTR_TAG, DOMAIN, SERVICE_CANCEL + +if TYPE_CHECKING: + from . import PushoverConfigEntry + +SERVICE_CANCEL_SCHEMA = probatio.Schema( + { + probatio.Required(ATTR_ENTRY_ID): cv.string, + probatio.Optional(ATTR_TAG): cv.string, + } +) + + +async def _async_cancel_service_handler(call: ServiceCall) -> None: + """Cancel emergency notifications for the targeted config entry.""" + hass = call.hass + entry_id: str = call.data[ATTR_ENTRY_ID] + entry: PushoverConfigEntry | None = hass.config_entries.async_get_entry(entry_id) + if entry is None: + raise ServiceValidationError(f"Pushover config entry {entry_id} does not exist") + + notify_service = entry.runtime_data.notify_service + if notify_service is None: + raise ServiceValidationError( + f"Pushover config entry {entry_id} has no notify service set up" + ) + + tag: str = call.data.get(ATTR_TAG, "") + await hass.async_add_executor_job(notify_service.cancel_by_tag, tag) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Pushover integration.""" + + hass.services.async_register( + DOMAIN, + SERVICE_CANCEL, + _async_cancel_service_handler, + schema=SERVICE_CANCEL_SCHEMA, + )