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-12-09 08:36:42 +00:00
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
import velbus
|
2017-07-26 12:03:29 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
2019-07-29 07:21:26 +00:00
|
|
|
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
2020-03-13 07:09:30 +00:00
|
|
|
from homeassistant.const import CONF_ADDRESS, CONF_NAME, CONF_PORT
|
2019-07-30 10:56:40 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2019-12-09 08:36:42 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
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
|
|
|
|
2020-03-13 07:09:30 +00:00
|
|
|
from .const import CONF_MEMO_TEXT, DOMAIN, SERVICE_SET_MEMO_TEXT
|
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
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
VELBUS_MESSAGE = "velbus.message"
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
CONFIG_SCHEMA = vol.Schema(
|
|
|
|
{DOMAIN: vol.Schema({vol.Required(CONF_PORT): cv.string})}, extra=vol.ALLOW_EXTRA
|
|
|
|
)
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2019-12-31 14:46:02 +00:00
|
|
|
COMPONENT_TYPES = ["switch", "sensor", "binary_sensor", "cover", "climate", "light"]
|
2019-07-29 07:21:26 +00:00
|
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
data = {CONF_PORT: port, CONF_NAME: "Velbus import"}
|
2019-07-29 07:21:26 +00:00
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
2019-07-31 19:25:30 +00:00
|
|
|
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()
|
2019-07-31 19:25:30 +00:00
|
|
|
discovery_info = {"cntrl": controller}
|
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):
|
2019-07-31 19:25:30 +00:00
|
|
|
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:
|
2020-01-03 23:01:37 +00:00
|
|
|
hass.add_job(hass.config_entries.async_forward_entry_setup(entry, category))
|
2019-07-29 07:21:26 +00:00
|
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("An error occurred: %s", err)
|
2019-07-30 10:56:40 +00:00
|
|
|
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:
|
2019-07-31 19:25:30 +00:00
|
|
|
_LOGGER.error("An error occurred: %s", err)
|
2019-03-03 12:37:36 +00:00
|
|
|
|
2019-07-31 19:25:30 +00:00
|
|
|
hass.services.async_register(DOMAIN, "sync_clock", syn_clock, schema=vol.Schema({}))
|
2017-07-26 12:03:29 +00:00
|
|
|
|
2020-03-13 07:09:30 +00:00
|
|
|
def set_memo_text(service):
|
|
|
|
"""Handle Memo Text service call."""
|
|
|
|
module_address = service.data[CONF_ADDRESS]
|
|
|
|
memo_text = service.data[CONF_MEMO_TEXT]
|
|
|
|
memo_text.hass = hass
|
|
|
|
try:
|
|
|
|
controller.get_module(module_address).set_memo_text(
|
|
|
|
memo_text.async_render()
|
|
|
|
)
|
|
|
|
except velbus.util.VelbusException as err:
|
|
|
|
_LOGGER.error("An error occurred while setting memo text: %s", err)
|
|
|
|
|
|
|
|
hass.services.async_register(
|
|
|
|
DOMAIN,
|
|
|
|
SERVICE_SET_MEMO_TEXT,
|
|
|
|
set_memo_text,
|
|
|
|
vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Required(CONF_ADDRESS): vol.All(
|
|
|
|
vol.Coerce(int), vol.Range(min=0, max=255)
|
|
|
|
),
|
|
|
|
vol.Optional(CONF_MEMO_TEXT, default=""): cv.template,
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
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."""
|
2019-07-31 19:25:30 +00:00
|
|
|
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()
|
2019-07-29 07:21:26 +00:00
|
|
|
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
|
2019-09-03 19:12:51 +00:00
|
|
|
return f"{serial}-{self._channel}"
|
2018-08-05 08:47:17 +00:00
|
|
|
|
|
|
|
@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 {
|
2019-07-31 19:25:30 +00:00
|
|
|
"identifiers": {
|
|
|
|
(DOMAIN, self._module.get_module_address(), self._module.serial)
|
2019-07-29 07:21:26 +00:00
|
|
|
},
|
2020-02-10 20:22:09 +00:00
|
|
|
"name": "{} ({})".format(
|
|
|
|
self._module.get_module_name(), self._module.get_module_address()
|
2019-07-31 19:25:30 +00:00
|
|
|
),
|
|
|
|
"manufacturer": "Velleman",
|
2020-02-10 20:22:09 +00:00
|
|
|
"model": self._module.get_module_type_name(),
|
2019-07-31 19:25:30 +00:00
|
|
|
"sw_version": "{}.{}-{}".format(
|
|
|
|
self._module.memory_map_version,
|
|
|
|
self._module.build_year,
|
|
|
|
self._module.build_week,
|
|
|
|
),
|
2019-07-29 07:21:26 +00:00
|
|
|
}
|