def setup_platform( hass: HomeAssistant, config: ConfigType, add_entities: AddEntitiesCallback, discovery_info: DiscoveryInfoType | None = None, ) -> None: """Set up the flic platform.""" # Initialize flic client responsible for # connecting to buttons and retrieving events host = config.get(CONF_HOST) port = config.get(CONF_PORT) discovery = config.get(CONF_DISCOVERY) try: client = pyflic.FlicClient(host, port) except ConnectionRefusedError: _LOGGER.error("Failed to connect to flic server") return def new_button_callback(address): """Set up newly verified button as device in Home Assistant.""" setup_button(hass, config, add_entities, client, address) client.on_new_verified_button = new_button_callback if discovery: start_scanning(config, add_entities, client) hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: client.close()) # Start the pyflic event handling thread threading.Thread(target=client.handle_events).start() def get_info_callback(items): """Add entities for already verified buttons.""" addresses = items["bd_addr_of_verified_buttons"] or [] for address in addresses: setup_button(hass, config, add_entities, client, address) # Get addresses of already verified buttons client.get_info(get_info_callback)
def async_setup_platform(hass, config, async_add_entities, discovery_info=None): """Setup the flic platform.""" import pyflic # Initialize flic client responsible for # connecting to buttons and retrieving events host = config.get(CONF_HOST) port = config.get(CONF_PORT) discovery = config.get(CONF_DISCOVERY) try: client = pyflic.FlicClient(host, port) except ConnectionRefusedError: _LOGGER.error("Failed to connect to flic server.") return def new_button_callback(address): """Setup newly verified button as device in home assistant.""" hass.add_job( async_setup_button(hass, config, async_add_entities, client, address)) client.on_new_verified_button = new_button_callback if discovery: start_scanning(hass, config, async_add_entities, client) hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, lambda event: client.close()) hass.loop.run_in_executor(None, client.handle_events) # Get addresses of already verified buttons addresses = yield from async_get_verified_addresses(client) if addresses: for address in addresses: yield from async_setup_button(hass, config, async_add_entities, client, address)