2019-02-13 20:21:14 +00:00
|
|
|
"""Sensor support for Skybell Doorbells."""
|
2017-10-08 18:14:39 +00:00
|
|
|
from datetime import timedelta
|
|
|
|
import logging
|
|
|
|
|
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant.components.sensor import PLATFORM_SCHEMA
|
2019-07-31 19:25:30 +00:00
|
|
|
from homeassistant.const import CONF_ENTITY_NAMESPACE, CONF_MONITORED_CONDITIONS
|
2017-10-08 18:14:39 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from . import DEFAULT_ENTITY_NAMESPACE, DOMAIN as SKYBELL_DOMAIN, SkybellDevice
|
|
|
|
|
2017-10-08 18:14:39 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(seconds=30)
|
|
|
|
|
|
|
|
# Sensor types: Name, icon
|
2019-07-31 19:25:30 +00:00
|
|
|
SENSOR_TYPES = {"chime_level": ["Chime Level", "bell-ring"]}
|
2017-10-08 18:14:39 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_ENTITY_NAMESPACE, default=DEFAULT_ENTITY_NAMESPACE
|
|
|
|
): cv.string,
|
|
|
|
vol.Required(CONF_MONITORED_CONDITIONS, default=[]): vol.All(
|
|
|
|
cv.ensure_list, [vol.In(SENSOR_TYPES)]
|
|
|
|
),
|
|
|
|
}
|
|
|
|
)
|
2017-10-08 18:14:39 +00:00
|
|
|
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
def setup_platform(hass, config, add_entities, discovery_info=None):
|
2017-10-08 18:14:39 +00:00
|
|
|
"""Set up the platform for a Skybell device."""
|
|
|
|
skybell = hass.data.get(SKYBELL_DOMAIN)
|
|
|
|
|
|
|
|
sensors = []
|
|
|
|
for sensor_type in config.get(CONF_MONITORED_CONDITIONS):
|
|
|
|
for device in skybell.get_devices():
|
|
|
|
sensors.append(SkybellSensor(device, sensor_type))
|
|
|
|
|
2018-08-24 14:37:30 +00:00
|
|
|
add_entities(sensors, True)
|
2017-10-08 18:14:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class SkybellSensor(SkybellDevice):
|
|
|
|
"""A sensor implementation for Skybell devices."""
|
|
|
|
|
|
|
|
def __init__(self, device, sensor_type):
|
|
|
|
"""Initialize a sensor for a Skybell device."""
|
|
|
|
super().__init__(device)
|
|
|
|
self._sensor_type = sensor_type
|
2019-07-31 19:25:30 +00:00
|
|
|
self._icon = "mdi:{}".format(SENSOR_TYPES[self._sensor_type][1])
|
|
|
|
self._name = "{0} {1}".format(
|
|
|
|
self._device.name, SENSOR_TYPES[self._sensor_type][0]
|
|
|
|
)
|
2017-10-08 18:14:39 +00:00
|
|
|
self._state = None
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the name of the sensor."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
|
|
|
"""Return the state of the sensor."""
|
|
|
|
return self._state
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self):
|
|
|
|
"""Icon to use in the frontend, if any."""
|
|
|
|
return self._icon
|
|
|
|
|
|
|
|
def update(self):
|
|
|
|
"""Get the latest data and updates the state."""
|
|
|
|
super().update()
|
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
if self._sensor_type == "chime_level":
|
2017-10-08 18:14:39 +00:00
|
|
|
self._state = self._device.outdoor_chime_level
|