|
3 | 3 | import copy
|
4 | 4 | import logging
|
5 | 5 | import os
|
| 6 | +import re |
6 | 7 |
|
7 | 8 | import voluptuous as vol
|
8 | 9 | from zhaquirks import setup as setup_quirks
|
@@ -85,19 +86,34 @@ async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
85 | 86 | return True
|
86 | 87 |
|
87 | 88 |
|
| 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 | + |
88 | 102 | async def async_setup_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
|
89 | 103 | """Set up ZHA.
|
90 | 104 |
|
91 | 105 | Will automatically load components to support devices found on the network.
|
92 | 106 | """
|
93 | 107 |
|
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 |
96 | 110 | path = config_entry.data[CONF_DEVICE][CONF_DEVICE_PATH]
|
| 111 | + cleaned_path = _clean_serial_port_path(path) |
97 | 112 | data = copy.deepcopy(dict(config_entry.data))
|
98 | 113 |
|
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 |
101 | 117 | hass.config_entries.async_update_entry(config_entry, data=data)
|
102 | 118 |
|
103 | 119 | zha_data = hass.data.setdefault(DATA_ZHA, {})
|
|
0 commit comments