async def test_create_local_entry_awair_error(hass: HomeAssistant): """Test overall flow when using local API and device is returns error.""" with patch( "python_awair.AwairClient.query", side_effect=AwairError(), ): menu_step = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=LOCAL_CONFIG) form_step = await hass.config_entries.flow.async_configure( menu_step["flow_id"], {"next_step_id": "local"}, ) # We're being shown the local instructions form_step = await hass.config_entries.flow.async_configure( form_step["flow_id"], {}, ) result = await hass.config_entries.flow.async_configure( form_step["flow_id"], LOCAL_CONFIG, ) # User is returned to form to try again assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["step_id"] == "local_pick"
def __init__(self, session: ClientSession, device_addrs: List[str]) -> None: """Initialize the Awair local sensors API wrapper.""" self._device_addrs = device_addrs if len(device_addrs) > 0: self.client = AwairClient(AccessTokenAuth(""), session) else: raise AwairError("No local Awair device addresses supplied!")
async def test_reauth_error(hass: HomeAssistant) -> None: """Test reauth flow.""" mock_config = MockConfigEntry( domain=DOMAIN, unique_id=CLOUD_UNIQUE_ID, data={ **CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah" }, ) mock_config.add_to_hass(hass) result = await hass.config_entries.flow.async_init( DOMAIN, context={ "source": SOURCE_REAUTH, "unique_id": CLOUD_UNIQUE_ID }, data={ **CLOUD_CONFIG, CONF_ACCESS_TOKEN: "blah" }, ) assert result["type"] == data_entry_flow.FlowResultType.FORM assert result["step_id"] == "reauth_confirm" assert result["errors"] == {} with patch("python_awair.AwairClient.query", side_effect=AwairError()): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=CLOUD_CONFIG, ) assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["reason"] == "unknown"
async def test_unexpected_api_error(hass): """Test that we abort on generic errors.""" with patch("python_awair.AwairClient.query", side_effect=AwairError()): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=CONFIG) assert result["type"] == "abort" assert result["reason"] == "unknown"
async def test_import_aborts_on_api_error(hass): """Test config.yaml imports on api error.""" with patch("python_awair.AwairClient.query", side_effect=AwairError()): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, data={CONF_ACCESS_TOKEN: CONFIG[CONF_ACCESS_TOKEN]}, ) assert result["type"] == "abort" assert result["reason"] == "unknown"
async def test_reauth(hass): """Test reauth flow.""" with patch("python_awair.AwairClient.query", side_effect=[USER_FIXTURE, DEVICES_FIXTURE]), patch( "homeassistant.components.awair.sensor.async_setup_entry", return_value=True, ): mock_config = MockConfigEntry(domain=DOMAIN, unique_id=UNIQUE_ID, data=CONFIG) mock_config.add_to_hass(hass) hass.config_entries.async_update_entry(mock_config, data={ **CONFIG, CONF_ACCESS_TOKEN: "blah" }) result = await hass.config_entries.flow.async_init( DOMAIN, context={ "source": "reauth", "unique_id": UNIQUE_ID }, data=CONFIG, ) assert result["type"] == "abort" assert result["reason"] == "reauth_successful" with patch("python_awair.AwairClient.query", side_effect=AuthError()): result = await hass.config_entries.flow.async_init( DOMAIN, context={ "source": "reauth", "unique_id": UNIQUE_ID }, data=CONFIG, ) assert result["errors"] == {CONF_ACCESS_TOKEN: "auth"} with patch("python_awair.AwairClient.query", side_effect=AwairError()): result = await hass.config_entries.flow.async_init( DOMAIN, context={ "source": "reauth", "unique_id": UNIQUE_ID }, data=CONFIG, ) assert result["type"] == "abort" assert result["reason"] == "unknown"
def __init__( self, session: ClientSession, access_token: Optional[str] = None, authenticator: Optional[AwairAuth] = None, ) -> None: """Initialize the Awair API wrapper.""" if authenticator: self.client = AwairClient(authenticator, session) elif access_token: self.client = AwairClient(AccessTokenAuth(access_token), session) else: raise AwairError("No authentication supplied!")
def __handle_non_200_error(resp: ClientResponse) -> NoReturn: if resp.status == 400: raise QueryError() if resp.status == 401 or resp.status == 403: raise AuthError() if resp.status == 404: raise NotFoundError() if resp.status == 429: raise RatelimitError() raise AwairError()
def __check_errors_array(json: Dict[Any, Any]) -> None: """Check for an "errors" array and process it. Holdover from the GraphQL API, unclear if we could still get messages like this. """ if "errors" in json: messages = [] for error in json["errors"]: if "Too many requests" in error["message"]: raise RatelimitError() messages.append(error.get("message", "Unknown error")) if messages: raise AwairError(", ".join(messages))
async def test_unexpected_api_error(hass: HomeAssistant): """Test that we abort on generic errors.""" with patch("python_awair.AwairClient.query", side_effect=AwairError()): menu_step = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=CLOUD_CONFIG) form_step = await hass.config_entries.flow.async_configure( menu_step["flow_id"], {"next_step_id": "cloud"}, ) result = await hass.config_entries.flow.async_configure( form_step["flow_id"], CLOUD_CONFIG, ) assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["reason"] == "unknown"