2015-03-09 01:14:44 +11:00
|
|
|
"""
|
|
|
|
Support for Vera sensors.
|
2015-03-08 23:52:50 +11:00
|
|
|
|
2015-10-20 22:08:06 +02:00
|
|
|
For more details about this platform, please refer to the documentation at
|
2015-11-09 13:12:18 +01:00
|
|
|
https://home-assistant.io/components/sensor.vera/
|
2015-03-08 23:52:50 +11:00
|
|
|
"""
|
2015-03-02 21:09:00 +11:00
|
|
|
import logging
|
2016-02-18 21:27:50 -08:00
|
|
|
|
|
|
|
import homeassistant.util.dt as dt_util
|
2015-03-09 08:10:31 +11:00
|
|
|
from homeassistant.const import (
|
2016-02-18 21:27:50 -08:00
|
|
|
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
|
2016-03-15 09:17:09 +00:00
|
|
|
TEMP_CELCIUS, TEMP_FAHRENHEIT)
|
2016-02-18 21:27:50 -08:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2016-03-15 09:17:09 +00:00
|
|
|
from homeassistant.components.vera import (
|
|
|
|
VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
|
2015-09-08 20:11:25 -07:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
DEPENDENCIES = ['vera']
|
2015-03-02 21:09:00 +11:00
|
|
|
|
2015-03-08 23:52:50 +11:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2015-03-02 21:09:00 +11:00
|
|
|
|
2015-03-09 01:58:11 +11:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Perform the setup for Vera controller devices."""
|
2016-03-15 09:17:09 +00:00
|
|
|
add_devices_callback(
|
|
|
|
VeraSensor(device, VERA_CONTROLLER)
|
|
|
|
for device in VERA_DEVICES['sensor'])
|
2015-03-07 12:59:13 +11:00
|
|
|
|
2015-03-09 01:58:11 +11:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
class VeraSensor(VeraDevice, Entity):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Representation of a Vera Sensor."""
|
2015-03-02 21:09:00 +11:00
|
|
|
|
2016-03-15 09:17:09 +00:00
|
|
|
def __init__(self, vera_device, controller):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Initialize the sensor."""
|
2016-03-15 09:17:09 +00:00
|
|
|
self.current_value = None
|
2015-03-30 00:51:03 +11:00
|
|
|
self._temperature_units = None
|
2016-03-15 09:17:09 +00:00
|
|
|
VeraDevice.__init__(self, vera_device, controller)
|
2015-03-02 21:09:00 +11:00
|
|
|
|
|
|
|
@property
|
|
|
|
def state(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the name of the sensor."""
|
2015-03-02 21:09:00 +11:00
|
|
|
return self.current_value
|
|
|
|
|
2015-03-30 00:51:03 +11:00
|
|
|
@property
|
|
|
|
def unit_of_measurement(self):
|
2016-03-08 16:46:34 +01: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 '%'
|
2015-03-30 00:51:03 +11:00
|
|
|
|
2015-03-02 21:09:00 +11:00
|
|
|
@property
|
2016-02-06 22:28:29 -08:00
|
|
|
def device_state_attributes(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Return the state attributes."""
|
2015-11-29 13:49:05 -08:00
|
|
|
attr = {}
|
2015-03-02 21:09:00 +11:00
|
|
|
if self.vera_device.has_battery:
|
2015-03-09 07:11:35 +11:00
|
|
|
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
|
2015-03-02 21:09:00 +11:00
|
|
|
|
|
|
|
if self.vera_device.is_armable:
|
2016-01-15 21:30:58 +00:00
|
|
|
armed = self.vera_device.is_armed
|
2016-01-15 21:42:51 +00:00
|
|
|
attr[ATTR_ARMED] = 'True' if armed else 'False'
|
2015-03-02 21:09:00 +11:00
|
|
|
|
|
|
|
if self.vera_device.is_trippable:
|
2016-01-15 21:30:58 +00:00
|
|
|
last_tripped = self.vera_device.last_trip
|
2015-05-18 18:54:25 +10:00
|
|
|
if last_tripped is not None:
|
|
|
|
utc_time = dt_util.utc_from_timestamp(int(last_tripped))
|
2016-04-16 00:55:35 -07:00
|
|
|
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
|
2015-05-18 18:54:25 +10:00
|
|
|
else:
|
|
|
|
attr[ATTR_LAST_TRIP_TIME] = None
|
2016-01-15 21:30:58 +00:00
|
|
|
tripped = self.vera_device.is_tripped
|
2016-01-15 21:42:51 +00:00
|
|
|
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
|
2015-03-02 21:09:00 +11:00
|
|
|
|
|
|
|
attr['Vera Device Id'] = self.vera_device.vera_device_id
|
|
|
|
return attr
|
|
|
|
|
|
|
|
def update(self):
|
2016-03-08 16:46:34 +01:00
|
|
|
"""Update the state."""
|
2015-03-02 21:09:00 +11:00
|
|
|
if self.vera_device.category == "Temperature Sensor":
|
2016-01-15 21:30:58 +00:00
|
|
|
current_temp = self.vera_device.temperature
|
2016-01-15 10:52:58 +00:00
|
|
|
vera_temp_units = (
|
|
|
|
self.vera_device.vera_controller.temperature_units)
|
2015-03-30 00:51:03 +11:00
|
|
|
|
|
|
|
if vera_temp_units == 'F':
|
|
|
|
self._temperature_units = TEMP_FAHRENHEIT
|
|
|
|
else:
|
|
|
|
self._temperature_units = TEMP_CELCIUS
|
|
|
|
|
|
|
|
if self.hass:
|
|
|
|
temp = self.hass.config.temperature(
|
|
|
|
current_temp,
|
|
|
|
self._temperature_units)
|
|
|
|
|
|
|
|
current_temp, self._temperature_units = temp
|
|
|
|
|
|
|
|
self.current_value = current_temp
|
2015-03-02 21:09:00 +11: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
|
2015-03-02 21:09:00 +11: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 21:09:00 +11:00
|
|
|
else:
|
|
|
|
self.current_value = 'Unknown'
|