From b9cbfbae585e29602b7513c7e8ba8378c85381db Mon Sep 17 00:00:00 2001 From: epenet <6771947+epenet@users.noreply.github.com> Date: Sun, 21 Nov 2021 22:49:03 +0100 Subject: [PATCH] Use ZeroconfServiceInfo in nut (#60047) --- homeassistant/components/nut/config_flow.py | 10 +++++++--- tests/components/nut/test_config_flow.py | 3 ++- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/homeassistant/components/nut/config_flow.py b/homeassistant/components/nut/config_flow.py index ae22526752d..9a8fc704886 100644 --- a/homeassistant/components/nut/config_flow.py +++ b/homeassistant/components/nut/config_flow.py @@ -4,6 +4,7 @@ import logging import voluptuous as vol from homeassistant import config_entries, core, exceptions +from homeassistant.components import zeroconf from homeassistant.const import ( CONF_ALIAS, CONF_BASE, @@ -14,6 +15,7 @@ from homeassistant.const import ( CONF_USERNAME, ) from homeassistant.core import callback +from homeassistant.data_entry_flow import FlowResult from . import PyNUTData from .const import DEFAULT_HOST, DEFAULT_PORT, DEFAULT_SCAN_INTERVAL, DOMAIN @@ -85,13 +87,15 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): self.ups_list = None self.title = None - async def async_step_zeroconf(self, discovery_info): + async def async_step_zeroconf( + self, discovery_info: zeroconf.ZeroconfServiceInfo + ) -> FlowResult: """Prepare configuration for a discovered nut device.""" self.discovery_info = discovery_info await self._async_handle_discovery_without_unique_id() self.context["title_placeholders"] = { - CONF_PORT: discovery_info.get(CONF_PORT, DEFAULT_PORT), - CONF_HOST: discovery_info[CONF_HOST], + CONF_PORT: discovery_info[zeroconf.ATTR_PORT] or DEFAULT_PORT, + CONF_HOST: discovery_info[zeroconf.ATTR_HOST], } return await self.async_step_user() diff --git a/tests/components/nut/test_config_flow.py b/tests/components/nut/test_config_flow.py index 8559ab50f98..892d74b4713 100644 --- a/tests/components/nut/test_config_flow.py +++ b/tests/components/nut/test_config_flow.py @@ -5,6 +5,7 @@ from unittest.mock import patch from pynut2.nut2 import PyNUTError from homeassistant import config_entries, data_entry_flow, setup +from homeassistant.components import zeroconf from homeassistant.components.nut.const import DOMAIN from homeassistant.const import ( CONF_ALIAS, @@ -34,7 +35,7 @@ async def test_form_zeroconf(hass): result = await hass.config_entries.flow.async_init( DOMAIN, context={"source": config_entries.SOURCE_ZEROCONF}, - data={CONF_HOST: "192.168.1.5", CONF_PORT: 1234}, + data=zeroconf.ZeroconfServiceInfo(host="192.168.1.5", port=1234), ) assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["step_id"] == "user" -- GitLab