Example #1
0
async def test_import_duplicate_yaml(hass: HomeAssistant) -> None:
    """Test that the yaml import works."""
    MockConfigEntry(
        domain=DOMAIN,
        data={"host": "192.168.1.123"},
        source=config_entries.SOURCE_IMPORT,
        unique_id="uuid",
    ).add_to_hass(hass)

    with patch(
        "pyoctoprintapi.OctoprintClient.get_discovery_info",
        return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch(
        "pyoctoprintapi.OctoprintClient.request_app_key", return_value="test-key"
    ) as request_app_key:
        result = await hass.config_entries.flow.async_init(
            DOMAIN,
            context={"source": config_entries.SOURCE_IMPORT},
            data={
                "host": "1.1.1.1",
                "api_key": "test-key",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
            },
        )
        await hass.async_block_till_done()
        assert len(request_app_key.mock_calls) == 0

    assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
    assert result["reason"] == "already_configured"
Example #2
0
async def test_import_yaml(hass: HomeAssistant) -> None:
    """Test that the yaml import works."""
    with patch(
        "pyoctoprintapi.OctoprintClient.get_server_info",
        return_value=True,
    ), patch(
        "pyoctoprintapi.OctoprintClient.get_discovery_info",
        return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch(
        "pyoctoprintapi.OctoprintClient.request_app_key", return_value="test-key"
    ), patch(
        "homeassistant.components.octoprint.async_setup", return_value=True
    ), patch(
        "homeassistant.components.octoprint.async_setup_entry",
        return_value=True,
    ):
        result = await hass.config_entries.flow.async_init(
            DOMAIN,
            context={"source": config_entries.SOURCE_IMPORT},
            data={
                "host": "1.1.1.1",
                "api_key": "test-key",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
            },
        )
        await hass.async_block_till_done()
    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
    assert "errors" not in result
Example #3
0
async def test_show_ssdp_form(hass: HomeAssistant) -> None:
    """Test that the zeroconf confirmation form is served."""

    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": config_entries.SOURCE_SSDP},
        data={
            "presentationURL": "http://192.168.1.123:80/discovery/device.xml",
            "port": 80,
            "UDN": "uuid:83747482",
        },
    )
    assert result["type"] == "form"
    assert not result["errors"]

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            "username": "******",
        },
    )
    assert result["type"] == "progress"

    with patch("pyoctoprintapi.OctoprintClient.request_app_key",
               return_value="test-key"):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"], )
        await hass.async_block_till_done()

    assert result["type"] == "progress_done"

    with patch(
            "pyoctoprintapi.OctoprintClient.get_server_info",
            return_value=True,
    ), patch(
            "pyoctoprintapi.OctoprintClient.get_discovery_info",
            return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch("homeassistant.components.octoprint.async_setup",
             return_value=True), patch(
                 "homeassistant.components.octoprint.async_setup_entry",
                 return_value=True,
             ):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "username": "******",
                "host": "1.1.1.1",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
                "api_key": "test-key",
            },
        )
        await hass.async_block_till_done()

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
Example #4
0
async def test_form(hass):
    """Test we get the form."""
    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )
    assert result["type"] == "form"
    assert not result["errors"]

    with patch(
        "pyoctoprintapi.OctoprintClient.request_app_key", return_value="test-key"
    ):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "username": "******",
                "host": "1.1.1.1",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
            },
        )
        await hass.async_block_till_done()
    assert result["type"] == "progress"

    with patch(
        "pyoctoprintapi.OctoprintClient.get_server_info",
        return_value=True,
    ), patch(
        "pyoctoprintapi.OctoprintClient.get_discovery_info",
        return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch(
        "homeassistant.components.octoprint.async_setup", return_value=True
    ) as mock_setup, patch(
        "homeassistant.components.octoprint.async_setup_entry",
        return_value=True,
    ) as mock_setup_entry:
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
        )
        await hass.async_block_till_done()

    assert result2["type"] == "create_entry"
    assert result2["title"] == "1.1.1.1"
    assert result2["data"] == {
        "username": "******",
        "host": "1.1.1.1",
        "api_key": "test-key",
        "name": "Printer",
        "port": 81,
        "ssl": True,
        "path": "/",
        "verify_ssl": True,
    }
    assert len(mock_setup.mock_calls) == 1
    assert len(mock_setup_entry.mock_calls) == 1
Example #5
0
async def test_user_duplicate_entry(hass):
    """Test that duplicate entries abort."""
    MockConfigEntry(
        domain=DOMAIN,
        data={"host": "192.168.1.123"},
        source=config_entries.SOURCE_IMPORT,
        unique_id="uuid",
    ).add_to_hass(hass)

    result = await hass.config_entries.flow.async_init(
        DOMAIN, context={"source": config_entries.SOURCE_USER}
    )
    assert result["type"] == "form"
    assert not result["errors"]

    with patch(
        "pyoctoprintapi.OctoprintClient.request_app_key", return_value="test-key"
    ):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "username": "******",
                "host": "1.1.1.1",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
            },
        )
        await hass.async_block_till_done()
    assert result["type"] == "progress"

    with patch(
        "pyoctoprintapi.OctoprintClient.get_server_info",
        return_value=True,
    ), patch(
        "pyoctoprintapi.OctoprintClient.get_discovery_info",
        return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch(
        "homeassistant.components.octoprint.async_setup", return_value=True
    ) as mock_setup, patch(
        "homeassistant.components.octoprint.async_setup_entry",
        return_value=True,
    ) as mock_setup_entry:
        result2 = await hass.config_entries.flow.async_configure(
            result["flow_id"],
        )
        await hass.async_block_till_done()

    assert result2["type"] == "abort"
    assert result2["reason"] == "already_configured"
    assert len(mock_setup.mock_calls) == 0
    assert len(mock_setup_entry.mock_calls) == 0
async def init_integration(
    hass,
    platform,
    printer: dict[str, Any] | UndefinedType | None = UNDEFINED,
    job: dict[str, Any] | None = None,
):
    """Set up the octoprint integration in Home Assistant."""
    printer_info: OctoprintPrinterInfo | None = None
    if printer is UNDEFINED:
        printer = DEFAULT_PRINTER
    if printer is not None:
        printer_info = OctoprintPrinterInfo(printer)
    if job is None:
        job = DEFAULT_JOB
    with patch("homeassistant.components.octoprint.PLATFORMS",
               [platform]), patch(
                   "pyoctoprintapi.OctoprintClient.get_server_info",
                   return_value={}), patch(
                       "pyoctoprintapi.OctoprintClient.get_printer_info",
                       return_value=printer_info,
                   ), patch(
                       "pyoctoprintapi.OctoprintClient.get_job_info",
                       return_value=OctoprintJobInfo(job),
                   ), patch(
                       "pyoctoprintapi.OctoprintClient.get_tracking_info",
                       return_value=TrackingSetting({"unique_id": "uuid"}),
                   ), patch(
                       "pyoctoprintapi.OctoprintClient.get_discovery_info",
                       return_value=DiscoverySettings({"upnpUuid": "uuid"}),
                   ):
        config_entry = MockConfigEntry(
            domain=DOMAIN,
            entry_id="uuid",
            unique_id="uuid",
            data={
                "host": "1.1.1.1",
                "api_key": "test-key",
                "name": "OctoPrint",
                "port": 81,
                "ssl": True,
                "path": "/",
            },
            title="OctoPrint",
        )
        config_entry.add_to_hass(hass)

        await hass.config_entries.async_setup(config_entry.entry_id)
        await hass.async_block_till_done()

    assert config_entry.state == ConfigEntryState.LOADED
Example #7
0
async def test_show_zerconf_form(hass: HomeAssistant) -> None:
    """Test that the zeroconf confirmation form is served."""

    result = await hass.config_entries.flow.async_init(
        DOMAIN,
        context={"source": config_entries.SOURCE_ZEROCONF},
        data=zeroconf.ZeroconfServiceInfo(
            host="192.168.1.123",
            hostname="example.local.",
            name="mock_name",
            port=80,
            properties={"uuid": "83747482", "path": "/foo/"},
            type="mock_type",
        ),
    )
    assert result["type"] == "form"
    assert not result["errors"]

    result = await hass.config_entries.flow.async_configure(
        result["flow_id"],
        {
            "username": "******",
        },
    )
    assert result["type"] == "progress"

    with patch(
        "pyoctoprintapi.OctoprintClient.request_app_key", return_value="test-key"
    ):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
        )
        await hass.async_block_till_done()

    assert result["type"] == "progress_done"

    with patch(
        "pyoctoprintapi.OctoprintClient.get_server_info",
        return_value=True,
    ), patch(
        "pyoctoprintapi.OctoprintClient.get_discovery_info",
        return_value=DiscoverySettings({"upnpUuid": "uuid"}),
    ), patch(
        "homeassistant.components.octoprint.async_setup", return_value=True
    ), patch(
        "homeassistant.components.octoprint.async_setup_entry",
        return_value=True,
    ):
        result = await hass.config_entries.flow.async_configure(
            result["flow_id"],
            {
                "username": "******",
                "host": "1.1.1.1",
                "name": "Printer",
                "port": 81,
                "ssl": True,
                "path": "/",
                "api_key": "test-key",
            },
        )
        await hass.async_block_till_done()

    assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY