2016-07-12 15:10:33 +00:00
|
|
|
"""
|
|
|
|
Component for Joaoapps Join services.
|
|
|
|
|
2017-05-02 20:47:20 +00:00
|
|
|
For more details about this component, please refer to the documentation at
|
2016-07-12 15:10:33 +00:00
|
|
|
https://home-assistant.io/components/join/
|
|
|
|
"""
|
|
|
|
import logging
|
2017-04-30 05:04:49 +00:00
|
|
|
|
2016-07-12 15:10:33 +00:00
|
|
|
import voluptuous as vol
|
2017-04-30 05:04:49 +00:00
|
|
|
|
2016-07-12 15:10:33 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2017-04-30 05:04:49 +00:00
|
|
|
from homeassistant.const import CONF_NAME, CONF_API_KEY
|
2016-07-12 15:10:33 +00:00
|
|
|
|
2017-05-04 20:14:14 +00:00
|
|
|
REQUIREMENTS = ['python-join-api==0.0.2']
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
DOMAIN = 'joaoapps_join'
|
|
|
|
CONF_DEVICE_ID = 'device_id'
|
2017-05-04 20:14:14 +00:00
|
|
|
CONF_DEVICE_IDS = 'device_ids'
|
|
|
|
CONF_DEVICE_NAMES = 'device_names'
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
2016-07-20 02:14:41 +00:00
|
|
|
DOMAIN: vol.All(cv.ensure_list, [{
|
2017-05-04 20:14:14 +00:00
|
|
|
vol.Required(CONF_API_KEY): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_ID): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_IDS): cv.string,
|
|
|
|
vol.Optional(CONF_DEVICE_NAMES): cv.string,
|
|
|
|
vol.Optional(CONF_NAME): cv.string
|
2016-07-20 02:14:41 +00:00
|
|
|
}])
|
2016-07-12 15:10:33 +00:00
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
|
|
|
|
2017-05-04 20:14:14 +00:00
|
|
|
def register_device(hass, api_key, name, device_id, device_ids, device_names):
|
2017-05-02 20:47:20 +00:00
|
|
|
"""Register services for each join device listed."""
|
2016-07-20 02:14:41 +00:00
|
|
|
from pyjoin import (ring_device, set_wallpaper, send_sms,
|
2016-07-12 15:10:33 +00:00
|
|
|
send_file, send_url, send_notification)
|
|
|
|
|
|
|
|
def ring_service(service):
|
|
|
|
"""Service to ring devices."""
|
2017-05-04 20:14:14 +00:00
|
|
|
ring_device(api_key=api_key, device_id=device_id,
|
|
|
|
device_ids=device_ids, device_names=device_names)
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
def set_wallpaper_service(service):
|
|
|
|
"""Service to set wallpaper on devices."""
|
2017-05-04 20:14:14 +00:00
|
|
|
set_wallpaper(api_key=api_key, device_id=device_id,
|
|
|
|
device_ids=device_ids, device_names=device_names,
|
|
|
|
url=service.data.get('url'))
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
def send_file_service(service):
|
|
|
|
"""Service to send files to devices."""
|
2017-05-04 20:14:14 +00:00
|
|
|
send_file(api_key=api_key, device_id=device_id,
|
|
|
|
device_ids=device_ids, device_names=device_names,
|
|
|
|
url=service.data.get('url'))
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
def send_url_service(service):
|
|
|
|
"""Service to open url on devices."""
|
2017-05-04 20:14:14 +00:00
|
|
|
send_url(api_key=api_key, device_id=device_id,
|
|
|
|
device_ids=device_ids, device_names=device_names,
|
|
|
|
url=service.data.get('url'))
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
def send_tasker_service(service):
|
|
|
|
"""Service to open url on devices."""
|
2017-05-04 20:14:14 +00:00
|
|
|
send_notification(api_key=api_key, device_id=device_id,
|
|
|
|
device_ids=device_ids, device_names=device_names,
|
|
|
|
text=service.data.get('command'))
|
2016-07-12 15:10:33 +00:00
|
|
|
|
|
|
|
def send_sms_service(service):
|
|
|
|
"""Service to send sms from devices."""
|
|
|
|
send_sms(device_id=device_id,
|
2017-05-04 20:14:14 +00:00
|
|
|
device_ids=device_ids,
|
|
|
|
device_names=device_names,
|
2016-07-12 15:10:33 +00:00
|
|
|
sms_number=service.data.get('number'),
|
|
|
|
sms_text=service.data.get('message'),
|
|
|
|
api_key=api_key)
|
|
|
|
|
|
|
|
hass.services.register(DOMAIN, name + 'ring', ring_service)
|
|
|
|
hass.services.register(DOMAIN, name + 'set_wallpaper',
|
|
|
|
set_wallpaper_service)
|
|
|
|
hass.services.register(DOMAIN, name + 'send_sms', send_sms_service)
|
|
|
|
hass.services.register(DOMAIN, name + 'send_file', send_file_service)
|
|
|
|
hass.services.register(DOMAIN, name + 'send_url', send_url_service)
|
|
|
|
hass.services.register(DOMAIN, name + 'send_tasker', send_tasker_service)
|
2016-07-20 02:14:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
def setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Join services."""
|
2016-07-20 02:14:41 +00:00
|
|
|
from pyjoin import get_devices
|
|
|
|
for device in config[DOMAIN]:
|
|
|
|
api_key = device.get(CONF_API_KEY)
|
2017-05-04 20:14:14 +00:00
|
|
|
device_id = device.get(CONF_DEVICE_ID)
|
|
|
|
device_ids = device.get(CONF_DEVICE_IDS)
|
|
|
|
device_names = device.get(CONF_DEVICE_NAMES)
|
2016-07-20 02:14:41 +00:00
|
|
|
name = device.get(CONF_NAME)
|
|
|
|
name = name.lower().replace(" ", "_") + "_" if name else ""
|
|
|
|
if api_key:
|
|
|
|
if not get_devices(api_key):
|
|
|
|
_LOGGER.error("Error connecting to Join, check API key")
|
|
|
|
return False
|
2017-05-04 20:14:14 +00:00
|
|
|
if device_id is None and device_ids is None and device_names is None:
|
|
|
|
_LOGGER.error("No device was provided. Please specify device_id"
|
|
|
|
", device_ids, or device_names")
|
|
|
|
return False
|
|
|
|
|
|
|
|
register_device(hass, api_key, name,
|
|
|
|
device_id, device_ids, device_names)
|
2016-07-12 15:10:33 +00:00
|
|
|
return True
|