async def async_step_user(self, user_input: dict | None = None) -> FlowResult: """Handle the start of the config flow.""" if not user_input: return await self._show_form() await self.async_set_unique_id(user_input[CONF_APP_KEY]) self._abort_if_unique_id_configured() session = aiohttp_client.async_get_clientsession(self.hass) api = API(user_input[CONF_APP_KEY], user_input[CONF_API_KEY], session=session) try: devices = await api.get_devices() except AmbientError: return await self._show_form({"base": "invalid_key"}) if not devices: return await self._show_form({"base": "no_devices"}) # The Application Key (which identifies each config entry) is too long # to show nicely in the UI, so we take the first 12 characters (similar # to how GitHub does it): return self.async_create_entry(title=user_input[CONF_APP_KEY][:12], data=user_input)
async def test_custom_logger(aresponses, caplog): """Test that a custom logger is used when provided to the client.""" caplog.set_level(logging.DEBUG) custom_logger = logging.getLogger("custom") aresponses.add( "rt.ambientweather.net", f"/v1/devices/{TEST_MAC}", "get", aresponses.Response( text=load_fixture("device_details_response.json"), status=200, headers={"Content-Type": "application/json; charset=utf-8"}, ), ) async with aiohttp.ClientSession() as session: api = API(TEST_API_KEY, TEST_APP_KEY, session=session, logger=custom_logger) await api.get_device_details(TEST_MAC, end_date=datetime.date(2019, 1, 6)) assert any( record.name == "custom" and "Received data" in record.message for record in caplog.records)
async def test_api_error(aresponses): """Test the REST API raising an exception upon HTTP error.""" aresponses.add( "rt.ambientweather.net", "/v1/devices", "get", aresponses.Response(text="", status=500), ) async with aiohttp.ClientSession() as session: api = API(TEST_API_KEY, TEST_APP_KEY, session=session) with pytest.raises(RequestError): await api.get_devices()
async def main() -> None: """Create the aiohttp session and run the example.""" logging.basicConfig(level=logging.INFO) async with ClientSession() as session: try: api = API(APP_KEY, API_KEY, session=session) devices = await api.get_devices() _LOGGER.info("Devices: %s", devices) for device in devices: details = await api.get_device_details(device["macAddress"]) _LOGGER.info("Device Details (%s): %s", device["macAddress"], details) except AmbientError as err: _LOGGER.error("There was an error: %s", err)
async def test_session_from_scratch(aresponses): """Test that an aiohttp ClientSession is created on the fly if needed.""" aresponses.add( "rt.ambientweather.net", "/v1/devices", "get", aresponses.Response( text=load_fixture("devices_response.json"), status=200, headers={"Content-Type": "application/json; charset=utf-8"}, ), ) api = API(TEST_API_KEY, TEST_APP_KEY) devices = await api.get_devices() assert len(devices) == 2
async def test_get_devices(aresponses): """Test retrieving devices from the REST API.""" aresponses.add( "rt.ambientweather.net", "/v1/devices", "get", aresponses.Response( text=load_fixture("devices_response.json"), status=200, headers={"Content-Type": "application/json; charset=utf-8"}, ), ) async with aiohttp.ClientSession() as session: api = API(TEST_API_KEY, TEST_APP_KEY, session=session) devices = await api.get_devices() assert len(devices) == 2