2015-03-08 14:14:44 +00:00
|
|
|
"""
|
|
|
|
Support for Vera sensors.
|
2015-03-08 12:52:50 +00:00
|
|
|
|
2015-10-20 20:08:06 +00:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 12:12:18 +00:00
|
|
|
https://home-assistant.io/components/sensor.vera/
|
2015-03-08 12:52:50 +00:00
|
|
|
"""
|
2015-03-02 10:09:00 +00:00
|
|
|
import logging
|
2017-04-25 07:17:25 +00:00
|
|
|
from datetime import timedelta
|
2016-02-19 05:27:50 +00:00
|
|
|
|
2015-03-08 21:10:31 +00:00
|
|
|
from homeassistant.const import (
|
2016-04-20 03:30:44 +00:00
|
|
|
TEMP_CELSIUS, TEMP_FAHRENHEIT)
|
2016-02-19 05:27:50 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2017-03-11 18:06:46 +00:00
|
|
|
from homeassistant.components.sensor import ENTITY_ID_FORMAT
|
2017-04-18 10:01:23 +00:00
|
|
|
from homeassistant.util import convert
|
2016-03-15 09:17:09 +00:00
|
|
|
from homeassistant.components.vera import (
|
2017-03-11 18:06:46 +00:00
|
|
|
VERA_CONTROLLER, VERA_DEVICES, VeraDevice)
|
2015-09-09 03:11:25 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
DEPENDENCIES = ['vera']
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2015-03-08 12:52:50 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2017-04-25 07:17:25 +00:00
|
|
|
SCAN_INTERVAL = timedelta(seconds=5)
|
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2016-10-30 08:58:34 +00:00
|
|
|
def setup_platform(hass, config, add_devices, discovery_info=None):
|
2017-05-02 16:18:47 +00:00
|
|
|
"""Set up the Vera controller devices."""
|
2016-10-30 08:58:34 +00:00
|
|
|
add_devices(
|
2016-03-15 09:17:09 +00:00
|
|
|
VeraSensor(device, VERA_CONTROLLER)
|
|
|
|
for device in VERA_DEVICES['sensor'])
|
2015-03-07 01:59:13 +00:00
|
|
|
|
2015-03-08 14:58:11 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
class VeraSensor(VeraDevice, Entity):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Representation of a Vera Sensor."""
|
2015-03-02 10:09:00 +00:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
def __init__(self, vera_device, controller):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Initialize the sensor."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self.current_value = None
|
2015-03-29 13:51:03 +00:00
|
|
|
self._temperature_units = None
|
2017-04-25 07:17:25 +00:00
|
|
|
self.last_changed_time = None
|
2016-03-15 09:17:09 +00:00
|
|
|
VeraDevice.__init__(self, vera_device, controller)
|
2017-03-11 18:06:46 +00:00
|
|
|
self.entity_id = ENTITY_ID_FORMAT.format(self.vera_id)
|
2015-03-02 10:09:00 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the name of the sensor."""
|
2015-03-02 10:09:00 +00:00
|
|
|
return self.current_value
|
|
|
|
|
2015-03-29 13:51:03 +00:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Return the unit of measurement of this entity, if any."""
|
2016-01-15 09:21:48 +00:00
|
|
|
if self.vera_device.category == "Temperature Sensor":
|
|
|
|
return self._temperature_units
|
|
|
|
elif self.vera_device.category == "Light Sensor":
|
|
|
|
return 'lux'
|
|
|
|
elif self.vera_device.category == "Humidity Sensor":
|
|
|
|
return '%'
|
2017-04-18 10:01:23 +00:00
|
|
|
elif self.vera_device.category == "Power meter":
|
|
|
|
return 'watts'
|
2015-03-29 13:51:03 +00:00
|
|
|
|
2015-03-02 10:09:00 +00:00
|
|
|
def update(self):
|
2016-03-08 15:46:34 +00:00
|
|
|
"""Update the state."""
|
2015-03-02 10:09:00 +00:00
|
|
|
if self.vera_device.category == "Temperature Sensor":
|
2016-08-05 05:37:30 +00:00
|
|
|
self.current_value = self.vera_device.temperature
|
|
|
|
|
2016-01-15 10:52:58 +00:00
|
|
|
vera_temp_units = (
|
|
|
|
self.vera_device.vera_controller.temperature_units)
|
2015-03-29 13:51:03 +00:00
|
|
|
|
|
|
|
if vera_temp_units == 'F':
|
|
|
|
self._temperature_units = TEMP_FAHRENHEIT
|
|
|
|
else:
|
2016-04-20 03:30:44 +00:00
|
|
|
self._temperature_units = TEMP_CELSIUS
|
2015-03-29 13:51:03 +00:00
|
|
|
|
2015-03-02 10:09:00 +00:00
|
|
|
elif self.vera_device.category == "Light Sensor":
|
2016-01-15 21:30:58 +00:00
|
|
|
self.current_value = self.vera_device.light
|
2016-01-15 09:21:48 +00:00
|
|
|
elif self.vera_device.category == "Humidity Sensor":
|
2016-01-15 21:30:58 +00:00
|
|
|
self.current_value = self.vera_device.humidity
|
2017-04-25 07:17:25 +00:00
|
|
|
elif self.vera_device.category == "Scene Controller":
|
|
|
|
value = self.vera_device.get_last_scene_id(True)
|
|
|
|
time = self.vera_device.get_last_scene_time(True)
|
|
|
|
if time == self.last_changed_time:
|
|
|
|
self.current_value = None
|
|
|
|
else:
|
|
|
|
self.current_value = value
|
|
|
|
self.last_changed_time = time
|
2017-04-18 10:01:23 +00:00
|
|
|
elif self.vera_device.category == "Power meter":
|
|
|
|
power = convert(self.vera_device.power, float, 0)
|
|
|
|
self.current_value = int(round(power, 0))
|
2015-03-02 10:09:00 +00:00
|
|
|
elif self.vera_device.category == "Sensor":
|
2016-01-15 21:30:58 +00:00
|
|
|
tripped = self.vera_device.is_tripped
|
|
|
|
self.current_value = 'Tripped' if tripped else 'Not Tripped'
|
2015-03-02 10:09:00 +00:00
|
|
|
else:
|
|
|
|
self.current_value = 'Unknown'
|