Tidy / Refactor Vera (#2569)

* Add power attribute to switch.

* Move device_state_attributes into base class.

* Fix imports following refactor.

* Bump pyvera version - should add contributed support for older (UI5) version dimmers and locks.

* Refactor device lookup to be based on vera classes, push category back into library.

* Add generic power attribute, fix inherited class order bug.

* Tidy.
This commit is contained in:
Greg Dowling 2016-07-20 04:13:33 +02:00 committed by Paulus Schoutsen
parent 7c431911d1
commit e34bfb7381
7 changed files with 73 additions and 140 deletions

View file

@ -6,9 +6,6 @@ https://home-assistant.io/components/binary_sensor.vera/
""" """
import logging import logging
import homeassistant.util.dt as dt_util
from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED)
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
BinarySensorDevice) BinarySensorDevice)
from homeassistant.components.vera import ( from homeassistant.components.vera import (
@ -34,30 +31,6 @@ class VeraBinarySensor(VeraDevice, BinarySensorDevice):
self._state = False self._state = False
VeraDevice.__init__(self, vera_device, controller) VeraDevice.__init__(self, vera_device, controller)
@property
def device_state_attributes(self):
"""Return the state attributes."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
if self.vera_device.is_armable:
armed = self.vera_device.is_armed
attr[ATTR_ARMED] = 'True' if armed else 'False'
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))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
@property @property
def is_on(self): def is_on(self):
"""Return true if sensor is on.""" """Return true if sensor is on."""

View file

@ -6,10 +6,8 @@ https://home-assistant.io/components/light.vera/
""" """
import logging import logging
import homeassistant.util.dt as dt_util
from homeassistant.components.light import ATTR_BRIGHTNESS, Light from homeassistant.components.light import ATTR_BRIGHTNESS, Light
from homeassistant.const import ( from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
STATE_OFF, STATE_ON) STATE_OFF, STATE_ON)
from homeassistant.components.vera import ( from homeassistant.components.vera import (
VeraDevice, VERA_DEVICES, VERA_CONTROLLER) VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
@ -56,31 +54,6 @@ class VeraLight(VeraDevice, Light):
self._state = STATE_OFF self._state = STATE_OFF
self.update_ha_state() self.update_ha_state()
@property
def device_state_attributes(self):
"""Return the state attributes."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
if self.vera_device.is_armable:
armed = self.vera_device.is_armed
attr[ATTR_ARMED] = 'True' if armed else 'False'
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))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
@property @property
def is_on(self): def is_on(self):
"""Return true if device is on.""" """Return true if device is on."""

View file

@ -8,7 +8,7 @@ import logging
from homeassistant.components.lock import LockDevice from homeassistant.components.lock import LockDevice
from homeassistant.const import ( from homeassistant.const import (
ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED) STATE_LOCKED, STATE_UNLOCKED)
from homeassistant.components.vera import ( from homeassistant.components.vera import (
VeraDevice, VERA_DEVICES, VERA_CONTROLLER) VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
@ -32,16 +32,6 @@ class VeraLock(VeraDevice, LockDevice):
self._state = None self._state = None
VeraDevice.__init__(self, vera_device, controller) VeraDevice.__init__(self, vera_device, controller)
@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
def lock(self, **kwargs): def lock(self, **kwargs):
"""Lock the device.""" """Lock the device."""
self.vera_device.lock() self.vera_device.lock()

View file

@ -6,9 +6,7 @@ https://home-assistant.io/components/sensor.vera/
""" """
import logging import logging
import homeassistant.util.dt as dt_util
from homeassistant.const import ( from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
TEMP_CELSIUS, TEMP_FAHRENHEIT) TEMP_CELSIUS, TEMP_FAHRENHEIT)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
from homeassistant.components.vera import ( from homeassistant.components.vera import (
@ -50,30 +48,6 @@ class VeraSensor(VeraDevice, Entity):
elif self.vera_device.category == "Humidity Sensor": elif self.vera_device.category == "Humidity Sensor":
return '%' return '%'
@property
def device_state_attributes(self):
"""Return the state attributes."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
if self.vera_device.is_armable:
armed = self.vera_device.is_armed
attr[ATTR_ARMED] = 'True' if armed else 'False'
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))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
def update(self): def update(self):
"""Update the state.""" """Update the state."""
if self.vera_device.category == "Temperature Sensor": if self.vera_device.category == "Temperature Sensor":

View file

@ -6,10 +6,9 @@ https://home-assistant.io/components/switch.vera/
""" """
import logging import logging
import homeassistant.util.dt as dt_util from homeassistant.util import convert
from homeassistant.components.switch import SwitchDevice from homeassistant.components.switch import SwitchDevice
from homeassistant.const import ( from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
STATE_OFF, STATE_ON) STATE_OFF, STATE_ON)
from homeassistant.components.vera import ( from homeassistant.components.vera import (
VeraDevice, VERA_DEVICES, VERA_CONTROLLER) VeraDevice, VERA_DEVICES, VERA_CONTROLLER)
@ -34,32 +33,6 @@ class VeraSwitch(VeraDevice, SwitchDevice):
self._state = False self._state = False
VeraDevice.__init__(self, vera_device, controller) VeraDevice.__init__(self, vera_device, controller)
@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
if self.vera_device.is_armable:
armed = self.vera_device.is_armed
attr[ATTR_ARMED] = 'True' if armed else 'False'
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))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr
def turn_on(self, **kwargs): def turn_on(self, **kwargs):
"""Turn device on.""" """Turn device on."""
self.vera_device.switch_on() self.vera_device.switch_on()
@ -72,6 +45,13 @@ class VeraSwitch(VeraDevice, SwitchDevice):
self._state = STATE_OFF self._state = STATE_OFF
self.update_ha_state() self.update_ha_state()
@property
def current_power_mwh(self):
"""Current power usage in mWh."""
power = self.vera_device.power
if power:
return convert(power, float, 0.0) * 1000
@property @property
def is_on(self): def is_on(self):
"""Return true if device is on.""" """Return true if device is on."""

View file

@ -9,34 +9,27 @@ from collections import defaultdict
from requests.exceptions import RequestException from requests.exceptions import RequestException
from homeassistant.util.dt import utc_from_timestamp
from homeassistant.util import convert
from homeassistant.helpers import discovery from homeassistant.helpers import discovery
from homeassistant.const import EVENT_HOMEASSISTANT_STOP from homeassistant.const import (
ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED,
EVENT_HOMEASSISTANT_STOP)
from homeassistant.helpers.entity import Entity from homeassistant.helpers.entity import Entity
REQUIREMENTS = ['pyvera==0.2.13'] REQUIREMENTS = ['pyvera==0.2.15']
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = 'vera' DOMAIN = 'vera'
VERA_CONTROLLER = None VERA_CONTROLLER = None
CONF_EXCLUDE = 'exclude' CONF_EXCLUDE = 'exclude'
CONF_LIGHTS = 'lights' CONF_LIGHTS = 'lights'
DEVICE_CATEGORIES = { ATTR_CURRENT_POWER_MWH = "current_power_mwh"
'Sensor': 'binary_sensor',
'Temperature Sensor': 'sensor',
'Light Sensor': 'sensor',
'Humidity Sensor': 'sensor',
'Dimmable Switch': 'light',
'Switch': 'switch',
'Armable Sensor': 'switch',
'On/Off Switch': 'switch',
'Doorlock': 'lock',
# 'Window Covering': NOT SUPPORTED YET
}
VERA_DEVICES = defaultdict(list) VERA_DEVICES = defaultdict(list)
@ -66,8 +59,7 @@ def setup(hass, base_config):
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
try: try:
all_devices = VERA_CONTROLLER.get_devices( all_devices = VERA_CONTROLLER.get_devices()
list(DEVICE_CATEGORIES.keys()))
except RequestException: except RequestException:
# There was a network related error connecting to the vera controller. # There was a network related error connecting to the vera controller.
_LOGGER.exception("Error communicating with Vera API") _LOGGER.exception("Error communicating with Vera API")
@ -86,11 +78,9 @@ def setup(hass, base_config):
for device in all_devices: for device in all_devices:
if device.device_id in exclude: if device.device_id in exclude:
continue continue
dev_type = DEVICE_CATEGORIES.get(device.category) dev_type = map_vera_device(device, lights_ids)
if dev_type is None: if dev_type is None:
continue continue
if dev_type == 'switch' and device.device_id in lights_ids:
dev_type = 'light'
VERA_DEVICES[dev_type].append(device) VERA_DEVICES[dev_type].append(device)
for component in 'binary_sensor', 'sensor', 'light', 'switch', 'lock': for component in 'binary_sensor', 'sensor', 'light', 'switch', 'lock':
@ -99,6 +89,29 @@ def setup(hass, base_config):
return True return True
def map_vera_device(vera_device, remap):
"""Map vera classes to HA types."""
# pylint: disable=too-many-return-statements
import pyvera as veraApi
if isinstance(vera_device, veraApi.VeraDimmer):
return 'light'
if isinstance(vera_device, veraApi.VeraBinarySensor):
return 'binary_sensor'
if isinstance(vera_device, veraApi.VeraSensor):
return 'sensor'
if isinstance(vera_device, veraApi.VeraArmableDevice):
return 'switch'
if isinstance(vera_device, veraApi.VeraLock):
return 'lock'
if isinstance(vera_device, veraApi.VeraSwitch):
if vera_device.device_id in remap:
return 'light'
else:
return 'switch'
# VeraCurtain: NOT SUPPORTED YET
return None
class VeraDevice(Entity): class VeraDevice(Entity):
"""Representation of a Vera devicetity.""" """Representation of a Vera devicetity."""
@ -123,3 +136,33 @@ class VeraDevice(Entity):
def should_poll(self): def should_poll(self):
"""No polling needed.""" """No polling needed."""
return False return False
@property
def device_state_attributes(self):
"""Return the state attributes of the device."""
attr = {}
if self.vera_device.has_battery:
attr[ATTR_BATTERY_LEVEL] = self.vera_device.battery_level + '%'
if self.vera_device.is_armable:
armed = self.vera_device.is_armed
attr[ATTR_ARMED] = 'True' if armed else 'False'
if self.vera_device.is_trippable:
last_tripped = self.vera_device.last_trip
if last_tripped is not None:
utc_time = utc_from_timestamp(int(last_tripped))
attr[ATTR_LAST_TRIP_TIME] = utc_time.isoformat()
else:
attr[ATTR_LAST_TRIP_TIME] = None
tripped = self.vera_device.is_tripped
attr[ATTR_TRIPPED] = 'True' if tripped else 'False'
power = self.vera_device.power
if power:
attr[ATTR_CURRENT_POWER_MWH] = convert(power, float, 0.0) * 1000
attr['Vera Device Id'] = self.vera_device.vera_device_id
return attr

View file

@ -350,7 +350,7 @@ python-wink==0.7.10
pyuserinput==0.1.9 pyuserinput==0.1.9
# homeassistant.components.vera # homeassistant.components.vera
pyvera==0.2.13 pyvera==0.2.15
# homeassistant.components.wemo # homeassistant.components.wemo
pywemo==0.4.3 pywemo==0.4.3