2019-02-13 20:21:14 +00:00
|
|
|
"""Support for Velbus devices."""
|
2019-07-29 07:21:26 +00:00
|
|
|
import asyncio
|
2017-07-26 12:03:29 +00:00
|
|
|
import logging
|
2019-07-29 07:21:26 +00:00
|
|
|
import velbus
|
2017-07-26 12:03:29 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
import homeassistant.helpers.config_validation as cv
|
2019-07-29 07:21:26 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
|
|
|
from homeassistant.const import CONF_PORT, CONF_NAME
|
2019-07-30 10:56:40 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2018-08-05 08:47:17 +00:00
|
|
|
from homeassistant.helpers.entity import Entity
|
2019-07-29 07:21:26 +00:00
|
|
|
from homeassistant.helpers.typing import HomeAssistantType
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
from .const import DOMAIN
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
VELBUS_MESSAGE = 'velbus.message'
|
|
|
|
|
|
|
|
CONFIG_SCHEMA = vol.Schema({
|
|
|
|
DOMAIN: vol.Schema({
|
|
|
|
vol.Required(CONF_PORT): cv.string,
|
|
|
|
})
|
|
|
|
}, extra=vol.ALLOW_EXTRA)
|
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
COMPONENT_TYPES = ['switch', 'sensor', 'binary_sensor', 'cover', 'climate']
|
|
|
|
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2018-08-05 08:47:17 +00:00
|
|
|
async def async_setup(hass, config):
|
2017-07-26 12:03:29 +00:00
|
|
|
"""Set up the Velbus platform."""
|
2019-07-29 07:21:26 +00:00
|
|
|
# Import from the configuration file if needed
|
|
|
|
if DOMAIN not in config:
|
|
|
|
return True
|
|
|
|
|
2017-07-26 12:03:29 +00:00
|
|
|
port = config[DOMAIN].get(CONF_PORT)
|
2019-07-29 07:21:26 +00:00
|
|
|
data = {}
|
|
|
|
|
|
|
|
if port:
|
|
|
|
data = {
|
|
|
|
CONF_PORT: port,
|
|
|
|
CONF_NAME: 'Velbus import'
|
|
|
|
}
|
2018-08-05 08:47:17 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
|
|
|
context={'source': SOURCE_IMPORT},
|
|
|
|
data=data
|
|
|
|
))
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
|
|
"""Establish connection with velbus."""
|
|
|
|
hass.data.setdefault(DOMAIN, {})
|
2018-08-05 08:47:17 +00:00
|
|
|
|
|
|
|
def callback():
|
|
|
|
modules = controller.get_modules()
|
|
|
|
discovery_info = {
|
2019-07-29 07:21:26 +00:00
|
|
|
'cntrl': controller
|
2018-08-05 08:47:17 +00:00
|
|
|
}
|
2019-07-29 07:21:26 +00:00
|
|
|
for category in COMPONENT_TYPES:
|
|
|
|
discovery_info[category] = []
|
|
|
|
|
2018-08-05 08:47:17 +00:00
|
|
|
for module in modules:
|
|
|
|
for channel in range(1, module.number_of_channels() + 1):
|
2019-07-29 07:21:26 +00:00
|
|
|
for category in COMPONENT_TYPES:
|
2018-08-05 08:47:17 +00:00
|
|
|
if category in module.get_categories(channel):
|
|
|
|
discovery_info[category].append((
|
|
|
|
module.get_module_address(),
|
|
|
|
channel
|
|
|
|
))
|
2019-07-29 07:21:26 +00:00
|
|
|
|
|
|
|
hass.data[DOMAIN][entry.entry_id] = discovery_info
|
|
|
|
|
|
|
|
for category in COMPONENT_TYPES:
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.async_forward_entry_setup(
|
|
|
|
entry, category))
|
|
|
|
|
2019-07-30 10:56:40 +00:00
|
|
|
try:
|
|
|
|
controller = velbus.Controller(entry.data[CONF_PORT])
|
|
|
|
controller.scan(callback)
|
|
|
|
except velbus.util.VelbusException as err:
|
|
|
|
_LOGGER.error('An error occurred: %s', err)
|
|
|
|
raise ConfigEntryNotReady
|
2018-08-05 08:47:17 +00:00
|
|
|
|
2019-03-03 12:37:36 +00:00
|
|
|
def syn_clock(self, service=None):
|
2019-07-30 10:56:40 +00:00
|
|
|
try:
|
|
|
|
controller.sync_clock()
|
|
|
|
except velbus.util.VelbusException as err:
|
|
|
|
_LOGGER.error('An error occurred: %s', err)
|
2019-03-03 12:37:36 +00:00
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN, 'sync_clock', syn_clock,
|
|
|
|
schema=vol.Schema({}))
|
2017-07-26 12:03:29 +00:00
|
|
|
|
|
|
|
return True
|
2018-08-05 08:47:17 +00:00
|
|
|
|
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry):
|
|
|
|
"""Remove the velbus connection."""
|
|
|
|
await asyncio.wait([
|
|
|
|
hass.config_entries.async_forward_entry_unload(entry, component)
|
|
|
|
for component in COMPONENT_TYPES
|
|
|
|
])
|
|
|
|
hass.data[DOMAIN][entry.entry_id]['cntrl'].stop()
|
|
|
|
hass.data[DOMAIN].pop(entry.entry_id)
|
|
|
|
if not hass.data[DOMAIN]:
|
|
|
|
hass.data.pop(DOMAIN)
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2018-08-05 08:47:17 +00:00
|
|
|
class VelbusEntity(Entity):
|
|
|
|
"""Representation of a Velbus entity."""
|
|
|
|
|
|
|
|
def __init__(self, module, channel):
|
|
|
|
"""Initialize a Velbus entity."""
|
|
|
|
self._module = module
|
|
|
|
self._channel = channel
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self):
|
|
|
|
"""Get unique ID."""
|
|
|
|
serial = 0
|
|
|
|
if self._module.serial == 0:
|
|
|
|
serial = self._module.get_module_address()
|
|
|
|
else:
|
|
|
|
serial = self._module.serial
|
|
|
|
return "{}-{}".format(serial, self._channel)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self):
|
|
|
|
"""Return the display name of this entity."""
|
|
|
|
return self._module.get_name(self._channel)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self):
|
|
|
|
"""Disable polling."""
|
|
|
|
return False
|
|
|
|
|
|
|
|
async def async_added_to_hass(self):
|
|
|
|
"""Add listener for state changes."""
|
|
|
|
self._module.on_status_update(self._channel, self._on_update)
|
|
|
|
|
|
|
|
def _on_update(self, state):
|
|
|
|
self.schedule_update_ha_state()
|
2019-07-29 07:21:26 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def device_info(self):
|
|
|
|
"""Return the device info."""
|
|
|
|
return {
|
|
|
|
'identifiers': {
|
|
|
|
(DOMAIN, self._module.get_module_address(),
|
|
|
|
self._module.serial)
|
|
|
|
},
|
|
|
|
'name': "{} {}".format(
|
|
|
|
self._module.get_module_address(),
|
|
|
|
self._module.get_module_name()),
|
|
|
|
'manufacturer': 'Velleman',
|
|
|
|
'model': self._module.get_module_name(),
|
|
|
|
'sw_version': "{}.{}-{}".format(
|
|
|
|
self._module.memory_map_version, self._module.build_year,
|
|
|
|
self._module.build_week)
|
|
|
|
}
|