async def test_options_flow_incorrect_prefixes(hass: HomeAssistant) -> None: """Test config flow options.""" config_entry = MockConfigEntry( domain=DOMAIN, unique_id=MOCK_UNIQUE_ID, data=MOCK_CONFIG_ENTRY, options={CONF_PREFIXES: None}, ) config_entry.add_to_hass(hass) with patch( "homeassistant.components.fritzbox_callmonitor.async_setup_entry", return_value=True, ): await hass.config_entries.async_setup(config_entry.entry_id) result = await hass.config_entries.options.async_init( config_entry.entry_id) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "init" result = await hass.config_entries.options.async_configure( result["flow_id"], user_input={CONF_PREFIXES: ""}) assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {"base": RESULT_MALFORMED_PREFIXES}
async def test_connection_errors_retry(hass, dsmr_connection_fixture): """Connection should be retried on error during setup.""" (connection_factory, transport, protocol) = dsmr_connection_fixture entry_data = { "port": "/dev/ttyUSB0", "dsmr_version": "2.2", "precision": 4, "reconnect_interval": 0, "serial_id": "1234", "serial_id_gas": "5678", } # override the mock to have it fail the first time and succeed after first_fail_connection_factory = MagicMock( return_value=(transport, protocol), side_effect=chain([TimeoutError], repeat(DEFAULT)), ) mock_entry = MockConfigEntry(domain="dsmr", unique_id="/dev/ttyUSB0", data=entry_data) mock_entry.add_to_hass(hass) with patch( "homeassistant.components.dsmr.sensor.create_dsmr_reader", first_fail_connection_factory, ): await hass.config_entries.async_setup(mock_entry.entry_id) await hass.async_block_till_done() # wait for sleep to resolve await hass.async_block_till_done() assert first_fail_connection_factory.call_count >= 2, "connecting not retried"
async def test_onboarding(hass: HomeAssistant) -> None: """Test setting up via onboarding.""" with patch(MODULE, return_value=MagicMock()): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "onboarding"}, ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY
async def _async_setup_component(opp, detected): mocked_under_voltage = MagicMock() type(mocked_under_voltage).get = MagicMock(return_value=detected) entry = MockConfigEntry(domain=DOMAIN) entry.add_to_opp(opp) with patch(MODULE, return_value=mocked_under_voltage): await async_setup_component(opp, DOMAIN, {DOMAIN: {}}) await opp.async_block_till_done() return mocked_under_voltage
async def test_onboarding_not_supported(hass: HomeAssistant) -> None: """Test setting up via onboarding with unsupported system.""" with patch(MODULE, return_value=None): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": "onboarding"}, ) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "no_devices_found"
async def test_setup_multiple_phonebooks(hass: HomeAssistant) -> None: """Test setting up manually.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "user" with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_ids", new_callable=PropertyMock, return_value=[0, 1], ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.call_action", return_value=MOCK_DEVICE_INFO, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_info", side_effect=[MOCK_PHONEBOOK_INFO_1, MOCK_PHONEBOOK_INFO_2], ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "phonebook" assert result["errors"] == {} with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.modelname", return_value=MOCK_PHONEBOOK_NAME_1, ), patch( "homeassistant.components.fritzbox_callmonitor.async_setup_entry", return_value=True, ) as mock_setup_entry: result = await hass.config_entries.flow.async_configure( result["flow_id"], {CONF_PHONEBOOK: MOCK_PHONEBOOK_NAME_2}, ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == MOCK_PHONEBOOK_NAME_2 assert result["data"] == { CONF_HOST: MOCK_HOST, CONF_PORT: MOCK_PORT, CONF_PASSWORD: MOCK_PASSWORD, CONF_USERNAME: MOCK_USERNAME, CONF_PREFIXES: None, CONF_PHONEBOOK: 1, SERIAL_NUMBER: MOCK_SERIAL_NUMBER, } assert len(mock_setup_entry.mock_calls) == 1
async def test_not_supported(hass: HomeAssistant) -> None: """Test setting up on not supported system.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch(MODULE, return_value=None): result = await hass.config_entries.flow.async_configure( result["flow_id"], {}) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == "no_devices_found"
async def test_not_discovered(hass: HomeAssistant) -> None: """Test setting up with no devices discovered.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch("homeassistant.components.wemo.config_flow.pywemo") as mock_pywemo: mock_pywemo.discover_devices.return_value = [] result = await hass.config_entries.flow.async_configure(result["flow_id"], {}) assert result["type"] == data_entry_flow.FlowResultType.ABORT assert result["reason"] == "no_devices_found"
async def test_setup_platform(hass, dsmr_connection_fixture): """Test setup of platform.""" async_add_entities = MagicMock() entry_data = { "platform": DOMAIN, "port": "/dev/ttyUSB0", "dsmr_version": "2.2", "precision": 4, "reconnect_interval": 30, } serial_data = {"serial_id": "1234", "serial_id_gas": "5678"} with patch( "homeassistant.components.dsmr.async_setup", return_value=True ), patch( "homeassistant.components.dsmr.async_setup_entry", return_value=True ), patch( "homeassistant.components.dsmr.config_flow._validate_dsmr_connection", return_value=serial_data, ): assert await async_setup_component(hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: entry_data}) await hass.async_block_till_done() assert not async_add_entities.called # Check config entry conf_entries = hass.config_entries.async_entries(DOMAIN) assert len(conf_entries) == 1 entry = conf_entries[0] assert entry.state == "loaded" assert entry.data == {**entry_data, **serial_data}
async def test_setup(hass: HomeAssistant) -> None: """Test setting up manually.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "confirm" assert not result["errors"] with patch(MODULE, return_value=MagicMock()): result = await hass.config_entries.flow.async_configure( result["flow_id"], {}) assert result["type"] == RESULT_TYPE_CREATE_ENTRY
async def test_action(opp, push_registration): """Test for turn_on and turn_off actions.""" webhook_id = push_registration["webhook_id"] assert await async_setup_component( opp, automation.DOMAIN, { automation.DOMAIN: [ { "trigger": { "platform": "event", "event_type": "test_notify", }, "action": [ { "variables": { "name": "Paulus" } }, { "domain": DOMAIN, "device_id": opp.data[DOMAIN]["devices"][webhook_id].id, "type": "notify", "message": "Hello {{ name }}", }, ], }, ] }, ) service_name = util.get_notify_service(opp, webhook_id) # Make sure it was actually registered assert opp.services.has_service("notify", service_name) with patch( "openpeerpower.components.mobile_app.notify.MobileAppNotificationService.async_send_message" ) as mock_send_message: opp.bus.async_fire("test_notify") await opp.async_block_till_done() assert len(mock_send_message.mock_calls) == 1 assert mock_send_message.mock_calls[0][2] == { "target": [webhook_id], "message": "Hello Paulus", "data": None, }
async def test_create_entry(hass, aioclient_mock): """Test that the user step works.""" aioclient_mock.get(API_POINT_URL, text=load_fixture("valid_station.json", "airly")) with patch("homeassistant.components.airly.async_setup_entry", return_value=True): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=CONFIG ) assert result["type"] == data_entry_flow.FlowResultType.CREATE_ENTRY assert result["title"] == CONFIG[CONF_NAME] assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE] assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE] assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY] assert result["data"][CONF_USE_NEAREST] is False
async def test_setup_invalid_auth(opp: OpenPeerPower) -> None: """Test we handle invalid auth.""" result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "openpeerpower.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=FritzConnectionException, ): result = await opp.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == RESULT_TYPE_FORM assert result["errors"] == {"base": RESULT_INVALID_AUTH}
async def test_setup_insufficient_permissions(hass: HomeAssistant) -> None: """Test we handle insufficient permissions.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=FritzSecurityError, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == FlowResultType.ABORT assert result["reason"] == ConnectResult.INSUFFICIENT_PERMISSIONS
async def test_setup_invalid_auth(hass: HomeAssistant) -> None: """Test we handle invalid auth.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=FritzConnectionException, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == FlowResultType.FORM assert result["errors"] == {"base": ConnectResult.INVALID_AUTH}
async def test_setup_cannot_connect(opp: OpenPeerPower) -> None: """Test we handle cannot connect.""" result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "openpeerpower.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=RequestsConnectionError, ): result = await opp.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == RESULT_NO_DEVIES_FOUND
async def test_setup_cannot_connect(hass: HomeAssistant) -> None: """Test we handle cannot connect.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=RequestsConnectionError, ): result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == FlowResultType.ABORT assert result["reason"] == ConnectResult.NO_DEVIES_FOUND
async def test_setup_insufficient_permissions(opp: OpenPeerPower) -> None: """Test we handle insufficient permissions.""" result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) with patch( "openpeerpower.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", side_effect=FritzSecurityError, ): result = await opp.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA) assert result["type"] == RESULT_TYPE_ABORT assert result["reason"] == RESULT_INSUFFICIENT_PERMISSIONS
async def test_create_entry_with_nearest_method(opp, aioclient_mock): """Test that the user step works with nearest method.""" aioclient_mock.get(API_POINT_URL, text=load_fixture("airly_no_station.json")) aioclient_mock.get(API_NEAREST_URL, text=load_fixture("airly_valid_station.json")) with patch("openpeerpower.components.airly.async_setup_entry", return_value=True): result = await opp.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, data=CONFIG ) assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["title"] == CONFIG[CONF_NAME] assert result["data"][CONF_LATITUDE] == CONFIG[CONF_LATITUDE] assert result["data"][CONF_LONGITUDE] == CONFIG[CONF_LONGITUDE] assert result["data"][CONF_API_KEY] == CONFIG[CONF_API_KEY] assert result["data"][CONF_USE_NEAREST] is True
async def test_setup_one_phonebook(hass: HomeAssistant) -> None: """Test setting up manually.""" result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_USER}, ) assert result["type"] == RESULT_TYPE_FORM assert result["step_id"] == "user" with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_ids", new_callable=PropertyMock, return_value=[0], ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_info", return_value=MOCK_PHONEBOOK_INFO_1, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.modelname", return_value=MOCK_PHONEBOOK_NAME_1, ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.call_action", return_value=MOCK_DEVICE_INFO, ), patch( "homeassistant.components.fritzbox_callmonitor.async_setup_entry", return_value=True, ) as mock_setup_entry: result = await hass.config_entries.flow.async_configure( result["flow_id"], user_input=MOCK_USER_DATA ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == MOCK_PHONEBOOK_NAME_1 assert result["data"] == MOCK_CONFIG_ENTRY assert len(mock_setup_entry.mock_calls) == 1
async def test_yaml_import(hass: HomeAssistant) -> None: """Test configuration.yaml import.""" with patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_ids", new_callable=PropertyMock, return_value=[0], ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.phonebook_info", return_value=MOCK_PHONEBOOK_INFO_1, ), patch( "homeassistant.components.fritzbox_callmonitor.base.FritzPhonebook.modelname", return_value=MOCK_PHONEBOOK_NAME_1, ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.__init__", return_value=None, ), patch( "homeassistant.components.fritzbox_callmonitor.config_flow.FritzConnection.call_action", return_value=MOCK_DEVICE_INFO, ), patch( "homeassistant.components.fritzbox_callmonitor.async_setup_entry", return_value=True, ) as mock_setup_entry: result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": SOURCE_IMPORT}, data=MOCK_YAML_CONFIG, ) assert result["type"] == RESULT_TYPE_CREATE_ENTRY assert result["title"] == MOCK_NAME assert result["data"] == MOCK_CONFIG_ENTRY assert len(mock_setup_entry.mock_calls) == 1