Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate bracketed IP addresses in ZHA config entry #95917

Merged
merged 4 commits into from
Jul 6, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions homeassistant/components/zha/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import copy
import logging
import os
import re

import voluptuous as vol
from zhaquirks import setup as setup_quirks
Expand Down Expand Up @@ -91,13 +92,13 @@ async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> b
Will automatically load components to support devices found on the network.
"""

# Strip whitespace around `socket://` URIs, this is no longer accepted by zigpy
# This will be removed in 2023.7.0
# Remove brackets around IP addresses, this no longer works in CPython 3.11.4
# This will be removed in 2023.11.0
path = config_entry.data[CONF_DEVICE][CONF_DEVICE_PATH]
data = copy.deepcopy(dict(config_entry.data))

if path.startswith("socket://") and path != path.strip():
data[CONF_DEVICE][CONF_DEVICE_PATH] = path.strip()
if re.match(r"^(socket|tcp)://\[\d+\.\d+\.\d+\.\d+\]:\d+$", path):
puddly marked this conversation as resolved.
Show resolved Hide resolved
data[CONF_DEVICE][CONF_DEVICE_PATH] = path.replace("[", "").replace("]", "")
hass.config_entries.async_update_entry(config_entry, data=data)

zha_data = hass.data.setdefault(DATA_ZHA, {})
Expand Down
9 changes: 5 additions & 4 deletions tests/components/zha/test_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,18 +115,19 @@ async def test_config_depreciation(hass: HomeAssistant, zha_config) -> None:
("path", "cleaned_path"),
[
("/dev/path1", "/dev/path1"),
("/dev/path1 ", "/dev/path1 "),
("socket://dev/path1 ", "socket://dev/path1"),
("/dev/path1[asd]", "/dev/path1[asd]"),
("socket://1.2.3.4:5678", "socket://1.2.3.4:5678"),
("socket://[1.2.3.4]:5678", "socket://1.2.3.4:5678"),
],
)
@patch("homeassistant.components.zha.setup_quirks", Mock(return_value=True))
@patch(
"homeassistant.components.zha.websocket_api.async_load_api", Mock(return_value=True)
)
async def test_setup_with_v3_spaces_in_uri(
async def test_setup_with_v3_brackets_in_uri(
hass: HomeAssistant, path: str, cleaned_path: str
) -> None:
"""Test migration of config entry from v3 with spaces after `socket://` URI."""
"""Test migration of config entry from v3 with brackets around the IP address."""
config_entry_v3 = MockConfigEntry(
domain=DOMAIN,
data={
Expand Down