1 """Support for iOS push notifications."""
3 from __future__
import annotations
5 from http
import HTTPStatus
17 BaseNotificationService,
23 from .
import device_name_for_push_id, devices_with_push, enabled_push_ids
25 _LOGGER = logging.getLogger(__name__)
27 PUSH_URL =
"https://ios-push.home-assistant.io/push"
31 hass: HomeAssistant, target: str, resp: dict[str, Any], level: int = 20
33 """Output rate limit log line at given level."""
34 rate_limits = resp[
"rateLimits"]
35 resetsAt = dt_util.parse_datetime(rate_limits[
"resetsAt"])
36 resetsAtTime = resetsAt - dt_util.utcnow()
if resetsAt
is not None else "---"
38 "iOS push notification rate limits for %s: "
39 "%d sent, %d allowed, %d errors, "
46 rate_limits[
"successful"],
47 rate_limits[
"maximum"],
48 rate_limits[
"errors"],
49 str(resetsAtTime).split(
".", maxsplit=1)[0],
56 discovery_info: DiscoveryInfoType |
None =
None,
57 ) -> iOSNotificationService |
None:
58 """Get the iOS notification service."""
59 if "ios.notify" not in hass.config.components:
61 hass.config.components.add(
"ios.notify")
70 """Implement the notification service for iOS."""
73 """Initialize the service."""
77 """Return a dictionary of registered targets."""
81 """Send a message to the Lambda APNS gateway."""
82 data: dict[str, Any] = {ATTR_MESSAGE: message}
86 kwargs.get(ATTR_TITLE)
is not None
87 and kwargs.get(ATTR_TITLE) != ATTR_TITLE_DEFAULT
89 data[ATTR_TITLE] = kwargs.get(ATTR_TITLE)
91 if not (targets := kwargs.get(ATTR_TARGET)):
94 if kwargs.get(ATTR_DATA)
is not None:
95 data[ATTR_DATA] = kwargs.get(ATTR_DATA)
97 for target
in targets:
99 _LOGGER.error(
"The target (%s) does not exist in .ios.conf", targets)
102 data[ATTR_TARGET] = target
104 req = requests.post(PUSH_URL, json=data, timeout=10)
106 if req.status_code != HTTPStatus.CREATED:
107 fallback_error = req.json().
get(
"errorMessage",
"Unknown error")
109 f
"Internal server error, please try again later: {fallback_error}"
111 message = req.json().
get(
"message", fallback_message)
112 if req.status_code == HTTPStatus.TOO_MANY_REQUESTS:
113 _LOGGER.warning(message)
116 _LOGGER.error(message)
None send_message(self, str message="", **Any kwargs)
dict[str, str] targets(self)
web.Response get(self, web.Request request, str config_key)
None log_rate_limits(HomeAssistant hass, str target, dict[str, Any] resp, int level=20)
iOSNotificationService|None get_service(HomeAssistant hass, ConfigType config, DiscoveryInfoType|None discovery_info=None)
str|None device_name_for_push_id(HomeAssistant hass, str push_id)
list[str] enabled_push_ids(HomeAssistant hass)
dict[str, str] devices_with_push(HomeAssistant hass)