Use IP addresses instead of mDNS names when wled discovered (#33608)

This commit is contained in:
Paulus Schoutsen 2020-04-03 22:41:08 -07:00 committed by GitHub
parent 07ae3f9ee9
commit c6ba607c8c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 35 additions and 33 deletions

View File

@ -45,7 +45,7 @@ class WLEDFlowHandler(ConfigFlow, domain=DOMAIN):
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167 # pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
self.context.update( self.context.update(
{ {
CONF_HOST: host, CONF_HOST: user_input["host"],
CONF_NAME: name, CONF_NAME: name,
CONF_MAC: user_input["properties"].get(CONF_MAC), CONF_MAC: user_input["properties"].get(CONF_MAC),
"title_placeholders": {"name": name}, "title_placeholders": {"name": name},

View File

@ -18,31 +18,31 @@ async def init_integration(
fixture = "wled/rgb.json" if not rgbw else "wled/rgbw.json" fixture = "wled/rgb.json" if not rgbw else "wled/rgbw.json"
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/", "http://192.168.1.123:80/json/",
text=load_fixture(fixture), text=load_fixture(fixture),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
aioclient_mock.post( aioclient_mock.post(
"http://example.local:80/json/state", "http://192.168.1.123:80/json/state",
json={}, json={},
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/info", "http://192.168.1.123:80/json/info",
json={}, json={},
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/state", "http://192.168.1.123:80/json/state",
json={}, json={},
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
entry = MockConfigEntry( entry = MockConfigEntry(
domain=DOMAIN, data={CONF_HOST: "example.local", CONF_MAC: "aabbccddeeff"} domain=DOMAIN, data={CONF_HOST: "192.168.1.123", CONF_MAC: "aabbccddeeff"}
) )
entry.add_to_hass(hass) entry.add_to_hass(hass)

View File

@ -40,7 +40,7 @@ async def test_show_zerconf_form(
) -> None: ) -> None:
"""Test that the zeroconf confirmation form is served.""" """Test that the zeroconf confirmation form is served."""
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/", "http://192.168.1.123:80/json/",
text=load_fixture("wled/rgb.json"), text=load_fixture("wled/rgb.json"),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
@ -49,10 +49,10 @@ async def test_show_zerconf_form(
flow.hass = hass flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF} flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf( result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "properties": {}} {"host": "192.168.1.123", "hostname": "example.local.", "properties": {}}
) )
assert flow.context[CONF_HOST] == "example.local" assert flow.context[CONF_HOST] == "192.168.1.123"
assert flow.context[CONF_NAME] == "example" assert flow.context[CONF_NAME] == "example"
assert result["description_placeholders"] == {CONF_NAME: "example"} assert result["description_placeholders"] == {CONF_NAME: "example"}
assert result["step_id"] == "zeroconf_confirm" assert result["step_id"] == "zeroconf_confirm"
@ -80,12 +80,12 @@ async def test_zeroconf_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test we abort zeroconf flow on WLED connection error.""" """Test we abort zeroconf flow on WLED connection error."""
aioclient_mock.get("http://example.local/json/", exc=aiohttp.ClientError) aioclient_mock.get("http://192.168.1.123/json/", exc=aiohttp.ClientError)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"hostname": "example.local.", "properties": {}}, data={"host": "192.168.1.123", "hostname": "example.local.", "properties": {}},
) )
assert result["reason"] == "connection_error" assert result["reason"] == "connection_error"
@ -96,7 +96,7 @@ async def test_zeroconf_confirm_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test we abort zeroconf flow on WLED connection error.""" """Test we abort zeroconf flow on WLED connection error."""
aioclient_mock.get("http://example.com/json/", exc=aiohttp.ClientError) aioclient_mock.get("http://192.168.1.123:80/json/", exc=aiohttp.ClientError)
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, config_flow.DOMAIN,
@ -105,7 +105,7 @@ async def test_zeroconf_confirm_connection_error(
CONF_HOST: "example.com", CONF_HOST: "example.com",
CONF_NAME: "test", CONF_NAME: "test",
}, },
data={"hostname": "example.com.", "properties": {}}, data={"host": "192.168.1.123", "hostname": "example.com.", "properties": {}},
) )
assert result["reason"] == "connection_error" assert result["reason"] == "connection_error"
@ -133,7 +133,7 @@ async def test_user_device_exists_abort(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, config_flow.DOMAIN,
context={"source": SOURCE_USER}, context={"source": SOURCE_USER},
data={CONF_HOST: "example.local"}, data={CONF_HOST: "192.168.1.123"},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
@ -149,7 +149,7 @@ async def test_zeroconf_device_exists_abort(
result = await hass.config_entries.flow.async_init( result = await hass.config_entries.flow.async_init(
config_flow.DOMAIN, config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"hostname": "example.local.", "properties": {}}, data={"host": "192.168.1.123", "hostname": "example.local.", "properties": {}},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
@ -165,7 +165,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(
config_flow.DOMAIN, config_flow.DOMAIN,
context={"source": SOURCE_ZEROCONF}, context={"source": SOURCE_ZEROCONF},
data={"hostname": "example.local.", "properties": {CONF_MAC: "aabbccddeeff"}}, data={
"host": "192.168.1.123",
"hostname": "example.local.",
"properties": {CONF_MAC: "aabbccddeeff"},
},
) )
assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT assert result["type"] == data_entry_flow.RESULT_TYPE_ABORT
@ -177,7 +181,7 @@ async def test_full_user_flow_implementation(
) -> None: ) -> None:
"""Test the full manual user flow from start to finish.""" """Test the full manual user flow from start to finish."""
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/", "http://192.168.1.123:80/json/",
text=load_fixture("wled/rgb.json"), text=load_fixture("wled/rgb.json"),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
@ -190,12 +194,12 @@ async def test_full_user_flow_implementation(
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await hass.config_entries.flow.async_configure( result = await hass.config_entries.flow.async_configure(
result["flow_id"], user_input={CONF_HOST: "example.local"} result["flow_id"], user_input={CONF_HOST: "192.168.1.123"}
) )
assert result["data"][CONF_HOST] == "example.local" assert result["data"][CONF_HOST] == "192.168.1.123"
assert result["data"][CONF_MAC] == "aabbccddeeff" assert result["data"][CONF_MAC] == "aabbccddeeff"
assert result["title"] == "example.local" assert result["title"] == "192.168.1.123"
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
@ -204,7 +208,7 @@ async def test_full_zeroconf_flow_implementation(
) -> None: ) -> None:
"""Test the full manual user flow from start to finish.""" """Test the full manual user flow from start to finish."""
aioclient_mock.get( aioclient_mock.get(
"http://example.local:80/json/", "http://192.168.1.123:80/json/",
text=load_fixture("wled/rgb.json"), text=load_fixture("wled/rgb.json"),
headers={"Content-Type": "application/json"}, headers={"Content-Type": "application/json"},
) )
@ -213,19 +217,17 @@ async def test_full_zeroconf_flow_implementation(
flow.hass = hass flow.hass = hass
flow.context = {"source": SOURCE_ZEROCONF} flow.context = {"source": SOURCE_ZEROCONF}
result = await flow.async_step_zeroconf( result = await flow.async_step_zeroconf(
{"hostname": "example.local.", "properties": {}} {"host": "192.168.1.123", "hostname": "example.local.", "properties": {}}
) )
assert flow.context[CONF_HOST] == "example.local" assert flow.context[CONF_HOST] == "192.168.1.123"
assert flow.context[CONF_NAME] == "example" assert flow.context[CONF_NAME] == "example"
assert result["description_placeholders"] == {CONF_NAME: "example"} assert result["description_placeholders"] == {CONF_NAME: "example"}
assert result["step_id"] == "zeroconf_confirm" assert result["step_id"] == "zeroconf_confirm"
assert result["type"] == data_entry_flow.RESULT_TYPE_FORM assert result["type"] == data_entry_flow.RESULT_TYPE_FORM
result = await flow.async_step_zeroconf_confirm( result = await flow.async_step_zeroconf_confirm(user_input={})
user_input={CONF_HOST: "example.local"} assert result["data"][CONF_HOST] == "192.168.1.123"
)
assert result["data"][CONF_HOST] == "example.local"
assert result["data"][CONF_MAC] == "aabbccddeeff" assert result["data"][CONF_MAC] == "aabbccddeeff"
assert result["title"] == "example" assert result["title"] == "example"
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY

View File

@ -13,7 +13,7 @@ async def test_config_entry_not_ready(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test the WLED configuration entry not ready.""" """Test the WLED configuration entry not ready."""
aioclient_mock.get("http://example.local:80/json/", exc=aiohttp.ClientError) aioclient_mock.get("http://192.168.1.123:80/json/", exc=aiohttp.ClientError)
entry = await init_integration(hass, aioclient_mock) entry = await init_integration(hass, aioclient_mock)
assert entry.state == ENTRY_STATE_SETUP_RETRY assert entry.state == ENTRY_STATE_SETUP_RETRY

View File

@ -141,7 +141,7 @@ async def test_light_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None: ) -> None:
"""Test error handling of the WLED lights.""" """Test error handling of the WLED lights."""
aioclient_mock.post("http://example.local:80/json/state", text="", status=400) aioclient_mock.post("http://192.168.1.123:80/json/state", text="", status=400)
await init_integration(hass, aioclient_mock) await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"): with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
@ -162,7 +162,7 @@ async def test_light_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test error handling of the WLED switches.""" """Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", exc=aiohttp.ClientError) aioclient_mock.post("http://192.168.1.123:80/json/state", exc=aiohttp.ClientError)
await init_integration(hass, aioclient_mock) await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"): with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
@ -339,7 +339,7 @@ async def test_effect_service_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None: ) -> None:
"""Test error handling of the WLED effect service.""" """Test error handling of the WLED effect service."""
aioclient_mock.post("http://example.local:80/json/state", text="", status=400) aioclient_mock.post("http://192.168.1.123:80/json/state", text="", status=400)
await init_integration(hass, aioclient_mock) await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"): with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):

View File

@ -139,7 +139,7 @@ async def test_switch_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog hass: HomeAssistant, aioclient_mock: AiohttpClientMocker, caplog
) -> None: ) -> None:
"""Test error handling of the WLED switches.""" """Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", text="", status=400) aioclient_mock.post("http://192.168.1.123:80/json/state", text="", status=400)
await init_integration(hass, aioclient_mock) await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"): with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):
@ -160,7 +160,7 @@ async def test_switch_connection_error(
hass: HomeAssistant, aioclient_mock: AiohttpClientMocker hass: HomeAssistant, aioclient_mock: AiohttpClientMocker
) -> None: ) -> None:
"""Test error handling of the WLED switches.""" """Test error handling of the WLED switches."""
aioclient_mock.post("http://example.local:80/json/state", exc=aiohttp.ClientError) aioclient_mock.post("http://192.168.1.123:80/json/state", exc=aiohttp.ClientError)
await init_integration(hass, aioclient_mock) await init_integration(hass, aioclient_mock)
with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"): with patch("homeassistant.components.wled.WLEDDataUpdateCoordinator.async_refresh"):