diff --git a/homeassistant/components/binary_sensor/vera.py b/homeassistant/components/binary_sensor/vera.py index 27d604805dc..8673c0e4696 100644 --- a/homeassistant/components/binary_sensor/vera.py +++ b/homeassistant/components/binary_sensor/vera.py @@ -6,9 +6,6 @@ https://home-assistant.io/components/binary_sensor.vera/ """ 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 ( BinarySensorDevice) from homeassistant.components.vera import ( @@ -34,30 +31,6 @@ class VeraBinarySensor(VeraDevice, BinarySensorDevice): self._state = False 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 def is_on(self): """Return true if sensor is on.""" diff --git a/homeassistant/components/light/vera.py b/homeassistant/components/light/vera.py index aa39fb03536..142ccbc5b22 100644 --- a/homeassistant/components/light/vera.py +++ b/homeassistant/components/light/vera.py @@ -6,10 +6,8 @@ https://home-assistant.io/components/light.vera/ """ import logging -import homeassistant.util.dt as dt_util from homeassistant.components.light import ATTR_BRIGHTNESS, Light from homeassistant.const import ( - ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED, STATE_OFF, STATE_ON) from homeassistant.components.vera import ( VeraDevice, VERA_DEVICES, VERA_CONTROLLER) @@ -56,31 +54,6 @@ class VeraLight(VeraDevice, Light): self._state = STATE_OFF 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 def is_on(self): """Return true if device is on.""" diff --git a/homeassistant/components/lock/vera.py b/homeassistant/components/lock/vera.py index f10b8857499..6ac8fb2c315 100644 --- a/homeassistant/components/lock/vera.py +++ b/homeassistant/components/lock/vera.py @@ -8,7 +8,7 @@ import logging from homeassistant.components.lock import LockDevice from homeassistant.const import ( - ATTR_BATTERY_LEVEL, STATE_LOCKED, STATE_UNLOCKED) + STATE_LOCKED, STATE_UNLOCKED) from homeassistant.components.vera import ( VeraDevice, VERA_DEVICES, VERA_CONTROLLER) @@ -32,16 +32,6 @@ class VeraLock(VeraDevice, LockDevice): self._state = None 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): """Lock the device.""" self.vera_device.lock() diff --git a/homeassistant/components/sensor/vera.py b/homeassistant/components/sensor/vera.py index 6334a229747..537b7847b7e 100644 --- a/homeassistant/components/sensor/vera.py +++ b/homeassistant/components/sensor/vera.py @@ -6,9 +6,7 @@ https://home-assistant.io/components/sensor.vera/ """ import logging -import homeassistant.util.dt as dt_util from homeassistant.const import ( - ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED, TEMP_CELSIUS, TEMP_FAHRENHEIT) from homeassistant.helpers.entity import Entity from homeassistant.components.vera import ( @@ -50,30 +48,6 @@ class VeraSensor(VeraDevice, Entity): elif self.vera_device.category == "Humidity Sensor": 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): """Update the state.""" if self.vera_device.category == "Temperature Sensor": diff --git a/homeassistant/components/switch/vera.py b/homeassistant/components/switch/vera.py index d71d064a3e8..e88e45a5171 100644 --- a/homeassistant/components/switch/vera.py +++ b/homeassistant/components/switch/vera.py @@ -6,10 +6,9 @@ https://home-assistant.io/components/switch.vera/ """ import logging -import homeassistant.util.dt as dt_util +from homeassistant.util import convert from homeassistant.components.switch import SwitchDevice from homeassistant.const import ( - ATTR_ARMED, ATTR_BATTERY_LEVEL, ATTR_LAST_TRIP_TIME, ATTR_TRIPPED, STATE_OFF, STATE_ON) from homeassistant.components.vera import ( VeraDevice, VERA_DEVICES, VERA_CONTROLLER) @@ -34,32 +33,6 @@ class VeraSwitch(VeraDevice, SwitchDevice): self._state = False 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): """Turn device on.""" self.vera_device.switch_on() @@ -72,6 +45,13 @@ class VeraSwitch(VeraDevice, SwitchDevice): self._state = STATE_OFF 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 def is_on(self): """Return true if device is on.""" diff --git a/homeassistant/components/vera.py b/homeassistant/components/vera.py index ce7bf1556fa..514fe002568 100644 --- a/homeassistant/components/vera.py +++ b/homeassistant/components/vera.py @@ -9,34 +9,27 @@ from collections import defaultdict 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.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 -REQUIREMENTS = ['pyvera==0.2.13'] +REQUIREMENTS = ['pyvera==0.2.15'] _LOGGER = logging.getLogger(__name__) DOMAIN = 'vera' - VERA_CONTROLLER = None CONF_EXCLUDE = 'exclude' CONF_LIGHTS = 'lights' -DEVICE_CATEGORIES = { - '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 -} +ATTR_CURRENT_POWER_MWH = "current_power_mwh" VERA_DEVICES = defaultdict(list) @@ -66,8 +59,7 @@ def setup(hass, base_config): hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription) try: - all_devices = VERA_CONTROLLER.get_devices( - list(DEVICE_CATEGORIES.keys())) + all_devices = VERA_CONTROLLER.get_devices() except RequestException: # There was a network related error connecting to the vera controller. _LOGGER.exception("Error communicating with Vera API") @@ -86,11 +78,9 @@ def setup(hass, base_config): for device in all_devices: if device.device_id in exclude: continue - dev_type = DEVICE_CATEGORIES.get(device.category) + dev_type = map_vera_device(device, lights_ids) if dev_type is None: continue - if dev_type == 'switch' and device.device_id in lights_ids: - dev_type = 'light' VERA_DEVICES[dev_type].append(device) for component in 'binary_sensor', 'sensor', 'light', 'switch', 'lock': @@ -99,6 +89,29 @@ def setup(hass, base_config): 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): """Representation of a Vera devicetity.""" @@ -123,3 +136,33 @@ class VeraDevice(Entity): def should_poll(self): """No polling needed.""" 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 diff --git a/requirements_all.txt b/requirements_all.txt index 7b4ff9f1889..fa6b8909c6b 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -350,7 +350,7 @@ python-wink==0.7.10 pyuserinput==0.1.9 # homeassistant.components.vera -pyvera==0.2.13 +pyvera==0.2.15 # homeassistant.components.wemo pywemo==0.4.3