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