mirror of
https://github.com/home-assistant/core.git
synced 2025-07-20 11:47:06 +00:00
Switch to using IP Addresses for connecting to smlight devices (#137204)
This commit is contained in:
parent
0f36759a38
commit
0f641fcb74
@ -14,6 +14,7 @@ from homeassistant.config_entries import SOURCE_USER, ConfigFlow, ConfigFlowResu
|
|||||||
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||||
from homeassistant.helpers.device_registry import format_mac
|
from homeassistant.helpers.device_registry import format_mac
|
||||||
|
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
|
||||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||||
|
|
||||||
from .const import DOMAIN
|
from .const import DOMAIN
|
||||||
@ -35,7 +36,8 @@ STEP_AUTH_DATA_SCHEMA = vol.Schema(
|
|||||||
class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||||
"""Handle a config flow for SMLIGHT Zigbee."""
|
"""Handle a config flow for SMLIGHT Zigbee."""
|
||||||
|
|
||||||
host: str
|
_host: str
|
||||||
|
_device_name: str
|
||||||
client: Api2
|
client: Api2
|
||||||
|
|
||||||
async def async_step_user(
|
async def async_step_user(
|
||||||
@ -45,11 +47,13 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
self.host = user_input[CONF_HOST]
|
self._host = user_input[CONF_HOST]
|
||||||
self.client = Api2(self.host, session=async_get_clientsession(self.hass))
|
self.client = Api2(self._host, session=async_get_clientsession(self.hass))
|
||||||
|
|
||||||
try:
|
try:
|
||||||
info = await self.client.get_info()
|
info = await self.client.get_info()
|
||||||
|
self._host = str(info.device_ip)
|
||||||
|
self._device_name = str(info.hostname)
|
||||||
|
|
||||||
if info.model not in Devices:
|
if info.model not in Devices:
|
||||||
return self.async_abort(reason="unsupported_device")
|
return self.async_abort(reason="unsupported_device")
|
||||||
@ -93,15 +97,14 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
self, discovery_info: ZeroconfServiceInfo
|
self, discovery_info: ZeroconfServiceInfo
|
||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle a discovered Lan coordinator."""
|
"""Handle a discovered Lan coordinator."""
|
||||||
local_name = discovery_info.hostname[:-1]
|
mac: str | None = discovery_info.properties.get("mac")
|
||||||
node_name = local_name.removesuffix(".local")
|
self._device_name = discovery_info.hostname.removesuffix(".local.")
|
||||||
|
self._host = discovery_info.host
|
||||||
|
|
||||||
self.host = local_name
|
self.context["title_placeholders"] = {CONF_NAME: self._device_name}
|
||||||
self.context["title_placeholders"] = {CONF_NAME: node_name}
|
self.client = Api2(self._host, session=async_get_clientsession(self.hass))
|
||||||
self.client = Api2(self.host, session=async_get_clientsession(self.hass))
|
|
||||||
|
|
||||||
mac = discovery_info.properties.get("mac")
|
# fallback for legacy firmware older than v2.3.x
|
||||||
# fallback for legacy firmware
|
|
||||||
if mac is None:
|
if mac is None:
|
||||||
try:
|
try:
|
||||||
info = await self.client.get_info()
|
info = await self.client.get_info()
|
||||||
@ -111,7 +114,7 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
mac = info.MAC
|
mac = info.MAC
|
||||||
|
|
||||||
await self.async_set_unique_id(format_mac(mac))
|
await self.async_set_unique_id(format_mac(mac))
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured(updates={CONF_HOST: self._host})
|
||||||
|
|
||||||
return await self.async_step_confirm_discovery()
|
return await self.async_step_confirm_discovery()
|
||||||
|
|
||||||
@ -122,7 +125,6 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors: dict[str, str] = {}
|
errors: dict[str, str] = {}
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
user_input[CONF_HOST] = self.host
|
|
||||||
try:
|
try:
|
||||||
info = await self.client.get_info()
|
info = await self.client.get_info()
|
||||||
|
|
||||||
@ -142,7 +144,7 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
return self.async_show_form(
|
return self.async_show_form(
|
||||||
step_id="confirm_discovery",
|
step_id="confirm_discovery",
|
||||||
description_placeholders={"host": self.host},
|
description_placeholders={"host": self._device_name},
|
||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -151,8 +153,8 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
) -> ConfigFlowResult:
|
) -> ConfigFlowResult:
|
||||||
"""Handle reauth when API Authentication failed."""
|
"""Handle reauth when API Authentication failed."""
|
||||||
|
|
||||||
self.host = entry_data[CONF_HOST]
|
self._host = entry_data[CONF_HOST]
|
||||||
self.client = Api2(self.host, session=async_get_clientsession(self.hass))
|
self.client = Api2(self._host, session=async_get_clientsession(self.hass))
|
||||||
|
|
||||||
return await self.async_step_reauth_confirm()
|
return await self.async_step_reauth_confirm()
|
||||||
|
|
||||||
@ -182,6 +184,16 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
errors=errors,
|
errors=errors,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
async def async_step_dhcp(
|
||||||
|
self, discovery_info: DhcpServiceInfo
|
||||||
|
) -> ConfigFlowResult:
|
||||||
|
"""Handle DHCP discovery."""
|
||||||
|
await self.async_set_unique_id(format_mac(discovery_info.macaddress))
|
||||||
|
self._abort_if_unique_id_configured(updates={CONF_HOST: discovery_info.ip})
|
||||||
|
# This should never happen since we only listen to DHCP requests
|
||||||
|
# for configured devices.
|
||||||
|
return self.async_abort(reason="already_configured")
|
||||||
|
|
||||||
async def _async_check_auth_required(self, user_input: dict[str, Any]) -> bool:
|
async def _async_check_auth_required(self, user_input: dict[str, Any]) -> bool:
|
||||||
"""Check if auth required and attempt to authenticate."""
|
"""Check if auth required and attempt to authenticate."""
|
||||||
if await self.client.check_auth_needed():
|
if await self.client.check_auth_needed():
|
||||||
@ -200,11 +212,14 @@ class SmlightConfigFlow(ConfigFlow, domain=DOMAIN):
|
|||||||
await self.async_set_unique_id(
|
await self.async_set_unique_id(
|
||||||
format_mac(info.MAC), raise_on_progress=self.source != SOURCE_USER
|
format_mac(info.MAC), raise_on_progress=self.source != SOURCE_USER
|
||||||
)
|
)
|
||||||
self._abort_if_unique_id_configured()
|
self._abort_if_unique_id_configured(updates={CONF_HOST: self._host})
|
||||||
|
|
||||||
if user_input.get(CONF_HOST) is None:
|
user_input[CONF_HOST] = self._host
|
||||||
user_input[CONF_HOST] = self.host
|
|
||||||
|
|
||||||
assert info.model is not None
|
assert info.model is not None
|
||||||
title = self.context.get("title_placeholders", {}).get(CONF_NAME) or info.model
|
title = (
|
||||||
|
self.context.get("title_placeholders", {}).get(CONF_NAME)
|
||||||
|
or self._device_name
|
||||||
|
or info.model
|
||||||
|
)
|
||||||
return self.async_create_entry(title=title, data=user_input)
|
return self.async_create_entry(title=title, data=user_input)
|
||||||
|
@ -3,6 +3,11 @@
|
|||||||
"name": "SMLIGHT SLZB",
|
"name": "SMLIGHT SLZB",
|
||||||
"codeowners": ["@tl-sl"],
|
"codeowners": ["@tl-sl"],
|
||||||
"config_flow": true,
|
"config_flow": true,
|
||||||
|
"dhcp": [
|
||||||
|
{
|
||||||
|
"registered_devices": true
|
||||||
|
}
|
||||||
|
],
|
||||||
"documentation": "https://www.home-assistant.io/integrations/smlight",
|
"documentation": "https://www.home-assistant.io/integrations/smlight",
|
||||||
"integration_type": "device",
|
"integration_type": "device",
|
||||||
"iot_class": "local_push",
|
"iot_class": "local_push",
|
||||||
|
4
homeassistant/generated/dhcp.py
generated
4
homeassistant/generated/dhcp.py
generated
@ -616,6 +616,10 @@ DHCP: Final[list[dict[str, str | bool]]] = [
|
|||||||
"hostname": "hub*",
|
"hostname": "hub*",
|
||||||
"macaddress": "286D97*",
|
"macaddress": "286D97*",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"domain": "smlight",
|
||||||
|
"registered_devices": True,
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"domain": "solaredge",
|
"domain": "solaredge",
|
||||||
"hostname": "target",
|
"hostname": "target",
|
||||||
|
@ -18,7 +18,8 @@ from tests.common import (
|
|||||||
load_json_object_fixture,
|
load_json_object_fixture,
|
||||||
)
|
)
|
||||||
|
|
||||||
MOCK_HOST = "slzb-06.local"
|
MOCK_DEVICE_NAME = "slzb-06"
|
||||||
|
MOCK_HOST = "192.168.1.161"
|
||||||
MOCK_USERNAME = "test-user"
|
MOCK_USERNAME = "test-user"
|
||||||
MOCK_PASSWORD = "test-pass"
|
MOCK_PASSWORD = "test-pass"
|
||||||
|
|
||||||
|
@ -3,7 +3,7 @@
|
|||||||
DeviceRegistryEntrySnapshot({
|
DeviceRegistryEntrySnapshot({
|
||||||
'area_id': None,
|
'area_id': None,
|
||||||
'config_entries': <ANY>,
|
'config_entries': <ANY>,
|
||||||
'configuration_url': 'http://slzb-06.local',
|
'configuration_url': 'http://192.168.1.161',
|
||||||
'connections': set({
|
'connections': set({
|
||||||
tuple(
|
tuple(
|
||||||
'mac',
|
'mac',
|
||||||
|
@ -8,19 +8,20 @@ from pysmlight.exceptions import SmlightAuthError, SmlightConnectionError
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.smlight.const import DOMAIN
|
from homeassistant.components.smlight.const import DOMAIN
|
||||||
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
|
from homeassistant.config_entries import SOURCE_DHCP, SOURCE_USER, SOURCE_ZEROCONF
|
||||||
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
from homeassistant.const import CONF_HOST, CONF_PASSWORD, CONF_USERNAME
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
from homeassistant.data_entry_flow import FlowResultType
|
from homeassistant.data_entry_flow import FlowResultType
|
||||||
|
from homeassistant.helpers.service_info.dhcp import DhcpServiceInfo
|
||||||
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
from homeassistant.helpers.service_info.zeroconf import ZeroconfServiceInfo
|
||||||
|
|
||||||
from .conftest import MOCK_HOST, MOCK_PASSWORD, MOCK_USERNAME
|
from .conftest import MOCK_DEVICE_NAME, MOCK_HOST, MOCK_PASSWORD, MOCK_USERNAME
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
DISCOVERY_INFO = ZeroconfServiceInfo(
|
DISCOVERY_INFO = ZeroconfServiceInfo(
|
||||||
ip_address=ip_address("127.0.0.1"),
|
ip_address=ip_address("192.168.1.161"),
|
||||||
ip_addresses=[ip_address("127.0.0.1")],
|
ip_addresses=[ip_address("192.168.1.161")],
|
||||||
hostname="slzb-06.local.",
|
hostname="slzb-06.local.",
|
||||||
name="mock_name",
|
name="mock_name",
|
||||||
port=6638,
|
port=6638,
|
||||||
@ -29,8 +30,8 @@ DISCOVERY_INFO = ZeroconfServiceInfo(
|
|||||||
)
|
)
|
||||||
|
|
||||||
DISCOVERY_INFO_LEGACY = ZeroconfServiceInfo(
|
DISCOVERY_INFO_LEGACY = ZeroconfServiceInfo(
|
||||||
ip_address=ip_address("127.0.0.1"),
|
ip_address=ip_address("192.168.1.161"),
|
||||||
ip_addresses=[ip_address("127.0.0.1")],
|
ip_addresses=[ip_address("192.168.1.161")],
|
||||||
hostname="slzb-06.local.",
|
hostname="slzb-06.local.",
|
||||||
name="mock_name",
|
name="mock_name",
|
||||||
port=6638,
|
port=6638,
|
||||||
@ -52,7 +53,7 @@ async def test_user_flow(hass: HomeAssistant, mock_setup_entry: AsyncMock) -> No
|
|||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
{
|
{
|
||||||
CONF_HOST: MOCK_HOST,
|
CONF_HOST: "slzb-06p7.local",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -76,7 +77,7 @@ async def test_zeroconf_flow(
|
|||||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["description_placeholders"] == {"host": MOCK_HOST}
|
assert result["description_placeholders"] == {"host": MOCK_DEVICE_NAME}
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "confirm_discovery"
|
assert result["step_id"] == "confirm_discovery"
|
||||||
|
|
||||||
@ -113,7 +114,7 @@ async def test_zeroconf_flow_auth(
|
|||||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["description_placeholders"] == {"host": MOCK_HOST}
|
assert result["description_placeholders"] == {"host": MOCK_DEVICE_NAME}
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "confirm_discovery"
|
assert result["step_id"] == "confirm_discovery"
|
||||||
|
|
||||||
@ -167,7 +168,7 @@ async def test_zeroconf_unsupported_abort(
|
|||||||
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=DISCOVERY_INFO
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["description_placeholders"] == {"host": MOCK_HOST}
|
assert result["description_placeholders"] == {"host": MOCK_DEVICE_NAME}
|
||||||
assert result["type"] is FlowResultType.FORM
|
assert result["type"] is FlowResultType.FORM
|
||||||
assert result["step_id"] == "confirm_discovery"
|
assert result["step_id"] == "confirm_discovery"
|
||||||
|
|
||||||
@ -489,7 +490,7 @@ async def test_zeroconf_legacy_mac(
|
|||||||
data=DISCOVERY_INFO_LEGACY,
|
data=DISCOVERY_INFO_LEGACY,
|
||||||
)
|
)
|
||||||
|
|
||||||
assert result["description_placeholders"] == {"host": MOCK_HOST}
|
assert result["description_placeholders"] == {"host": MOCK_DEVICE_NAME}
|
||||||
|
|
||||||
result2 = await hass.config_entries.flow.async_configure(
|
result2 = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"], user_input={}
|
result["flow_id"], user_input={}
|
||||||
@ -507,6 +508,76 @@ async def test_zeroconf_legacy_mac(
|
|||||||
assert len(mock_smlight_client.get_info.mock_calls) == 3
|
assert len(mock_smlight_client.get_info.mock_calls) == 3
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_smlight_client")
|
||||||
|
async def test_zeroconf_updates_host(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test zeroconf discovery updates host ip."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
service_info = DISCOVERY_INFO
|
||||||
|
service_info.ip_address = ip_address("192.168.1.164")
|
||||||
|
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_ZEROCONF}, data=service_info
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
assert mock_config_entry.data[CONF_HOST] == "192.168.1.164"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_smlight_client")
|
||||||
|
async def test_dhcp_discovery_updates_host(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test dhcp discovery updates host ip."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
service_info = DhcpServiceInfo(
|
||||||
|
ip="192.168.1.164",
|
||||||
|
hostname="slzb-06",
|
||||||
|
macaddress="aabbccddeeff",
|
||||||
|
)
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_DHCP}, data=service_info
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
assert mock_config_entry.data[CONF_HOST] == "192.168.1.164"
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.usefixtures("mock_smlight_client")
|
||||||
|
async def test_dhcp_discovery_aborts(
|
||||||
|
hass: HomeAssistant,
|
||||||
|
mock_setup_entry: AsyncMock,
|
||||||
|
mock_config_entry: MockConfigEntry,
|
||||||
|
) -> None:
|
||||||
|
"""Test dhcp discovery updates host ip."""
|
||||||
|
mock_config_entry.add_to_hass(hass)
|
||||||
|
|
||||||
|
service_info = DhcpServiceInfo(
|
||||||
|
ip="192.168.1.161",
|
||||||
|
hostname="slzb-06",
|
||||||
|
macaddress="000000000000",
|
||||||
|
)
|
||||||
|
result = await hass.config_entries.flow.async_init(
|
||||||
|
DOMAIN, context={"source": SOURCE_DHCP}, data=service_info
|
||||||
|
)
|
||||||
|
|
||||||
|
assert result["type"] is FlowResultType.ABORT
|
||||||
|
assert result["reason"] == "already_configured"
|
||||||
|
|
||||||
|
assert mock_config_entry.data[CONF_HOST] == "192.168.1.161"
|
||||||
|
|
||||||
|
|
||||||
async def test_reauth_flow(
|
async def test_reauth_flow(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
mock_smlight_client: MagicMock,
|
mock_smlight_client: MagicMock,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user