async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: """Set up BleBox devices from a config entry.""" websession = async_get_clientsession(hass) host = entry.data[CONF_HOST] port = entry.data[CONF_PORT] timeout = DEFAULT_SETUP_TIMEOUT api_host = ApiHost(host, port, timeout, websession, hass.loop) try: product = await Products.async_from_host(api_host) except Error as ex: _LOGGER.error("Identify failed at %s:%d (%s)", api_host.host, api_host.port, ex) raise ConfigEntryNotReady from ex domain = hass.data.setdefault(DOMAIN, {}) domain_entry = domain.setdefault(entry.entry_id, {}) product = domain_entry.setdefault(PRODUCT, product) hass.config_entries.async_setup_platforms(entry, PLATFORMS) return True
async def test_unknown_product_without_device_section( self, aioclient_mock, data): host = "172.1.2.3" with pytest.raises(error.UnsupportedBoxResponse, match=r"unknownBox"): json_get_expect(aioclient_mock, f"http://{host}:80/api/device/state", json=data) port = 80 timeout = 2 api_host = ApiHost(host, port, timeout, aioclient_mock, None, None) await Products.async_from_host(api_host)
async def async_entities(self, session): """Get a created entity at the given index.""" host = self.IP port = 80 timeout = 2 api_host = ApiHost(host, port, timeout, session, None, self.LOGGER) product = await Products.async_from_host(api_host) return [ self.ENTITY_CLASS(feature) for feature in product.features[self.DEVCLASS] ]
async def async_step_user(self, user_input=None): """Handle initial user-triggered config step.""" hass = self.hass schema = create_schema(user_input) if user_input is None: return self.async_show_form( step_id="user", data_schema=schema, errors={}, description_placeholders={}, ) addr = host_port(user_input) for entry in hass.config_entries.async_entries(DOMAIN): if addr == host_port(entry.data): host, port = addr return self.async_abort( reason=ADDRESS_ALREADY_CONFIGURED, description_placeholders={"address": f"{host}:{port}"}, ) websession = async_get_clientsession(hass) api_host = ApiHost(*addr, DEFAULT_SETUP_TIMEOUT, websession, hass.loop, _LOGGER) try: product = await Products.async_from_host(api_host) except UnsupportedBoxVersion as ex: return self.handle_step_exception("user", ex, schema, *addr, UNSUPPORTED_VERSION, _LOGGER.debug) except Error as ex: return self.handle_step_exception("user", ex, schema, *addr, CANNOT_CONNECT, _LOGGER.warning) except RuntimeError as ex: return self.handle_step_exception("user", ex, schema, *addr, UNKNOWN, _LOGGER.error) # Check if configured but IP changed since await self.async_set_unique_id(product.unique_id) self._abort_if_unique_id_configured() return self.async_create_entry(title=product.name, data=user_input)
async def async_step_user(self, user_input=None): """Handle initial user-triggered config step.""" errors = {} if user_input is not None: def host_port(data): return (data[CONF_HOST], data[CONF_PORT]) addr = host_port(user_input) for entry in self.hass.config_entries.async_entries(DOMAIN): if addr == host_port(entry.data): address = "{0}:{1}".format(*addr) return self.async_abort( reason="address_already_configured", description_placeholders={"address": address}, ) try: hass = self.hass websession = async_get_clientsession(hass) api_host = ApiHost(*addr, None, websession, hass.loop, _LOGGER) try: product = await Products.async_from_host(api_host) except UnsupportedBoxVersion as ex: _LOGGER.error("Outdated device firmware at %s:%d (%s)", *addr, ex) raise UnsupportedVersion from ex except Error as ex: _LOGGER.error("Failed to identify device at %s:%d (%s)", *addr, ex) raise CannotConnect from ex # Check if configured but IP changed since mac_address = product.unique_id await self.async_set_unique_id(mac_address) self._abort_if_unique_id_configured() # Return some info we want to store in the config entry. info = {"title": product.name} return self.async_create_entry(title=info["title"], data=user_input) except UnsupportedVersion: errors["base"] = "unsupported_version" except CannotConnect: errors["base"] = "cannot_connect" except RuntimeError as ex: _LOGGER.exception("Unexpected exception: %s", ex) errors["base"] = "unknown" if user_input is not None: host = user_input[CONF_HOST] port = user_input[CONF_PORT] else: host = PLACEHOLDER_HOST port = PLACEHOLDER_PORT return self.async_show_form( step_id="user", data_schema=create_schema(host, port), errors=errors, )