core/homeassistant/components/switch/vera.py

83 lines
2.5 KiB
Python
Raw Normal View History

2015-03-08 14:14:44 +00:00
"""
Support for Vera switches.
2015-10-20 20:08:20 +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/switch.vera/
"""
2015-03-02 10:09:00 +00:00
import logging
2016-02-19 05:27:50 +00:00
import homeassistant.util.dt as dt_util
from homeassistant.components.switch import SwitchDevice
from homeassistant.const import (
2016-02-19 05:27:50 +00:00
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
2016-03-15 09:17:09 +00:00
STATE_OFF, STATE_ON)
from homeassistant.components.vera import (
VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
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
2015-03-08 14:58:11 +00:00
2016-03-15 09:17:09 +00:00
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
2016-03-08 12:35:39 +00:00
"""Find and return Vera switches."""
2016-03-15 09:17:09 +00:00
add_devices_callback(
VeraSwitch(device, VERA_CONTROLLER) for
device in VERA_DEVICES['switch'])
2015-03-02 10:09:00 +00:00
2015-03-08 14:58:11 +00:00
2016-03-15 09:17:09 +00:00
class VeraSwitch(VeraDevice, SwitchDevice):
2016-03-08 12:35:39 +00:00
"""Representation of a Vera Switch."""
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 12:35:39 +00:00
"""Initialize the Vera device."""
2016-03-15 09:17:09 +00:00
self._state = False
VeraDevice.__init__(self, vera_device, controller)
2015-03-02 10:09:00 +00:00
@property
def device_state_attributes(self):
2016-03-08 12:35:39 +00:00
"""Return the state attributes of the device."""
attr = {}
2015-03-02 10:09:00 +00:00
if self.vera_device.has_battery:
2015-03-08 20:11:35 +00:00
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
2015-03-02 10:09:00 +00:00
if self.vera_device.is_armable:
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 10:09:00 +00:00
if self.vera_device.is_trippable:
last_tripped = self.vera_device.last_trip
if last_tripped is not None:
utc_time = dt_util.utc_from_timestamp(int(last_tripped))
2016-04-16 07:55:35 +00:00
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
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 10:09:00 +00:00
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
def turn_on(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn device on."""
2015-03-02 10:09:00 +00:00
self.vera_device.switch_on()
self._state = STATE_ON
self.update_ha_state()
2015-03-02 10:09:00 +00:00
def turn_off(self, **kwargs):
2016-03-08 12:35:39 +00:00
"""Turn device off."""
2015-03-02 10:09:00 +00:00
self.vera_device.switch_off()
self._state = STATE_OFF
self.update_ha_state()
2015-03-02 10:09:00 +00:00
@property
def is_on(self):
2016-03-08 12:35:39 +00:00
"""Return true if device is on."""
2016-03-15 09:17:09 +00:00
return self._state
def update(self):
2016-03-08 12:35:39 +00:00
"""Called by the vera device callback to update state."""
2016-03-15 09:17:09 +00:00
self._state = self.vera_device.is_switched_on()