async def async_setup(hass, config): """Set up the Velbus platform.""" import velbus port = config[DOMAIN].get(CONF_PORT) controller = velbus.Controller(port) hass.data[DOMAIN] = controller def stop_velbus(event): """Disconnect from serial port.""" _LOGGER.debug("Shutting down ") controller.stop() hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, stop_velbus) def callback(): modules = controller.get_modules() discovery_info = {'switch': [], 'binary_sensor': [], 'sensor': []} for module in modules: for channel in range(1, module.number_of_channels() + 1): for category in discovery_info: if category in module.get_categories(channel): discovery_info[category].append( (module.get_module_address(), channel)) load_platform(hass, 'switch', DOMAIN, discovery_info['switch'], config) load_platform(hass, 'binary_sensor', DOMAIN, discovery_info['binary_sensor'], config) load_platform(hass, 'sensor', DOMAIN, discovery_info['sensor'], config) controller.scan(callback) return True
def _test_connection(self, prt): """Try to connect to the velbus with the port specified.""" try: controller = velbus.Controller(prt) except Exception: # pylint: disable=broad-except self._errors[CONF_PORT] = "connection_failed" return False controller.stop() return True
def setup(hass, config): """Set up the Velbus platform.""" import velbus port = config[DOMAIN].get(CONF_PORT) connection = velbus.VelbusUSBConnection(port) controller = velbus.Controller(connection) hass.data[DOMAIN] = controller def stop_velbus(event): """Disconnect from serial port.""" _LOGGER.debug("Shutting down ") connection.stop() hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, stop_velbus) return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Establish connection with velbus.""" hass.data.setdefault(DOMAIN, {}) def callback(): modules = controller.get_modules() discovery_info = {"cntrl": controller} for category in COMPONENT_TYPES: discovery_info[category] = [] for module in modules: for channel in range(1, module.number_of_channels() + 1): for category in COMPONENT_TYPES: if category in module.get_categories(channel): discovery_info[category].append( (module.get_module_address(), channel)) 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)) 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 def syn_clock(self, service=None): try: controller.sync_clock() except velbus.util.VelbusException as err: _LOGGER.error("An error occurred: %s", err) hass.services.async_register(DOMAIN, "sync_clock", syn_clock, schema=vol.Schema({})) return True
async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry): """Establish connection with velbus.""" hass.data.setdefault(DOMAIN, {}) def callback(): modules = controller.get_modules() discovery_info = {"cntrl": controller} for platform in PLATFORMS: discovery_info[platform] = [] for module in modules: for channel in range(1, module.number_of_channels() + 1): for platform in PLATFORMS: if platform in module.get_categories(channel): discovery_info[platform].append( (module.get_module_address(), channel)) hass.data[DOMAIN][entry.entry_id] = discovery_info for platform in PLATFORMS: hass.add_job( hass.config_entries.async_forward_entry_setup(entry, platform)) 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 from err def syn_clock(self, service=None): try: controller.sync_clock() except velbus.util.VelbusException as err: _LOGGER.error("An error occurred: %s", err) hass.services.async_register(DOMAIN, "sync_clock", syn_clock, schema=vol.Schema({})) 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, }), ) return True
#!/usr/bin/python3 """ Example code to scan Velbus and return list of installed modules. """ import time import logging import sys import velbus def scan_finished(): """ Callback for finished scan """ logging.info(controller.get_modules("switch")) logging.info(controller.get_modules("binary_sensor")) logging.basicConfig(stream=sys.stdout, level=logging.INFO) # pylint: disable-msg=C0103 port = "/dev/ttyACM0" logging.info("Configuring controller") controller = velbus.Controller(port) logging.info("Starting scan") controller.scan(scan_finished) logging.info("Starting sleep") time.sleep(30) logging.info("Exiting ...") controller.stop()