async def test_request_all_info(hass): """Test request config with all possible info.""" exp_attr = { ATTR_FRIENDLY_NAME: "Test Request", configurator.ATTR_DESCRIPTION: """config description [link name](link url) ![Description image](config image url)""", configurator.ATTR_SUBMIT_CAPTION: "config submit caption", configurator.ATTR_FIELDS: [], configurator.ATTR_ENTITY_PICTURE: "config entity picture", configurator.ATTR_CONFIGURE_ID: configurator.async_request_config( hass, name="Test Request", callback=lambda _: None, description="config description", description_image="config image url", submit_caption="config submit caption", fields=None, link_name="link name", link_url="link url", entity_picture="config entity picture", ), } states = hass.states.async_all() assert len(states) == 1 state = states[0] assert state.state == configurator.STATE_CONFIGURE assert state.attributes == exp_attr
async def test_request_done_works(hass): """Test if calling request done works.""" request_id = configurator.async_request_config(hass, "Test Request", lambda _: None) configurator.async_request_done(hass, request_id) assert len(hass.states.async_all()) == 1 async_fire_time_changed(hass, dt_util.utcnow() + timedelta(seconds=1)) await hass.async_block_till_done() assert len(hass.states.async_all()) == 0
async def test_request_done_works(hass): """Test if calling request done works.""" request_id = configurator.async_request_config(hass, "Test Request", lambda _: None) configurator.async_request_done(hass, request_id) assert 1 == len(hass.states.async_all()) hass.bus.async_fire(EVENT_TIME_CHANGED) await hass.async_block_till_done() assert 0 == len(hass.states.async_all())
async def test_state_change_on_notify_errors(hass): """Test state change on notify errors.""" request_id = configurator.async_request_config(hass, "Test Request", lambda _: None) error = "Oh no bad bad bad" configurator.async_notify_errors(hass, request_id, error) states = hass.states.async_all() assert len(states) == 1 state = states[0] assert state.attributes.get(configurator.ATTR_ERRORS) == error
async def test_callback_called_on_configure(hass): """Test if our callback gets called when configure service called.""" calls = [] request_id = configurator.async_request_config(hass, "Test Request", lambda _: calls.append(1)) await hass.services.async_call( configurator.DOMAIN, configurator.SERVICE_CONFIGURE, {configurator.ATTR_CONFIGURE_ID: request_id}, ) await hass.async_block_till_done() assert 1 == len(calls), "Callback not called"
async def test_request_least_info(hass): """Test request config with least amount of data.""" request_id = configurator.async_request_config(hass, "Test Request", lambda _: None) assert 1 == len(hass.services.async_services().get( configurator.DOMAIN, [])), "No new service registered" states = hass.states.async_all() assert 1 == len(states), "Expected a new state registered" state = states[0] assert configurator.STATE_CONFIGURE == state.state assert request_id == state.attributes.get(configurator.ATTR_CONFIGURE_ID)
def _register_new_account(hass, account_name, api_key, shared_secret, stored_rtm_config, component): request_id = None api = Rtm(api_key, shared_secret, "write", None) url, frob = api.authenticate_desktop() _LOGGER.debug("Sent authentication request to server") def register_account_callback(_): """Call for register the configurator.""" api.retrieve_token(frob) token = api.token if api.token is None: _LOGGER.error("Failed to register, please try again") configurator.notify_errors( hass, request_id, "Failed to register, please try again.") return stored_rtm_config.set_token(account_name, token) _LOGGER.debug("Retrieved new token from server") _create_instance( hass, account_name, api_key, shared_secret, token, stored_rtm_config, component, ) configurator.request_done(hass, request_id) request_id = configurator.async_request_config( hass, f"{DOMAIN} - {account_name}", callback=register_account_callback, description=("You need to log in to Remember The Milk to" "connect your account. \n\n" "Step 1: Click on the link 'Remember The Milk login'\n\n" "Step 2: Click on 'login completed'"), link_name="Remember The Milk login", link_url=url, submit_caption="login completed", )
def async_request_configuration(hass, config, host, web_root): """Request configuration steps from the user.""" # We got an error if this method is called while we are configuring if host in _CONFIGURING: configurator.async_notify_errors( hass, _CONFIGURING[host], "Failed to register, please try again.") return async def async_configuration_callback(data): """Handle configuration changes.""" api_key = data.get(CONF_API_KEY) sab_api = SabnzbdApi(host, api_key, web_root=web_root, session=async_get_clientsession(hass)) if not await async_check_sabnzbd(sab_api): return def success(): """Signal successful setup.""" conf = load_json(hass.config.path(CONFIG_FILE)) conf[host] = {CONF_API_KEY: api_key} save_json(hass.config.path(CONFIG_FILE), conf) req_config = _CONFIGURING.pop(host) configurator.request_done(hass, req_config) hass.async_add_job(success) async_setup_sabnzbd(hass, sab_api, config, config.get(CONF_NAME, DEFAULT_NAME)) _CONFIGURING[host] = configurator.async_request_config( DEFAULT_NAME, async_configuration_callback, description="Enter the API Key", submit_caption="Confirm", fields=[{ "id": CONF_API_KEY, "name": "API Key", "type": "" }], )