Use ZeroconfServiceInfo in wled (#60130)

Co-authored-by: epenet <epenet@users.noreply.github.com>
This commit is contained in:
epenet 2021-11-22 15:19:54 +01:00 committed by GitHub
parent c8451001a0
commit 7e1b00c491
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 12 deletions

View File

@ -44,14 +44,14 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
"""Handle zeroconf discovery.""" """Handle zeroconf discovery."""
# Hostname is format: wled-livingroom.local. # Hostname is format: wled-livingroom.local.
host = discovery_info["hostname"].rstrip(".") host = discovery_info[zeroconf.ATTR_HOSTNAME].rstrip(".")
name, _ = host.rsplit(".") name, _ = host.rsplit(".")
self.context.update( self.context.update(
{ {
CONF_HOST: discovery_info["host"], CONF_HOST: discovery_info[zeroconf.ATTR_HOST],
CONF_NAME: name, CONF_NAME: name,
CONF_MAC: discovery_info["properties"].get(CONF_MAC), CONF_MAC: discovery_info[zeroconf.ATTR_PROPERTIES].get(CONF_MAC),
"title_placeholders": {"name": name}, "title_placeholders": {"name": name},
} }
) )

View File

@ -3,6 +3,7 @@ from unittest.mock import MagicMock
from wled import WLEDConnectionError from wled import WLEDConnectionError
from homeassistant.components import zeroconf
from homeassistant.components.wled.const import CONF_KEEP_MASTER_LIGHT, DOMAIN from homeassistant.components.wled.const import CONF_KEEP_MASTER_LIGHT, DOMAIN
from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF from homeassistant.config_entries import SOURCE_USER, SOURCE_ZEROCONF
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME from homeassistant.const import CONF_HOST, CONF_MAC, CONF_NAME
@ -47,7 +48,9 @@ async def test_full_zeroconf_flow_implementation(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"host": "192.168.1.123", "hostname": "example.local.", "properties": {}}, data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.123", hostname="example.local.", properties={}
),
) )
flows = hass.config_entries.flow.async_progress() flows = hass.config_entries.flow.async_progress()
@ -100,7 +103,9 @@ async def test_zeroconf_connection_error(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"host": "192.168.1.123", "hostname": "example.local.", "properties": {}}, data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.123", hostname="example.local.", properties={}
),
) )
assert result.get("type") == RESULT_TYPE_ABORT assert result.get("type") == RESULT_TYPE_ABORT
@ -120,7 +125,9 @@ async def test_zeroconf_confirm_connection_error(
CONF_HOST: "example.com", CONF_HOST: "example.com",
CONF_NAME: "test", CONF_NAME: "test",
}, },
data={"host": "192.168.1.123", "hostname": "example.com.", "properties": {}}, data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.123", hostname="example.com.", properties={}
),
) )
assert result.get("type") == RESULT_TYPE_ABORT assert result.get("type") == RESULT_TYPE_ABORT
@ -152,7 +159,9 @@ async def test_zeroconf_device_exists_abort(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"host": "192.168.1.123", "hostname": "example.local.", "properties": {}}, data=zeroconf.ZeroconfServiceInfo(
host="192.168.1.123", hostname="example.local.", properties={}
),
) )
assert result.get("type") == RESULT_TYPE_ABORT assert result.get("type") == RESULT_TYPE_ABORT
@ -168,11 +177,11 @@ async def test_zeroconf_with_mac_device_exists_abort(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
DOMAIN, DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={ data=zeroconf.ZeroconfServiceInfo(
"host": "192.168.1.123", host="192.168.1.123",
"hostname": "example.local.", hostname="example.local.",
"properties": {CONF_MAC: "aabbccddeeff"}, properties={CONF_MAC: "aabbccddeeff"},
}, ),
) )
assert result.get("type") == RESULT_TYPE_ABORT assert result.get("type") == RESULT_TYPE_ABORT