Use ZeroconfServiceInfo in nut (#60047)

pull/60119/head
epenet 2021-11-21 22:49:03 +01:00 committed by GitHub
parent 2675c6d408
commit b9cbfbae58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 9 additions and 4 deletions

View File

@ -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()

View File

@ -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"