Use ZeroconfServiceInfo in ipp (#59983)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-19 17:16:34 +01:00 committed by GitHub
parent e3ee19d0c4
commit 386520b883
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 33 deletions

View File

@ -15,6 +15,7 @@ from pyipp import (
)
import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigFlow
from homeassistant.const import (
CONF_HOST,
@ -99,25 +100,27 @@ class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_create_entry(title=user_input[CONF_HOST], data=user_input)
async def async_step_zeroconf(self, discovery_info: dict[str, Any]) -> FlowResult:
async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult:
"""Handle zeroconf discovery."""
port = discovery_info[CONF_PORT]
zctype = discovery_info["type"]
name = discovery_info[CONF_NAME].replace(f".{zctype}", "")
port = discovery_info[zeroconf.ATTR_PORT]
zctype = discovery_info[zeroconf.ATTR_TYPE]
name = discovery_info[zeroconf.ATTR_NAME].replace(f".{zctype}", "")
tls = zctype == "_ipps._tcp.local."
base_path = discovery_info["properties"].get("rp", "ipp/print")
base_path = discovery_info[zeroconf.ATTR_PROPERTIES].get("rp", "ipp/print")
self.context.update({"title_placeholders": {"name": name}})
self.discovery_info.update(
{
CONF_HOST: discovery_info[CONF_HOST],
CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
CONF_PORT: port,
CONF_SSL: tls,
CONF_VERIFY_SSL: False,
CONF_BASE_PATH: f"/{base_path}",
CONF_NAME: name,
CONF_UUID: discovery_info["properties"].get("UUID"),
CONF_UUID: discovery_info[zeroconf.ATTR_PROPERTIES].get("UUID"),
}
)

View File

@ -2,15 +2,9 @@
import aiohttp
from pyipp import IPPConnectionUpgradeRequired, IPPError
from homeassistant.components import zeroconf
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_UUID, DOMAIN
from homeassistant.const import (
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_SSL,
CONF_TYPE,
CONF_VERIFY_SSL,
)
from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL
from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, get_fixture_path
@ -40,23 +34,23 @@ MOCK_USER_INPUT = {
CONF_BASE_PATH: BASE_PATH,
}
MOCK_ZEROCONF_IPP_SERVICE_INFO = {
CONF_TYPE: IPP_ZEROCONF_SERVICE_TYPE,
CONF_NAME: f"{ZEROCONF_NAME}.{IPP_ZEROCONF_SERVICE_TYPE}",
CONF_HOST: ZEROCONF_HOST,
ATTR_HOSTNAME: ZEROCONF_HOSTNAME,
CONF_PORT: ZEROCONF_PORT,
ATTR_PROPERTIES: {"rp": ZEROCONF_RP},
}
MOCK_ZEROCONF_IPP_SERVICE_INFO = zeroconf.ZeroconfServiceInfo(
type=IPP_ZEROCONF_SERVICE_TYPE,
name=f"{ZEROCONF_NAME}.{IPP_ZEROCONF_SERVICE_TYPE}",
host=ZEROCONF_HOST,
hostname=ZEROCONF_HOSTNAME,
port=ZEROCONF_PORT,
properties={"rp": ZEROCONF_RP},
)
MOCK_ZEROCONF_IPPS_SERVICE_INFO = {
CONF_TYPE: IPPS_ZEROCONF_SERVICE_TYPE,
CONF_NAME: f"{ZEROCONF_NAME}.{IPPS_ZEROCONF_SERVICE_TYPE}",
CONF_HOST: ZEROCONF_HOST,
ATTR_HOSTNAME: ZEROCONF_HOSTNAME,
CONF_PORT: ZEROCONF_PORT,
ATTR_PROPERTIES: {"rp": ZEROCONF_RP},
}
MOCK_ZEROCONF_IPPS_SERVICE_INFO = zeroconf.ZeroconfServiceInfo(
type=IPPS_ZEROCONF_SERVICE_TYPE,
name=f"{ZEROCONF_NAME}.{IPPS_ZEROCONF_SERVICE_TYPE}",
host=ZEROCONF_HOST,
hostname=ZEROCONF_HOSTNAME,
port=ZEROCONF_PORT,
properties={"rp": ZEROCONF_RP},
)
def load_fixture_binary(filename):

View File

@ -1,6 +1,7 @@
"""Tests for the IPP config flow."""
from unittest.mock import patch
from homeassistant.components import zeroconf
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_UUID, DOMAIN
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_SSL
@ -281,7 +282,7 @@ async def test_zeroconf_with_uuid_device_exists_abort(
discovery_info = {
**MOCK_ZEROCONF_IPP_SERVICE_INFO,
"properties": {
**MOCK_ZEROCONF_IPP_SERVICE_INFO["properties"],
**MOCK_ZEROCONF_IPP_SERVICE_INFO[zeroconf.ATTR_PROPERTIES],
"UUID": "cfe92100-67c4-11d4-a45f-f8d027761251",
},
}
@ -303,7 +304,10 @@ async def test_zeroconf_empty_unique_id(
discovery_info = {
**MOCK_ZEROCONF_IPP_SERVICE_INFO,
"properties": {**MOCK_ZEROCONF_IPP_SERVICE_INFO["properties"], "UUID": ""},
"properties": {
**MOCK_ZEROCONF_IPP_SERVICE_INFO[zeroconf.ATTR_PROPERTIES],
"UUID": "",
},
}
result = await hass.config_entries.flow.async_init(
DOMAIN,