2019-04-03 15:40:03 +00:00
|
|
|
"""Simplepush notification service."""
|
2022-06-24 06:39:48 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
import logging
|
|
|
|
from typing import Any
|
|
|
|
|
2022-10-21 14:13:46 +00:00
|
|
|
from simplepush import BadRequest, UnknownError, send
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2022-09-12 16:07:26 +00:00
|
|
|
ATTR_DATA,
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_TITLE,
|
|
|
|
ATTR_TITLE_DEFAULT,
|
2022-08-04 19:03:26 +00:00
|
|
|
PLATFORM_SCHEMA as BASE_PLATFORM_SCHEMA,
|
2019-07-31 19:25:30 +00:00
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2021-02-13 12:07:11 +00:00
|
|
|
from homeassistant.const import CONF_EVENT, CONF_PASSWORD
|
2022-06-24 06:39:48 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-25 13:33:05 +00:00
|
|
|
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
|
2022-06-24 06:39:48 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2022-08-04 19:03:26 +00:00
|
|
|
from .const import ATTR_EVENT, CONF_DEVICE_KEY, CONF_SALT, DOMAIN
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2022-08-04 19:03:26 +00:00
|
|
|
# Configuring Simplepush under the notify has been removed in 2022.9.0
|
|
|
|
PLATFORM_SCHEMA = BASE_PLATFORM_SCHEMA
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2022-06-24 06:39:48 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2022-06-24 06:39:48 +00:00
|
|
|
|
|
|
|
async def async_get_service(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config: ConfigType,
|
|
|
|
discovery_info: DiscoveryInfoType | None = None,
|
|
|
|
) -> SimplePushNotificationService | None:
|
2018-05-31 21:58:03 +00:00
|
|
|
"""Get the Simplepush notification service."""
|
2022-06-24 06:39:48 +00:00
|
|
|
if discovery_info is None:
|
2022-07-29 16:56:19 +00:00
|
|
|
async_create_issue(
|
|
|
|
hass,
|
|
|
|
DOMAIN,
|
2022-08-04 19:03:26 +00:00
|
|
|
"removed_yaml",
|
2022-07-29 16:56:19 +00:00
|
|
|
breaks_in_ha_version="2022.9.0",
|
|
|
|
is_fixable=False,
|
|
|
|
severity=IssueSeverity.WARNING,
|
2022-08-04 19:03:26 +00:00
|
|
|
translation_key="removed_yaml",
|
2022-06-24 06:39:48 +00:00
|
|
|
)
|
|
|
|
return None
|
|
|
|
|
|
|
|
return SimplePushNotificationService(discovery_info)
|
2018-05-31 21:58:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SimplePushNotificationService(BaseNotificationService):
|
|
|
|
"""Implementation of the notification service for Simplepush."""
|
|
|
|
|
2022-06-24 06:39:48 +00:00
|
|
|
def __init__(self, config: dict[str, Any]) -> None:
|
2018-05-31 21:58:03 +00:00
|
|
|
"""Initialize the Simplepush notification service."""
|
2022-06-24 06:39:48 +00:00
|
|
|
self._device_key: str = config[CONF_DEVICE_KEY]
|
|
|
|
self._event: str | None = config.get(CONF_EVENT)
|
|
|
|
self._password: str | None = config.get(CONF_PASSWORD)
|
|
|
|
self._salt: str | None = config.get(CONF_SALT)
|
2018-05-31 21:58:03 +00:00
|
|
|
|
2022-06-24 06:39:48 +00:00
|
|
|
def send_message(self, message: str, **kwargs: Any) -> None:
|
2018-05-31 21:58:03 +00:00
|
|
|
"""Send a message to a Simplepush user."""
|
|
|
|
title = kwargs.get(ATTR_TITLE, ATTR_TITLE_DEFAULT)
|
|
|
|
|
2022-06-24 06:39:48 +00:00
|
|
|
# event can now be passed in the service data
|
|
|
|
event = None
|
|
|
|
if data := kwargs.get(ATTR_DATA):
|
|
|
|
event = data.get(ATTR_EVENT)
|
|
|
|
|
|
|
|
# use event from config until YAML config is removed
|
|
|
|
event = event or self._event
|
|
|
|
|
|
|
|
try:
|
|
|
|
if self._password:
|
2022-10-21 14:13:46 +00:00
|
|
|
send(
|
|
|
|
key=self._device_key,
|
|
|
|
password=self._password,
|
|
|
|
salt=self._salt,
|
|
|
|
title=title,
|
|
|
|
message=message,
|
2022-06-24 06:39:48 +00:00
|
|
|
event=event,
|
|
|
|
)
|
|
|
|
else:
|
2022-10-21 14:13:46 +00:00
|
|
|
send(key=self._device_key, title=title, message=message, event=event)
|
2022-06-24 06:39:48 +00:00
|
|
|
|
|
|
|
except BadRequest:
|
|
|
|
_LOGGER.error("Bad request. Title or message are too long")
|
|
|
|
except UnknownError:
|
|
|
|
_LOGGER.error("Failed to send the notification")
|