Use ZeroconfServiceInfo in plugwise (#60050)
This commit is contained in:
parent
29761e6eef
commit
10d0870198
2 changed files with 36 additions and 28 deletions
|
@ -1,4 +1,6 @@
|
|||
"""Config flow for Plugwise integration."""
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
|
||||
from plugwise.exceptions import InvalidAuthentication, PlugwiseException
|
||||
|
@ -6,6 +8,7 @@ from plugwise.smile import Smile
|
|||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core, exceptions
|
||||
from homeassistant.components import zeroconf
|
||||
from homeassistant.const import (
|
||||
CONF_BASE,
|
||||
CONF_HOST,
|
||||
|
@ -16,8 +19,8 @@ from homeassistant.const import (
|
|||
CONF_USERNAME,
|
||||
)
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||
|
||||
from .const import (
|
||||
API,
|
||||
|
@ -98,30 +101,34 @@ class PlugwiseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
|
||||
def __init__(self):
|
||||
"""Initialize the Plugwise config flow."""
|
||||
self.discovery_info = {}
|
||||
self.discovery_info: zeroconf.ZeroconfServiceInfo | None = None
|
||||
self._username: str = DEFAULT_USERNAME
|
||||
|
||||
async def async_step_zeroconf(self, discovery_info: DiscoveryInfoType):
|
||||
async def async_step_zeroconf(
|
||||
self, discovery_info: zeroconf.ZeroconfServiceInfo
|
||||
) -> FlowResult:
|
||||
"""Prepare configuration for a discovered Plugwise Smile."""
|
||||
self.discovery_info = discovery_info
|
||||
self.discovery_info[CONF_USERNAME] = DEFAULT_USERNAME
|
||||
_properties = self.discovery_info.get("properties")
|
||||
_properties = discovery_info[zeroconf.ATTR_PROPERTIES]
|
||||
|
||||
# unique_id is needed here, to be able to determine whether the discovered device is known, or not.
|
||||
unique_id = self.discovery_info.get("hostname").split(".")[0]
|
||||
unique_id = discovery_info[zeroconf.ATTR_HOSTNAME].split(".")[0]
|
||||
await self.async_set_unique_id(unique_id)
|
||||
self._abort_if_unique_id_configured({CONF_HOST: self.discovery_info[CONF_HOST]})
|
||||
self._abort_if_unique_id_configured(
|
||||
{CONF_HOST: discovery_info[zeroconf.ATTR_HOST]}
|
||||
)
|
||||
|
||||
if DEFAULT_USERNAME not in unique_id:
|
||||
self.discovery_info[CONF_USERNAME] = STRETCH_USERNAME
|
||||
self._username = STRETCH_USERNAME
|
||||
_product = _properties.get("product", None)
|
||||
_version = _properties.get("version", "n/a")
|
||||
_name = f"{ZEROCONF_MAP.get(_product, _product)} v{_version}"
|
||||
|
||||
self.context["title_placeholders"] = {
|
||||
CONF_HOST: self.discovery_info[CONF_HOST],
|
||||
CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
|
||||
CONF_NAME: _name,
|
||||
CONF_PORT: self.discovery_info[CONF_PORT],
|
||||
CONF_USERNAME: self.discovery_info[CONF_USERNAME],
|
||||
CONF_PORT: discovery_info[zeroconf.ATTR_PORT],
|
||||
CONF_USERNAME: self._username,
|
||||
}
|
||||
return await self.async_step_user_gateway()
|
||||
|
||||
|
@ -136,9 +143,9 @@ class PlugwiseConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|||
user_input.pop(FLOW_TYPE, None)
|
||||
|
||||
if self.discovery_info:
|
||||
user_input[CONF_HOST] = self.discovery_info[CONF_HOST]
|
||||
user_input[CONF_PORT] = self.discovery_info[CONF_PORT]
|
||||
user_input[CONF_USERNAME] = self.discovery_info[CONF_USERNAME]
|
||||
user_input[CONF_HOST] = self.discovery_info[zeroconf.ATTR_HOST]
|
||||
user_input[CONF_PORT] = self.discovery_info[zeroconf.ATTR_PORT]
|
||||
user_input[CONF_USERNAME] = self._username
|
||||
|
||||
try:
|
||||
api = await validate_gw_input(self.hass, user_input)
|
||||
|
|
|
@ -8,6 +8,7 @@ from plugwise.exceptions import (
|
|||
)
|
||||
import pytest
|
||||
|
||||
from homeassistant.components import zeroconf
|
||||
from homeassistant.components.plugwise.const import (
|
||||
API,
|
||||
DEFAULT_PORT,
|
||||
|
@ -39,28 +40,28 @@ TEST_PORT = 81
|
|||
TEST_USERNAME = "smile"
|
||||
TEST_USERNAME2 = "stretch"
|
||||
|
||||
TEST_DISCOVERY = {
|
||||
"host": TEST_HOST,
|
||||
"port": DEFAULT_PORT,
|
||||
"hostname": f"{TEST_HOSTNAME}.local.",
|
||||
"server": f"{TEST_HOSTNAME}.local.",
|
||||
"properties": {
|
||||
TEST_DISCOVERY = zeroconf.ZeroconfServiceInfo(
|
||||
host=TEST_HOST,
|
||||
port=DEFAULT_PORT,
|
||||
hostname=f"{TEST_HOSTNAME}.local.",
|
||||
server=f"{TEST_HOSTNAME}.local.",
|
||||
properties={
|
||||
"product": "smile",
|
||||
"version": "1.2.3",
|
||||
"hostname": f"{TEST_HOSTNAME}.local.",
|
||||
},
|
||||
}
|
||||
TEST_DISCOVERY2 = {
|
||||
"host": TEST_HOST,
|
||||
"port": DEFAULT_PORT,
|
||||
"hostname": f"{TEST_HOSTNAME2}.local.",
|
||||
"server": f"{TEST_HOSTNAME2}.local.",
|
||||
"properties": {
|
||||
)
|
||||
TEST_DISCOVERY2 = zeroconf.ZeroconfServiceInfo(
|
||||
host=TEST_HOST,
|
||||
port=DEFAULT_PORT,
|
||||
hostname=f"{TEST_HOSTNAME2}.local.",
|
||||
server=f"{TEST_HOSTNAME2}.local.",
|
||||
properties={
|
||||
"product": "stretch",
|
||||
"version": "1.2.3",
|
||||
"hostname": f"{TEST_HOSTNAME2}.local.",
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@pytest.fixture(name="mock_smile")
|
||||
|
|
Loading…
Add table
Reference in a new issue