diff --git a/homeassistant/components/persistent_notification/__init__.py b/homeassistant/components/persistent_notification/__init__.py index 633c42e8615b..4bae2ca4dfb3 100644 --- a/homeassistant/components/persistent_notification/__init__.py +++ b/homeassistant/components/persistent_notification/__init__.py @@ -8,7 +8,7 @@ from typing import Any import probatio from homeassistant.components import websocket_api -from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.core import HomeAssistant, callback from homeassistant.helpers import config_validation as cv from homeassistant.helpers.dispatcher import async_dispatcher_connect from homeassistant.helpers.typing import ConfigType @@ -33,10 +33,7 @@ from .helpers import ( # noqa: F401 create, dismiss, ) - -SCHEMA_SERVICE_NOTIFICATION = probatio.Schema( - {probatio.Required(ATTR_NOTIFICATION_ID): cv.string} -) +from .services import async_setup_services _LOGGER = logging.getLogger(__name__) @@ -46,44 +43,7 @@ CONFIG_SCHEMA = cv.empty_config_schema(DOMAIN) async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: """Set up the persistent notification component.""" - @callback - def create_service(call: ServiceCall) -> None: - """Handle a create notification service call.""" - async_create( - hass, - call.data[ATTR_MESSAGE], - call.data.get(ATTR_TITLE), - call.data.get(ATTR_NOTIFICATION_ID), - ) - - @callback - def dismiss_service(call: ServiceCall) -> None: - """Handle the dismiss notification service call.""" - async_dismiss(hass, call.data[ATTR_NOTIFICATION_ID]) - - @callback - def dismiss_all_service(call: ServiceCall) -> None: - """Handle the dismiss all notification service call.""" - async_dismiss_all(hass) - - hass.services.async_register( - DOMAIN, - "create", - create_service, - probatio.Schema( - { - probatio.Required(ATTR_MESSAGE): cv.string, - probatio.Optional(ATTR_TITLE): cv.string, - probatio.Optional(ATTR_NOTIFICATION_ID): cv.string, - } - ), - ) - - hass.services.async_register( - DOMAIN, "dismiss", dismiss_service, SCHEMA_SERVICE_NOTIFICATION - ) - - hass.services.async_register(DOMAIN, "dismiss_all", dismiss_all_service, None) + async_setup_services(hass) websocket_api.async_register_command(hass, websocket_get_notifications) websocket_api.async_register_command(hass, websocket_subscribe_notifications) diff --git a/homeassistant/components/persistent_notification/services.py b/homeassistant/components/persistent_notification/services.py new file mode 100644 index 000000000000..040645f7ea21 --- /dev/null +++ b/homeassistant/components/persistent_notification/services.py @@ -0,0 +1,63 @@ +"""Services for the Persistent Notification integration.""" + +import probatio + +from homeassistant.core import HomeAssistant, ServiceCall, callback +from homeassistant.helpers import config_validation as cv + +from .const import ATTR_MESSAGE, ATTR_NOTIFICATION_ID, ATTR_TITLE, DOMAIN +from .helpers import async_create, async_dismiss, async_dismiss_all + +SCHEMA_SERVICE_NOTIFICATION = probatio.Schema( + {probatio.Required(ATTR_NOTIFICATION_ID): cv.string} +) + + +@callback +def _create_service(call: ServiceCall) -> None: + """Handle a create notification service call.""" + hass = call.hass + async_create( + hass, + call.data[ATTR_MESSAGE], + call.data.get(ATTR_TITLE), + call.data.get(ATTR_NOTIFICATION_ID), + ) + + +@callback +def _dismiss_service(call: ServiceCall) -> None: + """Handle the dismiss notification service call.""" + hass = call.hass + async_dismiss(hass, call.data[ATTR_NOTIFICATION_ID]) + + +@callback +def _dismiss_all_service(call: ServiceCall) -> None: + """Handle the dismiss all notification service call.""" + hass = call.hass + async_dismiss_all(hass) + + +@callback +def async_setup_services(hass: HomeAssistant) -> None: + """Set up the services for the Persistent Notification integration.""" + + hass.services.async_register( + DOMAIN, + "create", + _create_service, + probatio.Schema( + { + probatio.Required(ATTR_MESSAGE): cv.string, + probatio.Optional(ATTR_TITLE): cv.string, + probatio.Optional(ATTR_NOTIFICATION_ID): cv.string, + } + ), + ) + + hass.services.async_register( + DOMAIN, "dismiss", _dismiss_service, SCHEMA_SERVICE_NOTIFICATION + ) + + hass.services.async_register(DOMAIN, "dismiss_all", _dismiss_all_service, None)