2019-04-03 15:40:03 +00:00
|
|
|
"""APNS Notification platform."""
|
2021-03-23 13:36:43 +00:00
|
|
|
from contextlib import suppress
|
2016-10-18 02:41:49 +00:00
|
|
|
import logging
|
2017-04-24 03:41:09 +00:00
|
|
|
|
2019-10-18 05:13:29 +00:00
|
|
|
from apns2.client import APNsClient
|
|
|
|
from apns2.errors import Unregistered
|
|
|
|
from apns2.payload import Payload
|
2016-10-18 02:41:49 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-12-09 12:57:24 +00:00
|
|
|
from homeassistant.components.device_tracker import DOMAIN as DEVICE_TRACKER_DOMAIN
|
2019-03-28 03:36:13 +00:00
|
|
|
from homeassistant.components.notify import (
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DATA,
|
|
|
|
ATTR_TARGET,
|
|
|
|
PLATFORM_SCHEMA,
|
|
|
|
BaseNotificationService,
|
|
|
|
)
|
2019-10-18 05:13:29 +00:00
|
|
|
from homeassistant.config import load_yaml_config_file
|
|
|
|
from homeassistant.const import ATTR_NAME, CONF_NAME, CONF_PLATFORM
|
|
|
|
from homeassistant.helpers import template as template_helper
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
from homeassistant.helpers.event import track_state_change
|
2019-11-27 22:15:25 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN
|
2019-07-31 19:25:30 +00:00
|
|
|
|
|
|
|
APNS_DEVICES = "apns.yaml"
|
|
|
|
CONF_CERTFILE = "cert_file"
|
|
|
|
CONF_TOPIC = "topic"
|
|
|
|
CONF_SANDBOX = "sandbox"
|
|
|
|
|
|
|
|
ATTR_PUSH_ID = "push_id"
|
|
|
|
|
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PLATFORM): "apns",
|
|
|
|
vol.Required(CONF_NAME): cv.string,
|
|
|
|
vol.Required(CONF_CERTFILE): cv.isfile,
|
|
|
|
vol.Required(CONF_TOPIC): cv.string,
|
|
|
|
vol.Optional(CONF_SANDBOX, default=False): cv.boolean,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
REGISTER_SERVICE_SCHEMA = vol.Schema(
|
|
|
|
{vol.Required(ATTR_PUSH_ID): cv.string, vol.Optional(ATTR_NAME): cv.string}
|
|
|
|
)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
|
2017-01-15 02:53:14 +00:00
|
|
|
def get_service(hass, config, discovery_info=None):
|
2016-10-18 02:41:49 +00:00
|
|
|
"""Return push service."""
|
2020-04-07 20:45:56 +00:00
|
|
|
name = config[CONF_NAME]
|
|
|
|
cert_file = config[CONF_CERTFILE]
|
|
|
|
topic = config[CONF_TOPIC]
|
|
|
|
sandbox = config[CONF_SANDBOX]
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
service = ApnsNotificationService(hass, name, topic, sandbox, cert_file)
|
2017-04-24 03:41:09 +00:00
|
|
|
hass.services.register(
|
2019-09-03 14:11:36 +00:00
|
|
|
DOMAIN, f"apns_{name}", service.register, schema=REGISTER_SERVICE_SCHEMA
|
2019-07-31 19:25:30 +00:00
|
|
|
)
|
2016-10-18 02:41:49 +00:00
|
|
|
return service
|
|
|
|
|
|
|
|
|
2018-07-20 08:45:20 +00:00
|
|
|
class ApnsDevice:
|
2016-10-18 02:41:49 +00:00
|
|
|
"""
|
2017-05-02 20:47:20 +00:00
|
|
|
The APNS Device class.
|
2016-10-18 02:41:49 +00:00
|
|
|
|
2017-05-02 20:47:20 +00:00
|
|
|
Stores information about a device that is registered for push
|
|
|
|
notifications.
|
2016-10-18 02:41:49 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, push_id, name, tracking_device_id=None, disabled=False):
|
2018-05-14 11:05:52 +00:00
|
|
|
"""Initialize APNS Device."""
|
2016-10-18 02:41:49 +00:00
|
|
|
self.device_push_id = push_id
|
|
|
|
self.device_name = name
|
|
|
|
self.tracking_id = tracking_device_id
|
|
|
|
self.device_disabled = disabled
|
|
|
|
|
|
|
|
@property
|
|
|
|
def push_id(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the APNS id for the device."""
|
2016-10-18 02:41:49 +00:00
|
|
|
return self.device_push_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Return the friendly name for the device."""
|
2016-10-18 02:41:49 +00:00
|
|
|
return self.device_name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def tracking_device_id(self):
|
|
|
|
"""
|
2017-05-02 16:18:47 +00:00
|
|
|
Return the device Id.
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
The id of a device that is tracked by the device
|
|
|
|
tracking component.
|
|
|
|
"""
|
|
|
|
return self.tracking_id
|
|
|
|
|
|
|
|
@property
|
|
|
|
def full_tracking_device_id(self):
|
|
|
|
"""
|
2017-05-02 16:18:47 +00:00
|
|
|
Return the fully qualified device id.
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
The full id of a device that is tracked by the device
|
|
|
|
tracking component.
|
|
|
|
"""
|
2019-09-03 14:11:36 +00:00
|
|
|
return f"{DEVICE_TRACKER_DOMAIN}.{self.tracking_id}"
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def disabled(self):
|
2018-05-14 11:05:52 +00:00
|
|
|
"""Return the state of the service."""
|
2016-10-18 02:41:49 +00:00
|
|
|
return self.device_disabled
|
|
|
|
|
|
|
|
def disable(self):
|
2017-09-23 15:15:46 +00:00
|
|
|
"""Disable the device from receiving notifications."""
|
2016-10-18 02:41:49 +00:00
|
|
|
self.device_disabled = True
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Return the comparison."""
|
2016-10-18 02:41:49 +00:00
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
return self.push_id == other.push_id and self.name == other.name
|
|
|
|
return NotImplemented
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
2018-01-27 19:58:27 +00:00
|
|
|
"""Return the comparison."""
|
2016-10-18 02:41:49 +00:00
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
|
2017-03-05 01:15:20 +00:00
|
|
|
def _write_device(out, device):
|
|
|
|
"""Write a single device to file."""
|
|
|
|
attributes = []
|
|
|
|
if device.name is not None:
|
2019-09-03 14:11:36 +00:00
|
|
|
attributes.append(f"name: {device.name}")
|
2017-03-05 01:15:20 +00:00
|
|
|
if device.tracking_device_id is not None:
|
2019-09-03 14:11:36 +00:00
|
|
|
attributes.append(f"tracking_device_id: {device.tracking_device_id}")
|
2017-03-05 01:15:20 +00:00
|
|
|
if device.disabled:
|
2019-07-31 19:25:30 +00:00
|
|
|
attributes.append("disabled: True")
|
2017-03-05 01:15:20 +00:00
|
|
|
|
|
|
|
out.write(device.push_id)
|
|
|
|
out.write(": {")
|
2017-04-24 03:41:09 +00:00
|
|
|
if attributes:
|
2017-03-05 01:15:20 +00:00
|
|
|
separator = ", "
|
|
|
|
out.write(separator.join(attributes))
|
|
|
|
|
|
|
|
out.write("}\n")
|
|
|
|
|
|
|
|
|
2016-10-18 02:41:49 +00:00
|
|
|
class ApnsNotificationService(BaseNotificationService):
|
|
|
|
"""Implement the notification service for the APNS service."""
|
|
|
|
|
|
|
|
def __init__(self, hass, app_name, topic, sandbox, cert_file):
|
|
|
|
"""Initialize APNS application."""
|
|
|
|
self.hass = hass
|
|
|
|
self.app_name = app_name
|
|
|
|
self.sandbox = sandbox
|
|
|
|
self.certificate = cert_file
|
2020-01-03 13:47:06 +00:00
|
|
|
self.yaml_path = hass.config.path(f"{app_name}_{APNS_DEVICES}")
|
2016-10-18 02:41:49 +00:00
|
|
|
self.devices = {}
|
|
|
|
self.device_states = {}
|
|
|
|
self.topic = topic
|
2019-05-19 10:01:29 +00:00
|
|
|
|
2021-03-23 13:36:43 +00:00
|
|
|
with suppress(FileNotFoundError):
|
2016-10-18 02:41:49 +00:00
|
|
|
self.devices = {
|
|
|
|
str(key): ApnsDevice(
|
|
|
|
str(key),
|
2019-07-31 19:25:30 +00:00
|
|
|
value.get("name"),
|
|
|
|
value.get("tracking_device_id"),
|
|
|
|
value.get("disabled", False),
|
2016-10-18 02:41:49 +00:00
|
|
|
)
|
2019-07-31 19:25:30 +00:00
|
|
|
for (key, value) in load_yaml_config_file(self.yaml_path).items()
|
2016-10-18 02:41:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tracking_ids = [
|
|
|
|
device.full_tracking_device_id
|
|
|
|
for (key, device) in self.devices.items()
|
|
|
|
if device.tracking_device_id is not None
|
|
|
|
]
|
2019-07-31 19:25:30 +00:00
|
|
|
track_state_change(hass, tracking_ids, self.device_state_changed_listener)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
def device_state_changed_listener(self, entity_id, from_s, to_s):
|
|
|
|
"""
|
2020-01-31 16:33:00 +00:00
|
|
|
Listen for state change.
|
2016-10-18 02:41:49 +00:00
|
|
|
|
2017-05-02 20:47:20 +00:00
|
|
|
Track device state change if a device has a tracking id specified.
|
2016-10-18 02:41:49 +00:00
|
|
|
"""
|
|
|
|
self.device_states[entity_id] = str(to_s.state)
|
|
|
|
|
|
|
|
def write_devices(self):
|
|
|
|
"""Write all known devices to file."""
|
2021-07-28 07:41:45 +00:00
|
|
|
with open(self.yaml_path, "w+", encoding="utf8") as out:
|
2020-10-28 10:59:07 +00:00
|
|
|
for device in self.devices.values():
|
2017-03-05 01:15:20 +00:00
|
|
|
_write_device(out, device)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
def register(self, call):
|
|
|
|
"""Register a device to receive push messages."""
|
|
|
|
push_id = call.data.get(ATTR_PUSH_ID)
|
|
|
|
|
|
|
|
device_name = call.data.get(ATTR_NAME)
|
|
|
|
current_device = self.devices.get(push_id)
|
2019-07-31 19:25:30 +00:00
|
|
|
current_tracking_id = (
|
|
|
|
None if current_device is None else current_device.tracking_device_id
|
|
|
|
)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
2017-05-02 16:18:47 +00:00
|
|
|
device = ApnsDevice(push_id, device_name, current_tracking_id)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
if current_device is None:
|
|
|
|
self.devices[push_id] = device
|
2021-07-28 07:41:45 +00:00
|
|
|
with open(self.yaml_path, "a", encoding="utf8") as out:
|
2017-03-05 01:15:20 +00:00
|
|
|
_write_device(out, device)
|
2016-10-18 02:41:49 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
if device != current_device:
|
|
|
|
self.devices[push_id] = device
|
|
|
|
self.write_devices()
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
def send_message(self, message=None, **kwargs):
|
|
|
|
"""Send push message to registered devices."""
|
|
|
|
|
|
|
|
apns = APNsClient(
|
2019-07-31 19:25:30 +00:00
|
|
|
self.certificate, use_sandbox=self.sandbox, use_alternative_port=False
|
|
|
|
)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
device_state = kwargs.get(ATTR_TARGET)
|
2021-10-22 09:13:05 +00:00
|
|
|
if (message_data := kwargs.get(ATTR_DATA)) is None:
|
2016-10-18 02:41:49 +00:00
|
|
|
message_data = {}
|
|
|
|
|
|
|
|
if isinstance(message, str):
|
|
|
|
rendered_message = message
|
|
|
|
elif isinstance(message, template_helper.Template):
|
2020-10-26 18:29:10 +00:00
|
|
|
rendered_message = message.render(parse_result=False)
|
2016-10-18 02:41:49 +00:00
|
|
|
else:
|
2019-07-31 19:25:30 +00:00
|
|
|
rendered_message = ""
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
payload = Payload(
|
|
|
|
alert=rendered_message,
|
2019-07-31 19:25:30 +00:00
|
|
|
badge=message_data.get("badge"),
|
|
|
|
sound=message_data.get("sound"),
|
|
|
|
category=message_data.get("category"),
|
|
|
|
custom=message_data.get("custom", {}),
|
|
|
|
content_available=message_data.get("content_available", False),
|
|
|
|
)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
device_update = False
|
|
|
|
|
|
|
|
for push_id, device in self.devices.items():
|
|
|
|
if not device.disabled:
|
|
|
|
state = None
|
|
|
|
if device.tracking_device_id is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
state = self.device_states.get(device.full_tracking_device_id)
|
2016-10-18 02:41:49 +00:00
|
|
|
|
|
|
|
if device_state is None or state == str(device_state):
|
|
|
|
try:
|
2019-07-31 19:25:30 +00:00
|
|
|
apns.send_notification(push_id, payload, topic=self.topic)
|
2016-10-18 02:41:49 +00:00
|
|
|
except Unregistered:
|
2017-05-02 16:18:47 +00:00
|
|
|
logging.error("Device %s has unregistered", push_id)
|
2016-10-18 02:41:49 +00:00
|
|
|
device_update = True
|
|
|
|
device.disable()
|
|
|
|
|
|
|
|
if device_update:
|
|
|
|
self.write_devices()
|
|
|
|
|
|
|
|
return True
|