core/homeassistant/components/sensor/vera.py

93 lines
3.2 KiB
Python
Raw Normal View History

2015-03-08 14:14:44 +00:00
"""
Support for Vera sensors.
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-02 10:09:00 +00:00
import logging
from datetime import timedelta
2016-02-19 05:27:50 +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
from homeassistant.components.sensor import ENTITY_ID_FORMAT
from homeassistant.util import convert
2016-03-15 09:17:09 +00:00
from homeassistant.components.vera import (
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
_LOGGER = logging.getLogger(__name__)
2015-03-02 10:09:00 +00:00
SCAN_INTERVAL = timedelta(seconds=5)
2015-03-08 14:58:11 +00:00
def setup_platform(hass, config, add_devices, discovery_info=None):
"""Set up the Vera controller devices."""
add_devices(
2016-03-15 09:17:09 +00:00
VeraSensor(device, VERA_CONTROLLER)
for device in VERA_DEVICES['sensor'])
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
self._temperature_units = None
self.last_changed_time = None
2016-03-15 09:17:09 +00:00
VeraDevice.__init__(self, vera_device, controller)
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
@property
def unit_of_measurement(self):
2016-03-08 15:46:34 +00:00
"""Return the unit of measurement of this entity, if any."""
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 '%'
elif self.vera_device.category == "Power meter":
return 'watts'
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)
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-02 10:09:00 +00:00
elif self.vera_device.category == "Light Sensor":
self.current_value = self.vera_device.light
elif self.vera_device.category == "Humidity Sensor":
self.current_value = self.vera_device.humidity
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
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":
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'