2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Ring Doorbell/Chimes."""
|
2020-01-10 20:35:31 +00:00
|
|
|
import asyncio
|
2019-12-05 05:13:28 +00:00
|
|
|
from datetime import timedelta
|
2020-01-10 20:35:31 +00:00
|
|
|
from functools import partial
|
2017-03-31 15:53:56 +00:00
|
|
|
import logging
|
2020-01-10 20:35:31 +00:00
|
|
|
from pathlib import Path
|
2018-02-11 17:20:28 +00:00
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
from requests.exceptions import ConnectTimeout, HTTPError
|
2019-12-05 05:13:28 +00:00
|
|
|
from ring_doorbell import Ring
|
2017-03-31 15:53:56 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
from homeassistant import config_entries
|
|
|
|
from homeassistant.const import CONF_PASSWORD, CONF_USERNAME
|
2018-02-11 17:20:28 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-12-05 05:13:28 +00:00
|
|
|
from homeassistant.helpers.dispatcher import dispatcher_send
|
|
|
|
from homeassistant.helpers.event import track_time_interval
|
2017-03-31 15:53:56 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-02-14 21:09:22 +00:00
|
|
|
ATTRIBUTION = "Data provided by Ring.com"
|
2017-03-31 15:53:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
NOTIFICATION_ID = "ring_notification"
|
|
|
|
NOTIFICATION_TITLE = "Ring Setup"
|
2017-03-31 15:53:56 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DATA_RING_DOORBELLS = "ring_doorbells"
|
|
|
|
DATA_RING_STICKUP_CAMS = "ring_stickup_cams"
|
|
|
|
DATA_RING_CHIMES = "ring_chimes"
|
2020-01-10 20:35:31 +00:00
|
|
|
DATA_TRACK_INTERVAL = "ring_track_interval"
|
2019-07-31 18:08:40 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
DOMAIN = "ring"
|
|
|
|
DEFAULT_CACHEDB = ".ring_cache.pickle"
|
|
|
|
DEFAULT_ENTITY_NAMESPACE = "ring"
|
|
|
|
SIGNAL_UPDATE_RING = "ring_update"
|
2019-07-31 18:08:40 +00:00
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=10)
|
2017-03-31 15:53:56 +00:00
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
PLATFORMS = ("binary_sensor", "light", "sensor", "switch", "camera")
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{
|
2020-01-10 20:35:31 +00:00
|
|
|
vol.Optional(DOMAIN): vol.Schema(
|
2019-07-31 19:25:30 +00:00
|
|
|
{
|
|
|
|
vol.Required(CONF_USERNAME): cv.string,
|
|
|
|
vol.Required(CONF_PASSWORD): cv.string,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
},
|
|
|
|
extra=vol.ALLOW_EXTRA,
|
|
|
|
)
|
2017-03-31 15:53:56 +00:00
|
|
|
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-04-30 05:04:49 +00:00
|
|
|
"""Set up the Ring component."""
|
2020-01-10 20:35:31 +00:00
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={"source": config_entries.SOURCE_IMPORT},
|
|
|
|
data={
|
|
|
|
"username": config[DOMAIN]["username"],
|
|
|
|
"password": config[DOMAIN]["password"],
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
return True
|
2019-07-31 18:08:40 +00:00
|
|
|
|
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
async def async_setup_entry(hass, entry):
|
|
|
|
"""Set up a config entry."""
|
|
|
|
cache = hass.config.path(DEFAULT_CACHEDB)
|
|
|
|
try:
|
|
|
|
ring = await hass.async_add_executor_job(
|
|
|
|
partial(
|
|
|
|
Ring,
|
|
|
|
username=entry.data["username"],
|
|
|
|
password="invalid-password",
|
|
|
|
cache_file=cache,
|
|
|
|
)
|
|
|
|
)
|
2017-03-31 15:53:56 +00:00
|
|
|
except (ConnectTimeout, HTTPError) as ex:
|
|
|
|
_LOGGER.error("Unable to connect to Ring service: %s", str(ex))
|
2020-01-10 20:35:31 +00:00
|
|
|
hass.components.persistent_notification.async_create(
|
2019-07-31 19:25:30 +00:00
|
|
|
"Error: {}<br />"
|
|
|
|
"You will need to restart hass after fixing."
|
|
|
|
"".format(ex),
|
2017-03-31 15:53:56 +00:00
|
|
|
title=NOTIFICATION_TITLE,
|
2019-07-31 19:25:30 +00:00
|
|
|
notification_id=NOTIFICATION_ID,
|
|
|
|
)
|
2017-03-31 15:53:56 +00:00
|
|
|
return False
|
2019-07-31 18:08:40 +00:00
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
if not ring.is_connected:
|
|
|
|
_LOGGER.error("Unable to connect to Ring service")
|
|
|
|
return False
|
|
|
|
|
|
|
|
await hass.async_add_executor_job(finish_setup_entry, hass, ring)
|
|
|
|
|
|
|
|
for component in PLATFORMS:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(entry, component)
|
|
|
|
)
|
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def finish_setup_entry(hass, ring):
|
|
|
|
"""Finish setting up entry."""
|
|
|
|
hass.data[DATA_RING_CHIMES] = chimes = ring.chimes
|
|
|
|
hass.data[DATA_RING_DOORBELLS] = doorbells = ring.doorbells
|
|
|
|
hass.data[DATA_RING_STICKUP_CAMS] = stickup_cams = ring.stickup_cams
|
|
|
|
|
|
|
|
ring_devices = chimes + doorbells + stickup_cams
|
|
|
|
|
2019-07-31 18:08:40 +00:00
|
|
|
def service_hub_refresh(service):
|
|
|
|
hub_refresh()
|
|
|
|
|
|
|
|
def timer_hub_refresh(event_time):
|
|
|
|
hub_refresh()
|
|
|
|
|
|
|
|
def hub_refresh():
|
|
|
|
"""Call ring to refresh information."""
|
|
|
|
_LOGGER.debug("Updating Ring Hub component")
|
|
|
|
|
|
|
|
for camera in ring_devices:
|
|
|
|
_LOGGER.debug("Updating camera %s", camera.name)
|
|
|
|
camera.update()
|
|
|
|
|
|
|
|
dispatcher_send(hass, SIGNAL_UPDATE_RING)
|
|
|
|
|
|
|
|
# register service
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.register(DOMAIN, "update", service_hub_refresh)
|
2019-07-31 18:08:40 +00:00
|
|
|
|
|
|
|
# register scan interval for ring
|
2020-01-10 20:35:31 +00:00
|
|
|
hass.data[DATA_TRACK_INTERVAL] = track_time_interval(
|
|
|
|
hass, timer_hub_refresh, SCAN_INTERVAL
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_unload_entry(hass, entry):
|
|
|
|
"""Unload Ring entry."""
|
|
|
|
unload_ok = all(
|
|
|
|
await asyncio.gather(
|
|
|
|
*[
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in PLATFORMS
|
|
|
|
]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
if not unload_ok:
|
|
|
|
return False
|
2019-07-31 18:08:40 +00:00
|
|
|
|
2020-01-10 20:35:31 +00:00
|
|
|
await hass.async_add_executor_job(hass.data[DATA_TRACK_INTERVAL])
|
|
|
|
|
|
|
|
hass.services.async_remove(DOMAIN, "update")
|
|
|
|
|
|
|
|
hass.data.pop(DATA_RING_DOORBELLS)
|
|
|
|
hass.data.pop(DATA_RING_STICKUP_CAMS)
|
|
|
|
hass.data.pop(DATA_RING_CHIMES)
|
|
|
|
hass.data.pop(DATA_TRACK_INTERVAL)
|
|
|
|
|
|
|
|
return unload_ok
|
|
|
|
|
|
|
|
|
|
|
|
async def async_remove_entry(hass, entry):
|
|
|
|
"""Act when an entry is removed."""
|
|
|
|
await hass.async_add_executor_job(Path(hass.config.path(DEFAULT_CACHEDB)).unlink)
|