async def test_full_user_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test the full manual user flow from start to finish.""" aioclient_mock.post( "http://192.168.1.123:80/mf", text=load_fixture("modern_forms/device_info.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) assert result.get("step_id") == "user" assert result.get("type") == RESULT_TYPE_FORM assert "flow_id" in result with patch( "homeassistant.components.modern_forms.async_setup_entry", return_value=True, ) as mock_setup_entry: result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_HOST: "192.168.1.123"} ) assert result2.get("title") == "ModernFormsFan" assert "data" in result2 assert result2.get("type") == RESULT_TYPE_CREATE_ENTRY assert result2["data"][CONF_HOST] == "192.168.1.123" assert result2["data"][CONF_MAC] == "AA:BB:CC:DD:EE:FF" assert len(mock_setup_entry.mock_calls) == 1
async def test_user_device_exists_abort( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test we abort zeroconf flow if Modern Forms device already configured.""" aioclient_mock.post( "http://192.168.1.123:80/mf", text=load_fixture("modern_forms/device_info.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) await init_integration(hass, aioclient_mock, skip_setup=True) await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data={ "host": "192.168.1.123", "hostname": "example.local.", "properties": {CONF_MAC: "AA:BB:CC:DD:EE:FF"}, }, ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data={ "host": "192.168.1.123", "hostname": "example.local.", "properties": {CONF_MAC: "AA:BB:CC:DD:EE:FF"}, }, ) assert result.get("type") == RESULT_TYPE_ABORT assert result.get("reason") == "already_configured"
async def test_zeroconf_confirm_connection_error( update_mock: MagicMock, hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test we abort zeroconf flow on Modern Forms connection error.""" aioclient_mock.post("http://192.168.1.123:80/mf", exc=aiohttp.ClientError) result = await hass.config_entries.flow.async_init( DOMAIN, context={ "source": SOURCE_ZEROCONF, CONF_HOST: "example.com", CONF_NAME: "test", }, data=zeroconf.ZeroconfServiceInfo( host="192.168.1.123", addresses=["192.168.1.123"], hostname="example.com.", name="mock_name", port=None, properties={}, type="mock_type", ), ) assert result.get("type") == RESULT_TYPE_ABORT assert result.get("reason") == "cannot_connect"
async def test_hassio_update_instance_running( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test we only allow a single config flow.""" aioclient_mock.get( "http://mock-adguard-updated:3000/control/status", json={"version": "v0.99.0"}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) aioclient_mock.get( "http://mock-adguard:3000/control/status", json={"version": "v0.99.0"}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) entry = MockConfigEntry( domain="adguard", data={ "host": "mock-adguard", "port": "3000", "verify_ssl": False, "username": None, "password": None, "ssl": False, }, ) entry.add_to_hass(hass) with patch.object( hass.config_entries, "async_forward_entry_setup", return_value=True, ) as mock_load: assert await hass.config_entries.async_setup(entry.entry_id) assert entry.state == config_entries.ENTRY_STATE_LOADED assert len(mock_load.mock_calls) == 2 with patch.object( hass.config_entries, "async_forward_entry_unload", return_value=True, ) as mock_unload, patch.object( hass.config_entries, "async_forward_entry_setup", return_value=True, ) as mock_load: result = await hass.config_entries.flow.async_init( "adguard", data={ "addon": "AdGuard Home Addon", "host": "mock-adguard-updated", "port": "3000", }, context={"source": "hassio"}, ) assert len(mock_unload.mock_calls) == 2 assert len(mock_load.mock_calls) == 2 assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "existing_instance_updated" assert entry.data["host"] == "mock-adguard-updated"
async def test_config_entry_not_ready( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker ) -> None: """Test configuration entry not ready on library error.""" aioclient_mock.post("http://127.0.0.1:10000/retrieve", exc=aiohttp.ClientError) entry = await init_integration(hass, aioclient_mock) assert entry.state == ENTRY_STATE_SETUP_RETRY
async def setup_integration( hass: HomeAssistantType, aioclient_mock: AiohttpClientMocker, skip_entry_setup: bool = False, setup_error: bool = False, ) -> MockConfigEntry: """Set up the DirecTV integration in Home Assistant.""" if setup_error: aioclient_mock.get( f"http://{HOST}:8080/info/getVersion", status=500, ) else: mock_connection(aioclient_mock) entry = MockConfigEntry( domain=DOMAIN, unique_id=RECEIVER_ID, data={ CONF_HOST: HOST, CONF_RECEIVER_ID: RECEIVER_ID }, ) entry.add_to_hass(hass) if not skip_entry_setup: await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() return entry
async def setup_integration( opp: OpenPeerPower, aioclient_mock: AiohttpClientMocker, skip_entry_setup: bool = False, setup_error: bool = False, ) -> MockConfigEntry: """Set up the DirecTV integration in Open Peer Power.""" if setup_error: aioclient_mock.get(f"http://{HOST}:8080/info/getVersion", status=HTTP_INTERNAL_SERVER_ERROR) else: mock_connection(aioclient_mock) entry = MockConfigEntry( domain=DOMAIN, unique_id=RECEIVER_ID, data={ CONF_HOST: HOST, CONF_RECEIVER_ID: RECEIVER_ID }, ) entry.add_to_opp(opp) if not skip_entry_setup: await opp.config_entries.async_setup(entry.entry_id) await opp.async_block_till_done() return entry
async def init_integration( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, skip_setup: bool = False, ) -> MockConfigEntry: """Set up the BSBLan integration in Home Assistant.""" aioclient_mock.post( "http://example.local:80/1234/JQ?Parameter=6224,6225,6226", params={"Parameter": "6224,6225,6226"}, text=load_fixture("bsblan/info.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) entry = MockConfigEntry( domain=DOMAIN, unique_id="RVS21.831F/127", data={ CONF_HOST: "example.local", CONF_PASSKEY: "1234", CONF_PORT: 80, CONF_DEVICE_IDENT: "RVS21.831F/127", }, ) entry.add_to_hass(hass) if not skip_setup: await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() return entry
def mock_connection(aioclient_mock: AiohttpClientMocker, authorized=True, conn_error=False) -> None: """Mock the requests to Atag endpoint.""" if conn_error: aioclient_mock.post( "http://127.0.0.1:10000/pair", exc=AtagException, ) aioclient_mock.post( "http://127.0.0.1:10000/retrieve", exc=AtagException, ) return PAIR_REPLY["pair_reply"].update( {"acc_status": AUTHORIZED if authorized else UNAUTHORIZED}) RECEIVE_REPLY["retrieve_reply"].update( {"acc_status": AUTHORIZED if authorized else UNAUTHORIZED}) aioclient_mock.post( "http://127.0.0.1:10000/retrieve", json=RECEIVE_REPLY, ) aioclient_mock.post( "http://127.0.0.1:10000/update", json=UPDATE_REPLY, ) aioclient_mock.post( "http://127.0.0.1:10000/pair", json=PAIR_REPLY, )
async def init_integration( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, rgbw: bool = False, skip_setup: bool = False, mock_type: Callable = modern_forms_call_mock, ) -> MockConfigEntry: """Set up the Modern Forms integration in Home Assistant.""" aioclient_mock.post( "http://192.168.1.123:80/mf", side_effect=mock_type, headers={"Content-Type": CONTENT_TYPE_JSON}, ) entry = MockConfigEntry(domain=DOMAIN, data={ CONF_HOST: "192.168.1.123", CONF_MAC: "AA:BB:CC:DD:EE:FF" }) entry.add_to_hass(hass) if not skip_setup: await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() return entry
async def setup_github_integration( hass: HomeAssistant, mock_config_entry: MockConfigEntry, aioclient_mock: AiohttpClientMocker, ) -> None: """Mock setting up the integration.""" repository_id = 1 for repository in mock_config_entry.options[CONF_REPOSITORIES]: aioclient_mock.get( f"https://api.github.com/repos/{repository}", json={ "full_name": repository, "id": repository_id }, headers={"Content-Type": "application/json"}, ) repository_id += 1 for endpoint in ("issues", "pulls", "releases", "commits"): aioclient_mock.get( f"https://api.github.com/repos/{repository}/{endpoint}", json=[], headers={"Content-Type": "application/json"}, ) mock_config_entry.add_to_hass(hass) setup_result = await hass.config_entries.async_setup( mock_config_entry.entry_id) await hass.async_block_till_done() assert setup_result assert mock_config_entry.state == config_entries.ConfigEntryState.LOADED
async def test_full_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test registering an integration and finishing flow works.""" aioclient_mock.get( f"{'https' if FIXTURE_USER_INPUT[CONF_SSL] else 'http'}" f"://{FIXTURE_USER_INPUT[CONF_HOST]}" f":{FIXTURE_USER_INPUT[CONF_PORT]}/control/status", json={"version": "v0.99.0"}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) assert result assert result.get("flow_id") assert result.get("type") == data_entry_flow.FlowResultType.FORM assert result.get("step_id") == "user" result2 = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=FIXTURE_USER_INPUT) assert result2 assert result2.get("type") == data_entry_flow.FlowResultType.CREATE_ENTRY assert result2.get("title") == FIXTURE_USER_INPUT[CONF_HOST] data = result2.get("data") assert data assert data[CONF_HOST] == FIXTURE_USER_INPUT[CONF_HOST] assert data[CONF_PASSWORD] == FIXTURE_USER_INPUT[CONF_PASSWORD] assert data[CONF_PORT] == FIXTURE_USER_INPUT[CONF_PORT] assert data[CONF_SSL] == FIXTURE_USER_INPUT[CONF_SSL] assert data[CONF_USERNAME] == FIXTURE_USER_INPUT[CONF_USERNAME] assert data[CONF_VERIFY_SSL] == FIXTURE_USER_INPUT[CONF_VERIFY_SSL]
async def test_invalid_token_expiry_in_config_entry( hass: HomeAssistant, component_setup: ComponentSetup, setup_config_entry: MockConfigEntry, aioclient_mock: AiohttpClientMocker, ) -> None: """Exercise case in issue #69623 with invalid token expiration persisted.""" # The token is refreshed and new expiration values are returned expires_in = 86400 expires_at = time.time() + expires_in aioclient_mock.post( "https://oauth2.googleapis.com/token", json={ "refresh_token": "some-refresh-token", "access_token": "some-updated-token", "expires_at": expires_at, "expires_in": expires_in, }, ) assert await component_setup() # Verify token expiration values are updated entries = hass.config_entries.async_entries(DOMAIN) assert len(entries) == 1 assert entries[0].state is ConfigEntryState.LOADED assert entries[0].data["token"]["access_token"] == "some-updated-token" assert entries[0].data["token"]["expires_in"] == expires_in
async def init_integration( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, skip_setup: bool = False, uuid: str = "cfe92100-67c4-11d4-a45f-f8d027761251", unique_id: str = "cfe92100-67c4-11d4-a45f-f8d027761251", ) -> MockConfigEntry: """Set up the IPP integration in Home Assistant.""" fixture = "ipp/get-printer-attributes.bin" aioclient_mock.post( "http://192.168.1.31:631/ipp/print", content=load_fixture_binary(fixture), headers={"Content-Type": "application/ipp"}, ) entry = MockConfigEntry( domain=DOMAIN, unique_id=unique_id, data={ CONF_HOST: "192.168.1.31", CONF_PORT: 631, CONF_SSL: False, CONF_VERIFY_SSL: True, CONF_BASE_PATH: "/ipp/print", CONF_UUID: uuid, }, ) entry.add_to_hass(hass) if not skip_setup: await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() return entry
async def test_custom_speed_unit(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, api_response: str) -> None: """Test Wind Gust speed with custom unit.""" uri = APIURL_TEMPLATE.format(TEST_CONFIG["location"]["longitude"], TEST_CONFIG["location"]["latitude"]) aioclient_mock.get(uri, text=api_response) entry = MockConfigEntry(domain="smhi", data=TEST_CONFIG, version=2) entry.add_to_hass(hass) await hass.config_entries.async_setup(entry.entry_id) await hass.async_block_till_done() state = hass.states.get(ENTITY_ID) assert state assert state.name == "test" assert state.attributes[ATTR_SMHI_WIND_GUST_SPEED] == 16.92 entity_reg = er.async_get(hass) entity_reg.async_update_entity_options( state.entity_id, WEATHER_DOMAIN, {ATTR_WEATHER_WIND_SPEED_UNIT: SPEED_METERS_PER_SECOND}, ) await hass.async_block_till_done() state = hass.states.get(ENTITY_ID) assert state.attributes[ATTR_SMHI_WIND_GUST_SPEED] == 4.7
async def test_full_zeroconf_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test the zeroconf flow from start to finish.""" aioclient_mock.get( "http://127.0.0.1:9123/elgato/accessory-info", text=load_fixture("elgato/info.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await hass.config_entries.flow.async_init( DOMAIN, context={CONF_SOURCE: SOURCE_ZEROCONF}, data={ "host": "127.0.0.1", "hostname": "example.local.", "port": 9123, "properties": {}, }, ) assert result["description_placeholders"] == { CONF_SERIAL_NUMBER: "CN11A1A00001" } assert result["step_id"] == "zeroconf_confirm" assert result["type"] == data_entry_flow.RESULT_TYPE_FORM result = await hass.config_entries.flow.async_configure(result["flow_id"], user_input={}) assert result["data"][CONF_HOST] == "127.0.0.1" assert result["data"][CONF_PORT] == 9123 assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001" assert result["title"] == "CN11A1A00001" assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
async def test_address_already_set_up( opp: OpenPeerPower, aioclient_mock: AiohttpClientMocker) -> None: """Test we abort if address has already been set up.""" MockConfigEntry(domain=DOMAIN, data={ **FIXTURE_USER_INPUT, CONF_ID: "12345" }, title="12345").add_to_opp(opp) aioclient_mock.post( "https://twentemilieuapi.ximmio.com/api/FetchAdress", json={"dataList": [{ "UniqueId": "12345" }]}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await opp.config_entries.flow.async_init( config_flow.DOMAIN, context={"source": config_entries.SOURCE_USER}, data=FIXTURE_USER_INPUT, ) assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["reason"] == "already_configured"
async def test_full_flow_implementation( opp: OpenPeerPower, aioclient_mock: AiohttpClientMocker) -> None: """Test registering an integration and finishing flow works.""" aioclient_mock.post( "https://twentemilieuapi.ximmio.com/api/FetchAdress", json={"dataList": [{ "UniqueId": "12345" }]}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" result = await opp.config_entries.flow.async_configure( result["flow_id"], FIXTURE_USER_INPUT, ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == "12345" assert result["data"][CONF_POST_CODE] == FIXTURE_USER_INPUT[CONF_POST_CODE] assert result["data"][CONF_HOUSE_NUMBER] == FIXTURE_USER_INPUT[ CONF_HOUSE_NUMBER] assert result["data"][CONF_HOUSE_LETTER] == FIXTURE_USER_INPUT[ CONF_HOUSE_LETTER]
async def init_integration( opp: OpenPeerPower, aioclient_mock: AiohttpClientMocker, skip_setup: bool = False, ) -> MockConfigEntry: """Set up the Agent DVR integration in Open Peer Power.""" aioclient_mock.get( "http://example.local:8090/command.cgi?cmd=getStatus", text=load_fixture("agent_dvr/status.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) aioclient_mock.get( "http://example.local:8090/command.cgi?cmd=getObjects", text=load_fixture("agent_dvr/objects.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) entry = MockConfigEntry( domain=DOMAIN, unique_id="c0715bba-c2d0-48ef-9e3e-bc81c9ea4447", data={ CONF_HOST: "example.local", CONF_PORT: 8090, SERVER_URL: "http://example.local:8090/", }, ) entry.add_to_opp(opp) if not skip_setup: await opp.config_entries.async_setup(entry.entry_id) await opp.async_block_till_done() return entry
async def test_light_error(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test error handling of the WLED switches.""" aioclient_mock.post("http://example.local:80/json/state", exc=aiohttp.ClientError) await init_integration(hass, aioclient_mock) await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: "light.wled_rgb_light"}, blocking=True, ) await hass.async_block_till_done() state = hass.states.get("light.wled_rgb_light") assert state.state == STATE_UNAVAILABLE await hass.services.async_call( LIGHT_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: "light.wled_rgb_light_1"}, blocking=True, ) await hass.async_block_till_done() state = hass.states.get("light.wled_rgb_light_1") assert state.state == STATE_UNAVAILABLE
async def test_full_zeroconf_tls_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test the full manual user flow from start to finish.""" aioclient_mock.post( "https://192.168.1.31:631/ipp/print", content=load_fixture_binary("ipp/get-printer-attributes.bin"), headers={"Content-Type": "application/ipp"}, ) discovery_info = MOCK_ZEROCONF_IPPS_SERVICE_INFO.copy() result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_ZEROCONF}, data=discovery_info, ) assert result["step_id"] == "zeroconf_confirm" assert result["type"] == RESULT_TYPE_FORM assert result["description_placeholders"] == {CONF_NAME: "EPSON123456"} result = await hass.config_entries.flow.async_configure(result["flow_id"], user_input={}) assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == "EPSON123456" assert result["data"] assert result["data"][CONF_HOST] == "192.168.1.31" assert result["data"][CONF_NAME] == "EPSON123456" assert result["data"][CONF_UUID] == "cfe92100-67c4-11d4-a45f-f8d027761251" assert result["data"][CONF_SSL]
async def test_hassio_confirm(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test we can finish a config flow.""" aioclient_mock.get( "http://mock-adguard:3000/control/status", json={"version": "v0.99.0"}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await hass.config_entries.flow.async_init( DOMAIN, data={ "addon": "AdGuard Home Addon", "host": "mock-adguard", "port": 3000 }, context={"source": config_entries.SOURCE_HASSIO}, ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "hassio_confirm" assert result["description_placeholders"] == { "addon": "AdGuard Home Addon" } result = await hass.config_entries.flow.async_configure( result["flow_id"], {}) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == "AdGuard Home Addon" assert result["data"][CONF_HOST] == "mock-adguard" assert result["data"][CONF_PASSWORD] is None assert result["data"][CONF_PORT] == 3000 assert result["data"][CONF_SSL] is False assert result["data"][CONF_USERNAME] is None assert result["data"][CONF_VERIFY_SSL]
async def test_full_zeroconf_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test the full manual user flow from start to finish.""" aioclient_mock.get( "http://1.2.3.4:9123/elgato/accessory-info", text=load_fixture("elgato/info.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) flow = config_flow.ElgatoFlowHandler() flow.hass = hass flow.context = {"source": SOURCE_ZEROCONF} result = await flow.async_step_zeroconf({"host": "1.2.3.4", "port": 9123}) assert flow.context[CONF_HOST] == "1.2.3.4" assert flow.context[CONF_PORT] == 9123 assert flow.context[CONF_SERIAL_NUMBER] == "CN11A1A00001" assert result["description_placeholders"] == { CONF_SERIAL_NUMBER: "CN11A1A00001" } assert result["step_id"] == "zeroconf_confirm" assert result["type"] == data_entry_flow.RESULT_TYPE_FORM result = await flow.async_step_zeroconf_confirm( user_input={CONF_HOST: "1.2.3.4"}) assert result["data"][CONF_HOST] == "1.2.3.4" assert result["data"][CONF_PORT] == 9123 assert result["data"][CONF_SERIAL_NUMBER] == "CN11A1A00001" assert result["title"] == "CN11A1A00001" assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
async def test_full_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test registering an integration and finishing flow works.""" aioclient_mock.get( f"{'https' if FIXTURE_USER_INPUT[CONF_SSL] else 'http'}" f"://{FIXTURE_USER_INPUT[CONF_HOST]}" f":{FIXTURE_USER_INPUT[CONF_PORT]}/control/status", json={"version": "v0.99.0"}, headers={"Content-Type": CONTENT_TYPE_JSON}, ) flow = config_flow.AdGuardHomeFlowHandler() flow.hass = hass result = await flow.async_step_user(user_input=None) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" result = await flow.async_step_user(user_input=FIXTURE_USER_INPUT) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == FIXTURE_USER_INPUT[CONF_HOST] assert result["data"][CONF_HOST] == FIXTURE_USER_INPUT[CONF_HOST] assert result["data"][CONF_PASSWORD] == FIXTURE_USER_INPUT[CONF_PASSWORD] assert result["data"][CONF_PORT] == FIXTURE_USER_INPUT[CONF_PORT] assert result["data"][CONF_SSL] == FIXTURE_USER_INPUT[CONF_SSL] assert result["data"][CONF_USERNAME] == FIXTURE_USER_INPUT[CONF_USERNAME] assert result["data"][CONF_VERIFY_SSL] == FIXTURE_USER_INPUT[ CONF_VERIFY_SSL]
async def test_full_user_flow_implementation( opp: OpenPeerPower, aioclient_mock: AiohttpClientMocker) -> None: """Test the full manual user flow from start to finish.""" aioclient_mock.get( "http://192.168.1.123:80/json/", text=load_fixture("wled/rgb.json"), headers={"Content-Type": CONTENT_TYPE_JSON}, ) result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) assert result.get("step_id") == "user" assert result.get("type") == RESULT_TYPE_FORM assert "flow_id" in result result = await opp.config_entries.flow.async_configure( result["flow_id"], user_input={CONF_HOST: "192.168.1.123"}) assert result.get("title") == "192.168.1.123" assert result.get("type") == RESULT_TYPE_CREATE_ENTRY assert "data" in result assert result["data"][CONF_HOST] == "192.168.1.123" assert result["data"][CONF_MAC] == "aabbccddeeff"
async def test_flow_with_activation_failure( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, ) -> None: """Test flow with activation failure of the device.""" aioclient_mock.post( "https://github.com/login/device/code", json={ "device_code": "3584d83530557fdd1f46af8289938c8ef79f9dc5", "user_code": "WDJB-MJHT", "verification_uri": "https://github.com/login/device", "expires_in": 900, "interval": 5, }, headers={"Content-Type": "application/json"}, ) aioclient_mock.post( "https://github.com/login/oauth/access_token", exc=GitHubException("Activation failed"), ) result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}, ) assert result["step_id"] == "device" assert result["type"] == RESULT_TYPE_SHOW_PROGRESS result = await hass.config_entries.flow.async_configure(result["flow_id"]) assert result["type"] == RESULT_TYPE_SHOW_PROGRESS_DONE assert result["step_id"] == "could_not_register"
async def test_full_zeroconf_flow_implementation( hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test the full manual user flow from start to finish.""" aioclient_mock.get( "http://192.168.1.123:80/json/", text=load_fixture("wled/rgb.json"), headers={"Content-Type": "application/json"}, ) flow = config_flow.WLEDFlowHandler() flow.hass = hass flow.context = {"source": SOURCE_ZEROCONF} result = await flow.async_step_zeroconf({ "host": "192.168.1.123", "hostname": "example.local.", "properties": {} }) assert flow.context[CONF_HOST] == "192.168.1.123" assert flow.context[CONF_NAME] == "example" assert result["description_placeholders"] == {CONF_NAME: "example"} assert result["step_id"] == "zeroconf_confirm" assert result["type"] == data_entry_flow.RESULT_TYPE_FORM result = await flow.async_step_zeroconf_confirm(user_input={}) assert result["data"][CONF_HOST] == "192.168.1.123" assert result["data"][CONF_MAC] == "aabbccddeeff" assert result["title"] == "example" assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
def pvpc_aioclient_mock(aioclient_mock: AiohttpClientMocker): """Create a mock config entry.""" aioclient_mock.get( "https://api.esios.ree.es/archives/70/download_json?locale=es&date=2019-10-26", text=load_fixture(f"{DOMAIN}/{FIXTURE_JSON_DATA_2019_10_26}"), ) aioclient_mock.get( "https://api.esios.ree.es/archives/70/download_json?locale=es&date=2019-10-27", text=load_fixture(f"{DOMAIN}/{FIXTURE_JSON_DATA_2019_10_27}"), ) # missing day aioclient_mock.get( "https://api.esios.ree.es/archives/70/download_json?locale=es&date=2019-10-28", text='{"message":"No values for specified archive"}', ) aioclient_mock.get( "https://api.esios.ree.es/archives/70/download_json?locale=es&date=2019-10-29", text=load_fixture(f"{DOMAIN}/{FIXTURE_JSON_DATA_2019_10_29}"), ) # new format for prices >= 2021-06-01 aioclient_mock.get( "https://api.esios.ree.es/archives/70/download_json?locale=es&date=2021-06-01", text=load_fixture(f"{DOMAIN}/{FIXTURE_JSON_DATA_2021_06_01}"), ) return aioclient_mock
async def test_effect_service_error(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog) -> None: """Test error handling of the WLED effect service.""" aioclient_mock.post("http://192.168.1.123:80/json/state", text="", status=400) await init_integration(hass, aioclient_mock) with patch( "homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh" ): await hass.services.async_call( DOMAIN, SERVICE_EFFECT, { ATTR_ENTITY_ID: "light.wled_rgb_light", ATTR_EFFECT: 9 }, blocking=True, ) await hass.async_block_till_done() state = hass.states.get("light.wled_rgb_light") assert state.state == STATE_ON assert "Invalid response from API" in caplog.text
async def test_form_invalid_auth(hass: HomeAssistant, aioclient_mock: AiohttpClientMocker) -> None: """Test we handle invalid auth error.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_USER}) aioclient_mock.get( "http://1.1.1.1:8080/status.json?show_avail=1", exc=aiohttp.ClientResponseError(Mock(), (), status=401), ) result2 = await hass.config_entries.flow.async_configure( result["flow_id"], { "host": "1.1.1.1", "port": 8080, "username": "******", "password": "******" }, ) assert result2["type"] == FlowResultType.FORM assert result2["errors"] == { "username": "******", "password": "******" }
def mock_session(): """Mock aiohttp.ClientSession.""" mocker = AiohttpClientMocker() with patch('aiohttp.ClientSession', side_effect=lambda *args, **kwargs: mocker.create_session(asyncio.get_event_loop())): yield mocker