2019-02-14 15:01:46 +00:00
|
|
|
"""Native Home Assistant iOS app component."""
|
2017-04-18 06:31:50 +00:00
|
|
|
import datetime
|
2019-02-14 15:01:46 +00:00
|
|
|
import logging
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
from homeassistant import config_entries
|
2016-10-22 06:20:15 +00:00
|
|
|
from homeassistant.components.http import HomeAssistantView
|
2019-02-14 15:01:46 +00:00
|
|
|
from homeassistant.const import HTTP_BAD_REQUEST, HTTP_INTERNAL_SERVER_ERROR
|
2017-11-20 03:47:55 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.exceptions import HomeAssistantError
|
2019-05-13 08:16:55 +00:00
|
|
|
from homeassistant.helpers import config_validation as cv, discovery
|
2017-11-20 03:47:55 +00:00
|
|
|
from homeassistant.util.json import load_json, save_json
|
|
|
|
|
2016-10-22 06:20:15 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "ios"
|
|
|
|
|
|
|
|
CONF_PUSH = "push"
|
|
|
|
CONF_PUSH_CATEGORIES = "categories"
|
|
|
|
CONF_PUSH_CATEGORIES_NAME = "name"
|
|
|
|
CONF_PUSH_CATEGORIES_IDENTIFIER = "identifier"
|
|
|
|
CONF_PUSH_CATEGORIES_ACTIONS = "actions"
|
|
|
|
|
|
|
|
CONF_PUSH_ACTIONS_IDENTIFIER = "identifier"
|
|
|
|
CONF_PUSH_ACTIONS_TITLE = "title"
|
|
|
|
CONF_PUSH_ACTIONS_ACTIVATION_MODE = "activationMode"
|
|
|
|
CONF_PUSH_ACTIONS_AUTHENTICATION_REQUIRED = "authenticationRequired"
|
|
|
|
CONF_PUSH_ACTIONS_DESTRUCTIVE = "destructive"
|
|
|
|
CONF_PUSH_ACTIONS_BEHAVIOR = "behavior"
|
|
|
|
CONF_PUSH_ACTIONS_CONTEXT = "context"
|
|
|
|
CONF_PUSH_ACTIONS_TEXT_INPUT_BUTTON_TITLE = "textInputButtonTitle"
|
|
|
|
CONF_PUSH_ACTIONS_TEXT_INPUT_PLACEHOLDER = "textInputPlaceholder"
|
|
|
|
|
|
|
|
ATTR_FOREGROUND = "foreground"
|
|
|
|
ATTR_BACKGROUND = "background"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
ACTIVATION_MODES = [ATTR_FOREGROUND, ATTR_BACKGROUND]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DEFAULT_BEHAVIOR = "default"
|
|
|
|
ATTR_TEXT_INPUT_BEHAVIOR = "textInput"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
BEHAVIORS = [ATTR_DEFAULT_BEHAVIOR, ATTR_TEXT_INPUT_BEHAVIOR]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LAST_SEEN_AT = "lastSeenAt"
|
2017-04-18 06:31:50 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DEVICE = "device"
|
|
|
|
ATTR_PUSH_TOKEN = "pushToken"
|
|
|
|
ATTR_APP = "app"
|
|
|
|
ATTR_PERMISSIONS = "permissions"
|
|
|
|
ATTR_PUSH_ID = "pushId"
|
|
|
|
ATTR_DEVICE_ID = "deviceId"
|
|
|
|
ATTR_PUSH_SOUNDS = "pushSounds"
|
|
|
|
ATTR_BATTERY = "battery"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_DEVICE_NAME = "name"
|
|
|
|
ATTR_DEVICE_LOCALIZED_MODEL = "localizedModel"
|
|
|
|
ATTR_DEVICE_MODEL = "model"
|
|
|
|
ATTR_DEVICE_PERMANENT_ID = "permanentID"
|
|
|
|
ATTR_DEVICE_SYSTEM_VERSION = "systemVersion"
|
|
|
|
ATTR_DEVICE_TYPE = "type"
|
|
|
|
ATTR_DEVICE_SYSTEM_NAME = "systemName"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_APP_BUNDLE_IDENTIFIER = "bundleIdentifier"
|
|
|
|
ATTR_APP_BUILD_NUMBER = "buildNumber"
|
|
|
|
ATTR_APP_VERSION_NUMBER = "versionNumber"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_LOCATION_PERMISSION = "location"
|
|
|
|
ATTR_NOTIFICATIONS_PERMISSION = "notifications"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
PERMISSIONS = [ATTR_LOCATION_PERMISSION, ATTR_NOTIFICATIONS_PERMISSION]
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
ATTR_BATTERY_STATE = "state"
|
|
|
|
ATTR_BATTERY_LEVEL = "level"
|
|
|
|
|
|
|
|
ATTR_BATTERY_STATE_UNPLUGGED = "Not Charging"
|
|
|
|
ATTR_BATTERY_STATE_CHARGING = "Charging"
|
|
|
|
ATTR_BATTERY_STATE_FULL = "Full"
|
|
|
|
ATTR_BATTERY_STATE_UNKNOWN = "Unknown"
|
|
|
|
|
|
|
|
BATTERY_STATES = [
|
|
|
|
ATTR_BATTERY_STATE_UNPLUGGED,
|
|
|
|
ATTR_BATTERY_STATE_CHARGING,
|
|
|
|
ATTR_BATTERY_STATE_FULL,
|
|
|
|
ATTR_BATTERY_STATE_UNKNOWN,
|
|
|
|
]
|
|
|
|
|
|
|
|
ATTR_DEVICES = "devices"
|
|
|
|
|
|
|
|
ACTION_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PUSH_ACTIONS_IDENTIFIER): vol.Upper,
|
|
|
|
vol.Required(CONF_PUSH_ACTIONS_TITLE): cv.string,
|
|
|
|
vol.Optional(
|
|
|
|
CONF_PUSH_ACTIONS_ACTIVATION_MODE, default=ATTR_BACKGROUND
|
|
|
|
): vol.In(ACTIVATION_MODES),
|
|
|
|
vol.Optional(
|
|
|
|
CONF_PUSH_ACTIONS_AUTHENTICATION_REQUIRED, default=False
|
|
|
|
): cv.boolean,
|
|
|
|
vol.Optional(CONF_PUSH_ACTIONS_DESTRUCTIVE, default=False): cv.boolean,
|
|
|
|
vol.Optional(CONF_PUSH_ACTIONS_BEHAVIOR, default=ATTR_DEFAULT_BEHAVIOR): vol.In(
|
|
|
|
BEHAVIORS
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_PUSH_ACTIONS_TEXT_INPUT_BUTTON_TITLE): cv.string,
|
|
|
|
vol.Optional(CONF_PUSH_ACTIONS_TEXT_INPUT_PLACEHOLDER): cv.string,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
ACTION_SCHEMA_LIST = vol.All(cv.ensure_list, [ACTION_SCHEMA])
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
DOMAIN: {
|
|
|
|
CONF_PUSH: {
|
|
|
|
CONF_PUSH_CATEGORIES: vol.All(
|
|
|
|
cv.ensure_list,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
vol.Required(CONF_PUSH_CATEGORIES_NAME): cv.string,
|
|
|
|
vol.Required(CONF_PUSH_CATEGORIES_IDENTIFIER): vol.Lower,
|
|
|
|
vol.Required(
|
|
|
|
CONF_PUSH_CATEGORIES_ACTIONS
|
|
|
|
): ACTION_SCHEMA_LIST,
|
|
|
|
}
|
|
|
|
],
|
|
|
|
)
|
|
|
|
}
|
2016-10-22 06:20:15 +00:00
|
|
|
}
|
2019-07-31 19:25:30 +00:00
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
|
|
|
|
|
|
|
IDENTIFY_DEVICE_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_DEVICE_NAME): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_LOCALIZED_MODEL): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_MODEL): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_PERMANENT_ID): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_SYSTEM_VERSION): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_TYPE): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_SYSTEM_NAME): cv.string,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
IDENTIFY_DEVICE_SCHEMA_CONTAINER = vol.All(dict, IDENTIFY_DEVICE_SCHEMA)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
IDENTIFY_APP_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_APP_BUNDLE_IDENTIFIER): cv.string,
|
|
|
|
vol.Required(ATTR_APP_BUILD_NUMBER): cv.positive_int,
|
|
|
|
vol.Optional(ATTR_APP_VERSION_NUMBER): cv.string,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
IDENTIFY_APP_SCHEMA_CONTAINER = vol.All(dict, IDENTIFY_APP_SCHEMA)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
IDENTIFY_BATTERY_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_BATTERY_LEVEL): cv.positive_int,
|
|
|
|
vol.Required(ATTR_BATTERY_STATE): vol.In(BATTERY_STATES),
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
IDENTIFY_BATTERY_SCHEMA_CONTAINER = vol.All(dict, IDENTIFY_BATTERY_SCHEMA)
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
IDENTIFY_SCHEMA = vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(ATTR_DEVICE): IDENTIFY_DEVICE_SCHEMA_CONTAINER,
|
|
|
|
vol.Required(ATTR_BATTERY): IDENTIFY_BATTERY_SCHEMA_CONTAINER,
|
|
|
|
vol.Required(ATTR_PUSH_TOKEN): cv.string,
|
|
|
|
vol.Required(ATTR_APP): IDENTIFY_APP_SCHEMA_CONTAINER,
|
|
|
|
vol.Required(ATTR_PERMISSIONS): vol.All(cv.ensure_list, [vol.In(PERMISSIONS)]),
|
|
|
|
vol.Required(ATTR_PUSH_ID): cv.string,
|
|
|
|
vol.Required(ATTR_DEVICE_ID): cv.string,
|
|
|
|
vol.Optional(ATTR_PUSH_SOUNDS): list,
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIGURATION_FILE = ".ios.conf"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
def devices_with_push(hass):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return a dictionary of push enabled targets."""
|
|
|
|
targets = {}
|
2018-09-12 18:17:52 +00:00
|
|
|
for device_name, device in hass.data[DOMAIN][ATTR_DEVICES].items():
|
2016-10-22 06:20:15 +00:00
|
|
|
if device.get(ATTR_PUSH_ID) is not None:
|
|
|
|
targets[device_name] = device.get(ATTR_PUSH_ID)
|
|
|
|
return targets
|
|
|
|
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
def enabled_push_ids(hass):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return a list of push enabled target push IDs."""
|
|
|
|
push_ids = list()
|
2018-09-12 18:17:52 +00:00
|
|
|
for device in hass.data[DOMAIN][ATTR_DEVICES].values():
|
2016-10-22 06:20:15 +00:00
|
|
|
if device.get(ATTR_PUSH_ID) is not None:
|
|
|
|
push_ids.append(device.get(ATTR_PUSH_ID))
|
|
|
|
return push_ids
|
|
|
|
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
def devices(hass):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return a dictionary of all identified devices."""
|
2018-09-12 18:17:52 +00:00
|
|
|
return hass.data[DOMAIN][ATTR_DEVICES]
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
def device_name_for_push_id(hass, push_id):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Return the device name for the push ID."""
|
2018-09-12 18:17:52 +00:00
|
|
|
for device_name, device in hass.data[DOMAIN][ATTR_DEVICES].items():
|
2016-10-22 06:20:15 +00:00
|
|
|
if device.get(ATTR_PUSH_ID) is push_id:
|
|
|
|
return device_name
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the iOS component."""
|
2018-09-12 18:17:52 +00:00
|
|
|
conf = config.get(DOMAIN)
|
|
|
|
|
|
|
|
ios_config = await hass.async_add_executor_job(
|
2019-07-31 19:25:30 +00:00
|
|
|
load_json, hass.config.path(CONFIGURATION_FILE)
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
if ios_config == {}:
|
|
|
|
ios_config[ATTR_DEVICES] = {}
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
ios_config[CONF_PUSH] = (conf or {}).get(CONF_PUSH, {})
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
hass.data[DOMAIN] = ios_config
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
# No entry support for notify component yet
|
2019-07-31 19:25:30 +00:00
|
|
|
discovery.load_platform(hass, "notify", DOMAIN, {}, config)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
if conf is not None:
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN, context={"source": config_entries.SOURCE_IMPORT}
|
|
|
|
)
|
|
|
|
)
|
2018-09-12 18:17:52 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up an iOS entry."""
|
|
|
|
hass.async_create_task(
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.config_entries.async_forward_entry_setup(entry, "sensor")
|
|
|
|
)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.http.register_view(iOSIdentifyDeviceView(hass.config.path(CONFIGURATION_FILE)))
|
2018-09-12 18:17:52 +00:00
|
|
|
hass.http.register_view(iOSPushConfigView(hass.data[DOMAIN][CONF_PUSH]))
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
# pylint: disable=invalid-name
|
|
|
|
class iOSPushConfigView(HomeAssistantView):
|
|
|
|
"""A view that provides the push categories configuration."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/ios/push"
|
|
|
|
name = "api:ios:push"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2016-11-25 21:04:06 +00:00
|
|
|
def __init__(self, push_config):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Init the view."""
|
|
|
|
self.push_config = push_config
|
|
|
|
|
2016-10-25 00:31:45 +00:00
|
|
|
@callback
|
2016-10-22 06:20:15 +00:00
|
|
|
def get(self, request):
|
|
|
|
"""Handle the GET request for the push configuration."""
|
|
|
|
return self.json(self.push_config)
|
|
|
|
|
|
|
|
|
|
|
|
class iOSIdentifyDeviceView(HomeAssistantView):
|
|
|
|
"""A view that accepts device identification requests."""
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
url = "/api/ios/identify"
|
|
|
|
name = "api:ios:identify"
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
def __init__(self, config_path):
|
|
|
|
"""Initiliaze the view."""
|
|
|
|
self._config_path = config_path
|
|
|
|
|
2018-10-01 06:52:42 +00:00
|
|
|
async def post(self, request):
|
2016-10-22 06:20:15 +00:00
|
|
|
"""Handle the POST request for device identification."""
|
|
|
|
try:
|
2018-10-01 06:52:42 +00:00
|
|
|
data = await request.json()
|
2016-10-25 00:31:45 +00:00
|
|
|
except ValueError:
|
2017-04-18 06:31:50 +00:00
|
|
|
return self.json_message("Invalid JSON", HTTP_BAD_REQUEST)
|
2016-10-25 00:31:45 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass = request.app["hass"]
|
2018-09-12 18:17:52 +00:00
|
|
|
|
2017-04-30 22:23:49 +00:00
|
|
|
# Commented for now while iOS app is getting frequent updates
|
|
|
|
# try:
|
|
|
|
# data = IDENTIFY_SCHEMA(req_data)
|
|
|
|
# except vol.Invalid as ex:
|
2018-09-19 13:40:36 +00:00
|
|
|
# return self.json_message(
|
|
|
|
# vol.humanize.humanize_error(request.json, ex),
|
|
|
|
# HTTP_BAD_REQUEST)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2017-12-03 22:59:22 +00:00
|
|
|
data[ATTR_LAST_SEEN_AT] = datetime.datetime.now().isoformat()
|
2017-04-18 06:31:50 +00:00
|
|
|
|
2016-10-22 06:20:15 +00:00
|
|
|
name = data.get(ATTR_DEVICE_ID)
|
|
|
|
|
2018-09-12 18:17:52 +00:00
|
|
|
hass.data[DOMAIN][ATTR_DEVICES][name] = data
|
2016-10-22 06:20:15 +00:00
|
|
|
|
2017-11-20 03:47:55 +00:00
|
|
|
try:
|
2018-09-12 18:17:52 +00:00
|
|
|
save_json(self._config_path, hass.data[DOMAIN])
|
2017-11-20 03:47:55 +00:00
|
|
|
except HomeAssistantError:
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.json_message("Error saving device.", HTTP_INTERNAL_SERVER_ERROR)
|
2016-10-22 06:20:15 +00:00
|
|
|
|
|
|
|
return self.json({"status": "registered"})
|