Filter IPv6 addreses from enphase_envoy discovery (#68362)

This commit is contained in:
J. Nick Koston 2022-03-19 10:40:00 -10:00 committed by GitHub
parent 0df88b80e7
commit fed447a3f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 3 deletions

View File

@ -16,6 +16,7 @@ from homeassistant.core import HomeAssistant, callback
from homeassistant.data_entry_flow import FlowResult from homeassistant.data_entry_flow import FlowResult
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.httpx_client import get_async_client from homeassistant.helpers.httpx_client import get_async_client
from homeassistant.util.network import is_ipv4_address
from .const import DOMAIN from .const import DOMAIN
@ -86,6 +87,8 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self, discovery_info: zeroconf.ZeroconfServiceInfo self, discovery_info: zeroconf.ZeroconfServiceInfo
) -> FlowResult: ) -> FlowResult:
"""Handle a flow initialized by zeroconf discovery.""" """Handle a flow initialized by zeroconf discovery."""
if not is_ipv4_address(discovery_info.host):
return self.async_abort(reason="not_ipv4_address")
serial = discovery_info.properties["serialnum"] serial = discovery_info.properties["serialnum"]
await self.async_set_unique_id(serial) await self.async_set_unique_id(serial)
self.ip_address = discovery_info.host self.ip_address = discovery_info.host

View File

@ -2,7 +2,8 @@
"config": { "config": {
"abort": { "abort": {
"already_configured": "Device is already configured", "already_configured": "Device is already configured",
"reauth_successful": "Re-authentication was successful" "reauth_successful": "Re-authentication was successful",
"not_ipv4_address": "Only IPv4 addresess are supported"
}, },
"error": { "error": {
"cannot_connect": "Failed to connect", "cannot_connect": "Failed to connect",

View File

@ -6,6 +6,7 @@ import httpx
from homeassistant import config_entries from homeassistant import config_entries
from homeassistant.components import zeroconf from homeassistant.components import zeroconf
from homeassistant.components.enphase_envoy.const import DOMAIN from homeassistant.components.enphase_envoy.const import DOMAIN
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant from homeassistant.core import HomeAssistant
from tests.common import MockConfigEntry from tests.common import MockConfigEntry
@ -312,8 +313,8 @@ async def test_zeroconf_serial_already_exists(hass: HomeAssistant) -> None:
DOMAIN, DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF}, context={"source": config_entries.SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo( data=zeroconf.ZeroconfServiceInfo(
host="1.1.1.1", host="4.4.4.4",
addresses=["1.1.1.1"], addresses=["4.4.4.4"],
hostname="mock_hostname", hostname="mock_hostname",
name="mock_name", name="mock_name",
port=None, port=None,
@ -324,6 +325,42 @@ async def test_zeroconf_serial_already_exists(hass: HomeAssistant) -> None:
assert result["type"] == "abort" assert result["type"] == "abort"
assert result["reason"] == "already_configured" assert result["reason"] == "already_configured"
assert config_entry.data[CONF_HOST] == "4.4.4.4"
async def test_zeroconf_serial_already_exists_ignores_ipv6(hass: HomeAssistant) -> None:
"""Test serial number already exists from zeroconf but the discovery is ipv6."""
config_entry = MockConfigEntry(
domain=DOMAIN,
data={
"host": "1.1.1.1",
"name": "Envoy",
"username": "test-username",
"password": "test-password",
},
unique_id="1234",
title="Envoy",
)
config_entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
context={"source": config_entries.SOURCE_ZEROCONF},
data=zeroconf.ZeroconfServiceInfo(
host="fd00::b27c:63bb:cc85:4ea0",
addresses=["fd00::b27c:63bb:cc85:4ea0"],
hostname="mock_hostname",
name="mock_name",
port=None,
properties={"serialnum": "1234"},
type="mock_type",
),
)
assert result["type"] == "abort"
assert result["reason"] == "not_ipv4_address"
assert config_entry.data[CONF_HOST] == "1.1.1.1"
async def test_zeroconf_host_already_exists(hass: HomeAssistant) -> None: async def test_zeroconf_host_already_exists(hass: HomeAssistant) -> None: