async def async_setup_entry(hass, config_entry): """Set up a bridge from a config entry.""" host = config_entry.data[CONF_HOST] keyfile = config_entry.data[CONF_KEYFILE] certfile = config_entry.data[CONF_CERTFILE] ca_certs = config_entry.data[CONF_CA_CERTS] bridge = Smartbridge.create_tls(hostname=host, keyfile=keyfile, certfile=certfile, ca_certs=ca_certs) await bridge.connect() if not bridge.is_connected(): _LOGGER.error("Unable to connect to Lutron Caseta bridge at %s", host) return False _LOGGER.debug("Connected to Lutron Caseta bridge at %s", host) # Store this bridge (keyed by entry_id) so it can be retrieved by the # components we're setting up. hass.data[DOMAIN][config_entry.entry_id] = bridge for component in LUTRON_CASETA_COMPONENTS: hass.async_create_task( hass.config_entries.async_forward_entry_setup( config_entry, component)) return True
def async_setup(hass, base_config): """Set up the Lutron component.""" from pylutron_caseta.smartbridge import Smartbridge config = base_config.get(DOMAIN) keyfile = hass.config.path(config[CONF_KEYFILE]) certfile = hass.config.path(config[CONF_CERTFILE]) ca_certs = hass.config.path(config[CONF_CA_CERTS]) bridge = Smartbridge.create_tls(hostname=config[CONF_HOST], keyfile=keyfile, certfile=certfile, ca_certs=ca_certs) hass.data[LUTRON_CASETA_SMARTBRIDGE] = bridge yield from bridge.connect() if not hass.data[LUTRON_CASETA_SMARTBRIDGE].is_connected(): _LOGGER.error("Unable to connect to Lutron smartbridge at %s", config[CONF_HOST]) return False _LOGGER.info("Connected to Lutron smartbridge at %s", config[CONF_HOST]) for component in LUTRON_CASETA_COMPONENTS: hass.async_add_job( discovery.async_load_platform(hass, component, DOMAIN, {}, config)) return True
async def async_validate_connectable_bridge_config(self): """Check if we can connect to the bridge with the current config.""" try: bridge = Smartbridge.create_tls( hostname=self.data[CONF_HOST], keyfile=self.data[CONF_KEYFILE], certfile=self.data[CONF_CERTFILE], ca_certs=self.data[CONF_CA_CERTS], ) await bridge.connect() if not bridge.is_connected(): return False await bridge.close() return True except (KeyError, ValueError): _LOGGER.error( "Error while checking connectivity to bridge %s", self.data[CONF_HOST], ) return False except Exception: # pylint: disable=broad-except _LOGGER.exception( "Unknown exception while checking connectivity to bridge %s", self.data[CONF_HOST], ) return False
async def async_validate_connectable_bridge_config(self): """Check if we can connect to the bridge with the current config.""" bridge = None try: bridge = Smartbridge.create_tls( hostname=self.data[CONF_HOST], keyfile=self.hass.config.path(self.data[CONF_KEYFILE]), certfile=self.hass.config.path(self.data[CONF_CERTFILE]), ca_certs=self.hass.config.path(self.data[CONF_CA_CERTS]), ) except ssl.SSLError: _LOGGER.error( "Invalid certificate used to connect to bridge at %s", self.data[CONF_HOST], ) return False connected_ok = False try: async with async_timeout.timeout(BRIDGE_TIMEOUT): await bridge.connect() connected_ok = bridge.is_connected() except asyncio.TimeoutError: _LOGGER.error( "Timeout while trying to connect to bridge at %s", self.data[CONF_HOST], ) await bridge.close() return connected_ok
def async_setup(hass, base_config): """Set up the Lutron component.""" from pylutron_caseta.smartbridge import Smartbridge config = base_config.get(DOMAIN) keyfile = hass.config.path(config[CONF_KEYFILE]) certfile = hass.config.path(config[CONF_CERTFILE]) ca_certs = hass.config.path(config[CONF_CA_CERTS]) bridge = Smartbridge.create_tls(hostname=config[CONF_HOST], keyfile=keyfile, certfile=certfile, ca_certs=ca_certs) hass.data[LUTRON_CASETA_SMARTBRIDGE] = bridge yield from bridge.connect() if not hass.data[LUTRON_CASETA_SMARTBRIDGE].is_connected(): _LOGGER.error("Unable to connect to Lutron smartbridge at %s", config[CONF_HOST]) return False _LOGGER.info("Connected to Lutron smartbridge at %s", config[CONF_HOST]) for component in LUTRON_CASETA_COMPONENTS: hass.async_add_job(discovery.async_load_platform(hass, component, DOMAIN, {}, config)) return True
async def async_get_lutron_id(self) -> str | None: """Check if we can connect to the bridge with the current config.""" try: bridge = Smartbridge.create_tls( hostname=self.data[CONF_HOST], keyfile=self.hass.config.path(self.data[CONF_KEYFILE]), certfile=self.hass.config.path(self.data[CONF_CERTFILE]), ca_certs=self.hass.config.path(self.data[CONF_CA_CERTS]), ) except ssl.SSLError: _LOGGER.error( "Invalid certificate used to connect to bridge at %s", self.data[CONF_HOST], ) return None try: async with async_timeout.timeout(BRIDGE_TIMEOUT): await bridge.connect() except asyncio.TimeoutError: _LOGGER.error( "Timeout while trying to connect to bridge at %s", self.data[CONF_HOST], ) else: if not bridge.is_connected(): return None devices = bridge.get_devices() bridge_device = devices[BRIDGE_DEVICE_ID] return hex(bridge_device["serial"])[2:].zfill(8) finally: await bridge.close() return None
async def async_setup_entry(hass: HomeAssistant, config_entry: config_entries.ConfigEntry) -> bool: """Set up a bridge from a config entry.""" entry_id = config_entry.entry_id host = config_entry.data[CONF_HOST] keyfile = hass.config.path(config_entry.data[CONF_KEYFILE]) certfile = hass.config.path(config_entry.data[CONF_CERTFILE]) ca_certs = hass.config.path(config_entry.data[CONF_CA_CERTS]) bridge = None await _async_migrate_unique_ids(hass, config_entry) try: bridge = Smartbridge.create_tls(hostname=host, keyfile=keyfile, certfile=certfile, ca_certs=ca_certs) except ssl.SSLError: _LOGGER.error("Invalid certificate used to connect to bridge at %s", host) return False timed_out = True with contextlib.suppress(asyncio.TimeoutError): async with async_timeout.timeout(BRIDGE_TIMEOUT): await bridge.connect() timed_out = False if timed_out or not bridge.is_connected(): await bridge.close() if timed_out: raise ConfigEntryNotReady( f"Timed out while trying to connect to {host}") if not bridge.is_connected(): raise ConfigEntryNotReady(f"Cannot connect to {host}") _LOGGER.debug("Connected to Lutron Caseta bridge via LEAP at %s", host) devices = bridge.get_devices() bridge_device = devices[BRIDGE_DEVICE_ID] if not config_entry.unique_id: hass.config_entries.async_update_entry(config_entry, unique_id=serial_to_unique_id( bridge_device["serial"])) buttons = bridge.buttons _async_register_bridge_device(hass, entry_id, bridge_device) button_devices = _async_register_button_devices(hass, entry_id, bridge_device, buttons) _async_subscribe_pico_remote_events(hass, bridge, buttons) # Store this bridge (keyed by entry_id) so it can be retrieved by the # platforms we're setting up. hass.data[DOMAIN][entry_id] = LutronCasetaData(bridge, bridge_device, button_devices) hass.config_entries.async_setup_platforms(config_entry, PLATFORMS) return True
async def async_setup_entry(hass, config_entry): """Set up a bridge from a config entry.""" host = config_entry.data[CONF_HOST] keyfile = hass.config.path(config_entry.data[CONF_KEYFILE]) certfile = hass.config.path(config_entry.data[CONF_CERTFILE]) ca_certs = hass.config.path(config_entry.data[CONF_CA_CERTS]) bridge = None try: bridge = Smartbridge.create_tls(hostname=host, keyfile=keyfile, certfile=certfile, ca_certs=ca_certs) except ssl.SSLError: _LOGGER.error("Invalid certificate used to connect to bridge at %s", host) return False timed_out = True try: async with async_timeout.timeout(BRIDGE_TIMEOUT): await bridge.connect() timed_out = False except asyncio.TimeoutError: _LOGGER.error("Timeout while trying to connect to bridge at %s", host) if timed_out or not bridge.is_connected(): await bridge.close() raise ConfigEntryNotReady _LOGGER.debug("Connected to Lutron Caseta bridge via LEAP at %s", host) devices = bridge.get_devices() bridge_device = devices[BRIDGE_DEVICE_ID] await _async_register_bridge_device(hass, config_entry.entry_id, bridge_device) # Store this bridge (keyed by entry_id) so it can be retrieved by the # components we're setting up. hass.data[DOMAIN][config_entry.entry_id] = { BRIDGE_LEAP: bridge, BRIDGE_DEVICE: bridge_device, BUTTON_DEVICES: {}, BRIDGE_LIP: None, } if bridge.lip_devices: # If the bridge also supports LIP (Lutron Integration Protocol) # we can fire events when pico buttons are pressed to allow # pico remotes to control other devices. await async_setup_lip(hass, config_entry, bridge.lip_devices) for component in LUTRON_CASETA_COMPONENTS: hass.async_create_task( hass.config_entries.async_forward_entry_setup( config_entry, component)) return True
async def _bridge_connect(self): self.sb = Smartbridge.create_tls(hostname=self.lutron_bridge_ip, keyfile='./caseta.key', certfile='./caseta.crt', ca_certs='caseta-bridge.crt', ) await self.sb.connect() if self.sb.is_connected(): LOGGER.info("Successfully connected to bridge!") else: LOGGER.error("Could not connect to bridge") self.connecting = False
def bridge_connect(self): asyncio.set_event_loop(asyncio.new_event_loop()) loop = asyncio.get_event_loop() self.sb = Smartbridge.create_tls(hostname=self.lutron_bridge_ip, keyfile='./caseta.key', certfile='./caseta.crt', ca_certs='caseta-bridge.crt', loop=loop) loop.run_until_complete(self.sb.connect()) if self.sb.is_connected(): LOGGER.info("Successfully connected to bridge!") else: LOGGER.error("Could not connect to bridge")