Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make IPP protocol version configurable #128997

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion homeassistant/components/ipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
)
from homeassistant.core import HomeAssistant

from .const import CONF_BASE_PATH
from .const import CONF_BASE_PATH, CONF_PROTO_LEGACY
from .coordinator import IPPDataUpdateCoordinator

PLATFORMS = [Platform.SENSOR]
Expand All @@ -31,6 +31,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: IPPConfigEntry) -> bool:
host=entry.data[CONF_HOST],
port=entry.data[CONF_PORT],
base_path=entry.data[CONF_BASE_PATH],
proto_legacy=entry.data[CONF_PROTO_LEGACY],
tls=entry.data[CONF_SSL],
verify_ssl=entry.data[CONF_VERIFY_SSL],
device_id=device_id,
Expand Down
16 changes: 13 additions & 3 deletions homeassistant/components/ipp/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,14 @@
from homeassistant.core import HomeAssistant
from homeassistant.helpers.aiohttp_client import async_get_clientsession

from .const import CONF_BASE_PATH, CONF_SERIAL, DOMAIN
from .const import (
CONF_BASE_PATH,
CONF_PROTO_LEGACY,
CONF_SERIAL,
DOMAIN,
IPP_PROTO_VERSION_DEFAULT,
IPP_PROTO_VERSION_LEGACY,
)

_LOGGER = logging.getLogger(__name__)

Expand All @@ -47,6 +54,9 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
tls=data[CONF_SSL],
verify_ssl=data[CONF_VERIFY_SSL],
session=session,
ipp_version=IPP_PROTO_VERSION_LEGACY
if data[CONF_PROTO_LEGACY]
else IPP_PROTO_VERSION_DEFAULT,
)

printer = await ipp.printer()
Expand All @@ -57,8 +67,6 @@ async def validate_input(hass: HomeAssistant, data: dict) -> dict[str, Any]:
class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle an IPP config flow."""

VERSION = 1

def __init__(self) -> None:
"""Set up the instance."""
self.discovery_info: dict[str, Any] = {}
Expand Down Expand Up @@ -127,6 +135,7 @@ async def async_step_zeroconf(
CONF_BASE_PATH: f"/{base_path}",
CONF_NAME: name,
CONF_UUID: unique_id,
CONF_PROTO_LEGACY: False,
}
)

Expand Down Expand Up @@ -215,6 +224,7 @@ def _show_setup_form(self, errors: dict | None = None) -> ConfigFlowResult:
vol.Required(CONF_BASE_PATH, default="/ipp/print"): str,
vol.Required(CONF_SSL, default=False): bool,
vol.Required(CONF_VERIFY_SSL, default=False): bool,
vol.Required(CONF_PROTO_LEGACY, default=False): bool,
}
),
errors=errors or {},
Expand Down
4 changes: 4 additions & 0 deletions homeassistant/components/ipp/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,9 @@

# Config Keys
CONF_BASE_PATH = "base_path"
CONF_PROTO_LEGACY = "proto_legacy"
CONF_SERIAL = "serial"
CONF_TLS = "tls"

IPP_PROTO_VERSION_LEGACY = (1, 1)
IPP_PROTO_VERSION_DEFAULT = (2, 0)
6 changes: 5 additions & 1 deletion homeassistant/components/ipp/coordinator.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed

from .const import DOMAIN
from .const import DOMAIN, IPP_PROTO_VERSION_DEFAULT, IPP_PROTO_VERSION_LEGACY

SCAN_INTERVAL = timedelta(seconds=60)

Expand All @@ -30,6 +30,7 @@ def __init__(
base_path: str,
tls: bool,
verify_ssl: bool,
proto_legacy: bool,
device_id: str,
) -> None:
"""Initialize global IPP data updater."""
Expand All @@ -41,6 +42,9 @@ def __init__(
tls=tls,
verify_ssl=verify_ssl,
session=async_get_clientsession(hass, verify_ssl),
ipp_version=IPP_PROTO_VERSION_LEGACY
if proto_legacy
else IPP_PROTO_VERSION_DEFAULT,
)

super().__init__(
Expand Down
3 changes: 2 additions & 1 deletion homeassistant/components/ipp/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"port": "[%key:common::config_flow::data::port%]",
"base_path": "Relative path to the printer",
"ssl": "[%key:common::config_flow::data::ssl%]",
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]"
"verify_ssl": "[%key:common::config_flow::data::verify_ssl%]",
"proto_legacy": "Use IPP v1.1 for compatibility with older printers"
}
},
"zeroconf_confirm": {
Expand Down
12 changes: 11 additions & 1 deletion tests/components/ipp/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from ipaddress import ip_address

from homeassistant.components import zeroconf
from homeassistant.components.ipp.const import CONF_BASE_PATH
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_PROTO_LEGACY
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL

ATTR_HOSTNAME = "hostname"
Expand All @@ -28,6 +28,16 @@
CONF_SSL: False,
CONF_VERIFY_SSL: False,
CONF_BASE_PATH: BASE_PATH,
CONF_PROTO_LEGACY: False,
}

MOCK_USER_INPUT_PROTO_LEGACY = {
CONF_HOST: "192.168.1.32",
CONF_PORT: PORT,
CONF_SSL: False,
CONF_VERIFY_SSL: False,
CONF_BASE_PATH: "/ipp",
CONF_PROTO_LEGACY: True,
}

MOCK_ZEROCONF_IPP_SERVICE_INFO = zeroconf.ZeroconfServiceInfo(
Expand Down
3 changes: 2 additions & 1 deletion tests/components/ipp/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from pyipp import Printer
import pytest

from homeassistant.components.ipp.const import CONF_BASE_PATH, DOMAIN
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_PROTO_LEGACY, DOMAIN
from homeassistant.const import (
CONF_HOST,
CONF_PORT,
Expand All @@ -33,6 +33,7 @@ def mock_config_entry() -> MockConfigEntry:
CONF_VERIFY_SSL: True,
CONF_BASE_PATH: "/ipp/print",
CONF_UUID: "cfe92100-67c4-11d4-a45f-f8d027761251",
CONF_PROTO_LEGACY: False,
},
unique_id="cfe92100-67c4-11d4-a45f-f8d027761251",
)
Expand Down
18 changes: 18 additions & 0 deletions tests/components/ipp/fixtures/printer_proto_legacy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"printer-state": "idle",
"printer-name": "Test Legacy Printer",
"printer-location": null,
"printer-make-and-model": "Test C822 Series",
"printer-uri-supported": [
"http://192.168.1.32/ipp",
"http://192.168.1.32:631/ipp",
"http://192.168.1.32/ipp/lp",
"http://192.168.1.32:631/ipp/lp",
"ipp://192.168.1.32/ipp",
"ipp://192.168.1.32:631/ipp",
"ipp://192.168.1.32/ipp/lp",
"ipp://192.168.1.32:631/ipp/lp"
],
"printer-info": "PCL,XPS,IBMPPR,EPSONFX",
"printer-more-info": null
}
1 change: 1 addition & 0 deletions tests/components/ipp/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
'base_path': '/ipp/print',
'host': '192.168.1.31',
'port': 631,
'proto_legacy': False,
'ssl': False,
'uuid': 'cfe92100-67c4-11d4-a45f-f8d027761251',
'verify_ssl': True,
Expand Down
29 changes: 28 additions & 1 deletion tests/components/ipp/test_config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@
)
import pytest

from homeassistant.components.ipp.const import CONF_BASE_PATH, DOMAIN
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_PROTO_LEGACY, DOMAIN
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SSL, CONF_UUID
from homeassistant.core import HomeAssistant
from homeassistant.data_entry_flow import FlowResultType

from . import (
MOCK_USER_INPUT,
MOCK_USER_INPUT_PROTO_LEGACY,
MOCK_ZEROCONF_IPP_SERVICE_INFO,
MOCK_ZEROCONF_IPPS_SERVICE_INFO,
)
Expand Down Expand Up @@ -132,6 +133,32 @@ async def test_user_connection_upgrade_required(
assert result["errors"] == {"base": "connection_upgrade"}


async def test_user_legacy_printer(hass: HomeAssistant) -> None:
"""Test that a legacy printer can be added."""
fixture = await hass.async_add_executor_job(
load_fixture, "ipp/printer_proto_legacy.json"
)
mock_legacy_printer = Printer.from_dict(json.loads(fixture))
user_input = MOCK_USER_INPUT_PROTO_LEGACY.copy()
with patch(
"homeassistant.components.ipp.config_flow.IPP", autospec=True
) as ipp_mock:
client = ipp_mock.return_value
client.printer.return_value = mock_legacy_printer
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_USER},
data=user_input,
)

assert result["type"] is FlowResultType.CREATE_ENTRY
assert result["title"] == "192.168.1.32"

assert result["data"]
assert result["data"][CONF_HOST] == "192.168.1.32"
assert result["data"][CONF_PROTO_LEGACY] is True


async def test_zeroconf_connection_upgrade_required(
hass: HomeAssistant,
mock_ipp_config_flow: MagicMock,
Expand Down