core/homeassistant/components/owlet/__init__.py

79 lines
1.8 KiB
Python
Raw Normal View History

2019-02-16 09:12:16 +00:00
"""Support for Owlet baby monitors."""
import logging
import voluptuous as vol
2019-02-16 16:03:08 +00:00
from homeassistant.const import CONF_NAME, CONF_PASSWORD, CONF_USERNAME
2019-02-16 09:12:16 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.discovery import load_platform
2019-02-16 16:03:08 +00:00
from .const import (
2019-07-31 19:25:30 +00:00
SENSOR_BASE_STATION,
SENSOR_HEART_RATE,
SENSOR_MOVEMENT,
SENSOR_OXYGEN_LEVEL,
)
2019-02-16 09:12:16 +00:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 19:25:30 +00:00
DOMAIN = "owlet"
2019-02-16 09:12:16 +00:00
SENSOR_TYPES = [
SENSOR_OXYGEN_LEVEL,
SENSOR_HEART_RATE,
SENSOR_BASE_STATION,
2019-02-16 16:03:08 +00:00
SENSOR_MOVEMENT,
2019-02-16 09:12:16 +00:00
]
2019-07-31 19:25:30 +00:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_USERNAME): cv.string,
vol.Required(CONF_PASSWORD): cv.string,
vol.Optional(CONF_NAME): cv.string,
}
)
},
extra=vol.ALLOW_EXTRA,
)
2019-02-16 09:12:16 +00:00
def setup(hass, config):
"""Set up owlet component."""
from pyowlet.PyOwlet import PyOwlet
username = config[DOMAIN][CONF_USERNAME]
password = config[DOMAIN][CONF_PASSWORD]
name = config[DOMAIN].get(CONF_NAME)
try:
device = PyOwlet(username, password)
except KeyError:
2019-07-31 19:25:30 +00:00
_LOGGER.error(
"Owlet authentication failed. Please verify your " "credentials are correct"
)
2019-02-16 09:12:16 +00:00
return False
device.update_properties()
if not name:
name = f"{device.baby_name}'s Owlet"
2019-02-16 09:12:16 +00:00
hass.data[DOMAIN] = OwletDevice(device, name, SENSOR_TYPES)
2019-07-31 19:25:30 +00:00
load_platform(hass, "sensor", DOMAIN, {}, config)
load_platform(hass, "binary_sensor", DOMAIN, {}, config)
2019-02-16 09:12:16 +00:00
return True
2019-07-31 19:25:30 +00:00
class OwletDevice:
2019-02-16 09:12:16 +00:00
"""Represents a configured Owlet device."""
def __init__(self, device, name, monitor):
"""Initialize device."""
self.name = name
self.monitor = monitor
self.device = device