Use ZeroconfServiceInfo in octoprint (#60049)

This commit is contained in:
epenet 2021-11-21 17:35:24 +01:00 committed by GitHub
parent c3e9c1a7e8
commit 36a67d060b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 20 deletions

View File

@ -6,6 +6,7 @@ import voluptuous as vol
from yarl import URL from yarl import URL
from homeassistant import config_entries, data_entry_flow, exceptions from homeassistant import config_entries, data_entry_flow, exceptions
from homeassistant.components import zeroconf
from homeassistant.const import ( from homeassistant.const import (
CONF_API_KEY, CONF_API_KEY,
CONF_HOST, CONF_HOST,
@ -138,20 +139,22 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Handle import.""" """Handle import."""
return await self.async_step_user(user_input) return await self.async_step_user(user_input)
async def async_step_zeroconf(self, discovery_info): async def async_step_zeroconf(
self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> data_entry_flow.FlowResult:
"""Handle discovery flow.""" """Handle discovery flow."""
uuid = discovery_info["properties"]["uuid"] uuid = discovery_info[zeroconf.ATTR_PROPERTIES]["uuid"]
await self.async_set_unique_id(uuid) await self.async_set_unique_id(uuid)
self._abort_if_unique_id_configured() self._abort_if_unique_id_configured()
self.context["title_placeholders"] = { self.context["title_placeholders"] = {
CONF_HOST: discovery_info[CONF_HOST], CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
} }
self.discovery_schema = _schema_with_defaults( self.discovery_schema = _schema_with_defaults(
host=discovery_info[CONF_HOST], host=discovery_info[zeroconf.ATTR_HOST],
port=discovery_info[CONF_PORT], port=discovery_info[zeroconf.ATTR_PORT],
path=discovery_info["properties"][CONF_PATH], path=discovery_info[zeroconf.ATTR_PROPERTIES][CONF_PATH],
) )
return await self.async_step_user() return await self.async_step_user()

View File

@ -4,6 +4,7 @@ from unittest.mock import patch
from pyoctoprintapi import ApiError, DiscoverySettings from pyoctoprintapi import ApiError, DiscoverySettings
from homeassistant import config_entries, data_entry_flow from homeassistant import config_entries, data_entry_flow
from homeassistant.components import zeroconf
from homeassistant.components.octoprint.const import DOMAIN from homeassistant.components.octoprint.const import DOMAIN
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
@ -169,13 +170,12 @@ async def test_show_zerconf_form(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
"host": "192.168.1.123", host="192.168.1.123",
"port": 80, port=80,
"hostname": "example.local.", hostname="example.local.",
"uuid": "83747482", properties={"uuid": "83747482", "path": "/foo/"},
"properties": {"uuid": "83747482", "path": "/foo/"}, ),
},
) )
assert result["type"] == "form" assert result["type"] == "form"
assert not result["errors"] assert not result["errors"]
@ -485,13 +485,12 @@ async def test_duplicate_zerconf_ignored(hass: HomeAssistant) -> None:
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
"host": "192.168.1.123", host="192.168.1.123",
"port": 80, port=80,
"hostname": "example.local.", hostname="example.local.",
"uuid": "83747482", properties={"uuid": "83747482", "path": "/foo/"},
"properties": {"uuid": "83747482", "path": "/foo/"}, ),
},
) )
assert result["type"] == "abort" assert result["type"] == "abort"
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"