8000 Migrate bracketed IP addresses in ZHA config entry (#95917) · home-assistant/core@ecc0917 · GitHub
[go: up one dir, main page]

Skip to content

Commit ecc0917

Browse files
authored
Migrate bracketed IP addresses in ZHA config entry (#95917)
* Automatically correct IP addresses surrounded by brackets * Simplify regex * Move pattern inline * Maintain old behavior of stripping whitespace
1 parent 5964534 commit ecc0917

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

homeassistant/components/zha/__init__.py

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import copy
44
import logging
55
import os
6+
import re
67

78
import voluptuous as vol
89
from zhaquirks import setup as setup_quirks
@@ -85,19 +86,34 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
8586
return True
8687

8788

89+
def _clean_serial_port_path(path: str) -> str:
90+
"""Clean the serial port path, applying corrections where necessary."""
91+
92+
if path.startswith("socket://"):
93+
path = path.strip()
94+
95+
# Removes extraneous brackets from IP addresses (they don't parse in CPython 3.11.4)
96+
if re.match(r"^socket://\[\d+\.\d+\.\d+\.\d+\]:\d+$", path):
97+
path = path.replace("[", "").replace("]", "")
98+
99+
return path
100+
101+
88102
async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
89103
"""Set up ZHA.
90104
91105
Will automatically load components to support devices found on the network.
92106
"""
93107

94-
# Strip whitespace around `socket://` URIs, this is no longer accepted by zigpy
95-
# This will be removed in 2023.7.0
108+
# Remove brackets around IP addresses, this no longer works in CPython 3.11.4
109+
# This will be removed in 2023.11.0
96110
path = config_entry.data[CONF_DEVICE][CONF_DEVICE_PATH]
111+
cleaned_path = _clean_serial_port_path(path)
97112
data = copy.deepcopy(dict(config_entry.data))
98113

99-
if path.startswith("socket://") and path != path.strip():
100-
data[CONF_DEVICE][CONF_DEVICE_PATH] = path.strip()
114+
if path != cleaned_path:
115+
_LOGGER.debug("Cleaned serial port path %r -> %r", path, cleaned_path)
116+
data[CONF_DEVICE][CONF_DEVICE_PATH] = cleaned_path
101117
hass.config_entries.async_update_entry(config_entry, data=data)
102118

103119
zha_data = hass.data.setdefault(DATA_ZHA, {})

tests/components/zha/test_init.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,19 +114,27 @@ async def test_config_depreciation(hass: HomeAssistant, zha_config) -> None:
114114
@pytest.mark.parametrize(
115115
("path", "cleaned_path"),
116116
[
117+
# No corrections
117118
("/dev/path1", "/dev/path1"),
119+
("/dev/path1[asd]", "/dev/path1[asd]"),
118120
("/dev/path1 ", "/dev/path1 "),
121+
("socket://1.2.3.4:5678", "socket://1.2.3.4:5678"),
122+
# Brackets around URI
123+
("socket://[1.2.3.4]:5678", "socket://1.2.3.4:5678"),
124+
# Spaces
119125
("socket://dev/path1 ", "socket://dev/path1"),
126+
# Both
127+
("socket://[1.2.3.4]:5678 ", "socket://1.2.3.4:5678"),
120128
],
121129
)
122130
@patch("homeassistant.components.zha.setup_quirks", Mock(return_value=True))
123131
@patch(
124132
"homeassistant.components.zha.websocket_api.async_load_api", Mock(return_value=True)
125133
)
126-
async def test_setup_with_v3_spaces_in_uri(
134+
async def test_setup_with_v3_cleaning_uri(
127135
hass: HomeAssistant, path: str, cleaned_path: str
128136
) -> None:
129-
"""Test migration of config entry from v3 with spaces after `socket://` URI."""
137+
"""Test migration of config entry from v3, applying corrections to the port path."""
130138
config_entry_v3 = MockConfigEntry(
131139
domain=DOMAIN,
132140
data={

0 commit comments

Comments
 (0)
0