1
mirror of https://github.com/home-assistant/core synced 2024-09-15 17:29:45 +02:00
ha-core/homeassistant/components/vera/__init__.py

215 lines
6.1 KiB
Python
Raw Normal View History

"""Support for Vera devices."""
2016-03-15 10:17:09 +01:00
from collections import defaultdict
import logging
import pyvera as veraApi
2016-08-19 23:43:40 +02:00
from requests.exceptions import RequestException
import voluptuous as vol
from homeassistant.const import (
2019-07-31 21:25:30 +02:00
ATTR_ARMED,
ATTR_BATTERY_LEVEL,
ATTR_LAST_TRIP_TIME,
ATTR_TRIPPED,
CONF_EXCLUDE,
CONF_LIGHTS,
EVENT_HOMEASSISTANT_STOP,
2019-07-31 21:25:30 +02:00
)
from homeassistant.helpers import config_validation as cv, discovery
2016-03-15 10:17:09 +01:00
from homeassistant.helpers.entity import Entity
from homeassistant.util import convert, slugify
from homeassistant.util.dt import utc_from_timestamp
2016-03-15 10:17:09 +01:00
_LOGGER = logging.getLogger(__name__)
2019-07-31 21:25:30 +02:00
DOMAIN = "vera"
2016-03-15 10:17:09 +01:00
2019-07-31 21:25:30 +02:00
VERA_CONTROLLER = "vera_controller"
2016-03-15 10:17:09 +01:00
2019-07-31 21:25:30 +02:00
CONF_CONTROLLER = "vera_controller_url"
2016-03-15 10:17:09 +01:00
2019-07-31 21:25:30 +02:00
VERA_ID_FORMAT = "{}_{}"
ATTR_CURRENT_POWER_W = "current_power_w"
ATTR_CURRENT_ENERGY_KWH = "current_energy_kwh"
2016-03-15 10:17:09 +01:00
2019-07-31 21:25:30 +02:00
VERA_DEVICES = "vera_devices"
VERA_SCENES = "vera_scenes"
2016-03-15 10:17:09 +01:00
2016-08-19 23:43:40 +02:00
VERA_ID_LIST_SCHEMA = vol.Schema([int])
2019-07-31 21:25:30 +02:00
CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.Schema(
{
vol.Required(CONF_CONTROLLER): cv.url,
vol.Optional(CONF_EXCLUDE, default=[]): VERA_ID_LIST_SCHEMA,
vol.Optional(CONF_LIGHTS, default=[]): VERA_ID_LIST_SCHEMA,
}
)
},
extra=vol.ALLOW_EXTRA,
)
2016-08-19 23:43:40 +02:00
VERA_COMPONENTS = [
2019-07-31 21:25:30 +02:00
"binary_sensor",
"sensor",
"light",
"switch",
"lock",
"climate",
"cover",
"scene",
]
2016-03-15 10:17:09 +01:00
def setup(hass, base_config):
"""Set up for Vera devices."""
2016-03-15 10:17:09 +01:00
def stop_subscription(event):
"""Shutdown Vera subscriptions and subscription thread on exit."""
_LOGGER.info("Shutting down subscriptions")
hass.data[VERA_CONTROLLER].stop()
2016-03-15 10:17:09 +01:00
config = base_config.get(DOMAIN)
# Get Vera specific configuration.
base_url = config.get(CONF_CONTROLLER)
light_ids = config.get(CONF_LIGHTS)
exclude_ids = config.get(CONF_EXCLUDE)
# Initialize the Vera controller.
controller, _ = veraApi.init_controller(base_url)
hass.data[VERA_CONTROLLER] = controller
2016-03-15 10:17:09 +01:00
hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_subscription)
try:
all_devices = controller.get_devices()
all_scenes = controller.get_scenes()
2016-03-15 10:17:09 +01:00
except RequestException:
# There was a network related error connecting to the Vera controller.
2016-03-15 10:17:09 +01:00
_LOGGER.exception("Error communicating with Vera API")
return False
# Exclude devices unwanted by user.
2019-07-31 21:25:30 +02:00
devices = [device for device in all_devices if device.device_id not in exclude_ids]
2016-03-15 10:17:09 +01:00
vera_devices = defaultdict(list)
for device in devices:
device_type = map_vera_device(device, light_ids)
if device_type is None:
2016-03-15 10:17:09 +01:00
continue
vera_devices[device_type].append(device)
hass.data[VERA_DEVICES] = vera_devices
vera_scenes = []
for scene in all_scenes:
vera_scenes.append(scene)
hass.data[VERA_SCENES] = vera_scenes
2016-03-15 10:17:09 +01:00
for component in VERA_COMPONENTS:
2016-06-15 07:51:46 +02:00
discovery.load_platform(hass, component, DOMAIN, {}, base_config)
2016-03-15 10:17:09 +01:00
return True
def map_vera_device(vera_device, remap):
"""Map vera classes to Home Assistant types."""
2019-07-31 21:25:30 +02:00
if isinstance(vera_device, veraApi.VeraDimmer):
2019-07-31 21:25:30 +02:00
return "light"
if isinstance(vera_device, veraApi.VeraBinarySensor):
2019-07-31 21:25:30 +02:00
return "binary_sensor"
if isinstance(vera_device, veraApi.VeraSensor):
2019-07-31 21:25:30 +02:00
return "sensor"
if isinstance(vera_device, veraApi.VeraArmableDevice):
2019-07-31 21:25:30 +02:00
return "switch"
if isinstance(vera_device, veraApi.VeraLock):
2019-07-31 21:25:30 +02:00
return "lock"
if isinstance(vera_device, veraApi.VeraThermostat):
2019-07-31 21:25:30 +02:00
return "climate"
2016-09-15 20:47:03 +02:00
if isinstance(vera_device, veraApi.VeraCurtain):
2019-07-31 21:25:30 +02:00
return "cover"
if isinstance(vera_device, veraApi.VeraSceneController):
2019-07-31 21:25:30 +02:00
return "sensor"
if isinstance(vera_device, veraApi.VeraSwitch):
if vera_device.device_id in remap:
2019-07-31 21:25:30 +02:00
return "light"
return "switch"
return None
2016-03-15 10:17:09 +01:00
class VeraDevice(Entity):
"""Representation of a Vera device entity."""
2016-03-15 10:17:09 +01:00
def __init__(self, vera_device, controller):
"""Initialize the device."""
self.vera_device = vera_device
self.controller = controller
self._name = self.vera_device.name
# Append device id to prevent name clashes in HA.
self.vera_id = VERA_ID_FORMAT.format(
2019-07-31 21:25:30 +02:00
slugify(vera_device.name), vera_device.device_id
)
2016-03-15 10:17:09 +01:00
self.controller.register(vera_device, self._update_callback)
def _update_callback(self, _device):
"""Update the state."""
self.schedule_update_ha_state(True)
2016-03-15 10:17:09 +01:00
@property
def name(self):
"""Return the name of the device."""
return self._name
@property
def should_poll(self):
"""Get polling requirement from vera device."""
return self.vera_device.should_poll
@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
2019-07-31 21:25:30 +02:00
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
2019-07-31 21:25:30 +02:00
attr[ATTR_TRIPPED] = "True" if tripped else "False"
power = self.vera_device.power
if power:
attr[ATTR_CURRENT_POWER_W] = convert(power, float, 0.0)
energy = self.vera_device.energy
if energy:
attr[ATTR_CURRENT_ENERGY_KWH] = convert(energy, float, 0.0)
2019-07-31 21:25:30 +02:00
attr["Vera Device Id"] = self.vera_device.vera_device_id
return attr
@property
def unique_id(self) -> str:
"""Return a unique ID.
The Vera assigns a unique and immutable ID number to each device.
"""
return str(self.vera_device.vera_device_id)