mirror of
https://github.com/home-assistant/core.git
synced 2026-12-05 05:14:01 -04:00
Move pushover service registration to services module (#184640)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
0c7945de9d
commit
aa5ca82d31
@@ -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
|
||||
|
||||
|
||||
@@ -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,
|
||||
)
|
||||
Reference in New Issue
Block a user