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 import voluptuous as vol
from homeassistant.components import zeroconf
from homeassistant.config_entries import ConfigFlow from homeassistant.config_entries import ConfigFlow
from homeassistant.const import ( from homeassistant.const import (
CONF_HOST, CONF_HOST,
@ -99,25 +100,27 @@ class IPPFlowHandler(ConfigFlow, domain=DOMAIN):
return self.async_create_entry(title=user_input[CONF_HOST], data=user_input) 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.""" """Handle zeroconf discovery."""
port = discovery_info[CONF_PORT] port = discovery_info[zeroconf.ATTR_PORT]
zctype = discovery_info["type"] zctype = discovery_info[zeroconf.ATTR_TYPE]
name = discovery_info[CONF_NAME].replace(f".{zctype}", "") name = discovery_info[zeroconf.ATTR_NAME].replace(f".{zctype}", "")
tls = zctype == "_ipps._tcp.local." 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.context.update({"title_placeholders": {"name": name}})
self.discovery_info.update( self.discovery_info.update(
{ {
CONF_HOST: discovery_info[CONF_HOST], CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
CONF_PORT: port, CONF_PORT: port,
CONF_SSL: tls, CONF_SSL: tls,
CONF_VERIFY_SSL: False, CONF_VERIFY_SSL: False,
CONF_BASE_PATH: f"/{base_path}", CONF_BASE_PATH: f"/{base_path}",
CONF_NAME: name, 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 import aiohttp
from pyipp import IPPConnectionUpgradeRequired, IPPError from pyipp import IPPConnectionUpgradeRequired, IPPError
from homeassistant.components import zeroconf
from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_UUID, DOMAIN from homeassistant.components.ipp.const import CONF_BASE_PATH, CONF_UUID, DOMAIN
from homeassistant.const import ( from homeassistant.const import CONF_HOST, CONF_PORT, CONF_SSL, CONF_VERIFY_SSL
CONF_HOST,
CONF_NAME,
CONF_PORT,
CONF_SSL,
CONF_TYPE,
CONF_VERIFY_SSL,
)
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry, get_fixture_path from tests.common import MockConfigEntry, get_fixture_path
@ -40,23 +34,23 @@ MOCK_USER_INPUT = {
CONF_BASE_PATH: BASE_PATH, CONF_BASE_PATH: BASE_PATH,
} }
MOCK_ZEROCONF_IPP_SERVICE_INFO = { MOCK_ZEROCONF_IPP_SERVICE_INFO = zeroconf.ZeroconfServiceInfo(
CONF_TYPE: IPP_ZEROCONF_SERVICE_TYPE, type=IPP_ZEROCONF_SERVICE_TYPE,
CONF_NAME: f"{ZEROCONF_NAME}.{IPP_ZEROCONF_SERVICE_TYPE}", name=f"{ZEROCONF_NAME}.{IPP_ZEROCONF_SERVICE_TYPE}",
CONF_HOST: ZEROCONF_HOST, host=ZEROCONF_HOST,
ATTR_HOSTNAME: ZEROCONF_HOSTNAME, hostname=ZEROCONF_HOSTNAME,
CONF_PORT: ZEROCONF_PORT, port=ZEROCONF_PORT,
ATTR_PROPERTIES: {"rp": ZEROCONF_RP}, properties={"rp": ZEROCONF_RP},
} )
MOCK_ZEROCONF_IPPS_SERVICE_INFO = { MOCK_ZEROCONF_IPPS_SERVICE_INFO = zeroconf.ZeroconfServiceInfo(
CONF_TYPE: IPPS_ZEROCONF_SERVICE_TYPE, type=IPPS_ZEROCONF_SERVICE_TYPE,
CONF_NAME: f"{ZEROCONF_NAME}.{IPPS_ZEROCONF_SERVICE_TYPE}", name=f"{ZEROCONF_NAME}.{IPPS_ZEROCONF_SERVICE_TYPE}",
CONF_HOST: ZEROCONF_HOST, host=ZEROCONF_HOST,
ATTR_HOSTNAME: ZEROCONF_HOSTNAME, hostname=ZEROCONF_HOSTNAME,
CONF_PORT: ZEROCONF_PORT, port=ZEROCONF_PORT,
ATTR_PROPERTIES: {"rp": ZEROCONF_RP}, properties={"rp": ZEROCONF_RP},
} )
def load_fixture_binary(filename): def load_fixture_binary(filename):

View File

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