Abort the config flow if the Shelly device is not supported (#184668)

Co-authored-by: Shay Levy <levyshay1@gmail.com>
This commit is contained in:
Maciej Bieniek
2026-10-07 16:04:12 +02:00
committed by GitHub
co-authored by Shay Levy
parent 9b51eac345
commit 38264dba0a
4 changed files with 221 additions and 5 deletions
@@ -94,12 +94,14 @@ from .utils import (
get_block_device_sleep_period,
get_coap_context,
get_device_entry_gen,
get_device_from_manufacturer_data,
get_http_port,
get_info_auth,
get_info_gen,
get_model_name,
get_rpc_device_wakeup_period,
get_ws_context,
is_device_supported,
mac_address_from_name,
)
@@ -444,6 +446,12 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
await self.async_set_unique_id(self.info[CONF_MAC], raise_on_progress=False)
self._abort_if_unique_id_configured({CONF_HOST: host})
if not is_device_supported(self.info):
return self.async_abort(
reason="unsupported_device",
description_placeholders={"model": get_model_name(self.info)},
)
self.host = host
self.port = port
self.verify_ssl = verify_ssl
@@ -507,6 +515,15 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
self.device_name = device_data.name
await self.async_set_unique_id(device_data.mac, raise_on_progress=False)
self._abort_if_unique_id_configured()
if (
device := get_device_from_manufacturer_data(
device_data.discovery_info.manufacturer_data
)
) and not device.supported:
return self.async_abort(
reason="unsupported_device",
description_placeholders={"model": device.name},
)
self.context.update(
{
"title_placeholders": {"name": self.device_name},
@@ -782,6 +799,15 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
# Check if already configured - abort if device is already set up
await self.async_set_unique_id(mac)
self._abort_if_unique_id_configured()
if (
device := get_device_from_manufacturer_data(
discovery_info.manufacturer_data
)
) and not device.supported:
return self.async_abort(
reason="unsupported_device",
description_placeholders={"model": device.name},
)
# Store BLE device and name for WiFi provisioning
self.ble_device = async_ble_device_from_address(
@@ -1202,6 +1228,12 @@ class ShellyConfigFlow(ConfigFlow, domain=DOMAIN):
mac = self.info[CONF_MAC]
await self._async_handle_zeroconf_mac_discovery(mac, host, port)
if not is_device_supported(self.info):
return self.async_abort(
reason="unsupported_device",
description_placeholders={"model": get_model_name(self.info)},
)
self.host = host
self.port = port
self.verify_ssl = verify_ssl
@@ -14,6 +14,7 @@
"no_wifi_networks": "No Wi-Fi networks found during scan.",
"reauth_unsuccessful": "Re-authentication was unsuccessful, please remove the integration and set it up again.",
"unknown": "[%key:common::config_flow::error::unknown%]",
"unsupported_device": "{model} is not supported by the Shelly integration because it uses an incompatible protocol.",
"wifi_provisioned": "Wi-Fi credentials for {ssid} have been provisioned to {name}. The device is connecting to Wi-Fi and will complete setup automatically."
},
"error": {
+28
View File
@@ -5,6 +5,8 @@ from ipaddress import IPv4Address, IPv6Address, ip_address
from typing import TYPE_CHECKING, Any, cast
from aiohttp.web import Request, WebSocketResponse
from aioshelly.ble import get_device_from_model_id
from aioshelly.ble.manufacturer_data import parse_shelly_manufacturer_data
from aioshelly.block_device import COAP, Block, BlockDevice
from aioshelly.const import (
BLOCK_GENERATIONS,
@@ -12,6 +14,7 @@ from aioshelly.const import (
BLU_TRV_MODEL_NAME,
DEFAULT_COAP_PORT,
DEFAULT_HTTP_PORT,
DEVICES,
MODEL_1L,
MODEL_DIMMER,
MODEL_DIMMER_2,
@@ -20,6 +23,7 @@ from aioshelly.const import (
MODEL_NAMES,
MODEL_PLUG,
RPC_GENERATIONS,
ShellyDevice,
)
from aioshelly.rpc_device import RpcDevice, WsServer
from yarl import URL
@@ -321,6 +325,30 @@ def get_model_name(info: dict[str, Any]) -> str:
return cast(str, MODEL_NAMES.get(info["type"], info["type"]))
def is_device_supported(info: dict[str, Any]) -> bool:
"""Return True if the device model is supported."""
if get_info_gen(info) in RPC_GENERATIONS:
# Devices with firmware not fully provisioned
model = info.get(CONF_MODEL, "")
else:
model = info["type"]
if (device := DEVICES.get(model)) is None:
return True
return device.supported
def get_device_from_manufacturer_data(
manufacturer_data: dict[int, bytes],
) -> ShellyDevice | None:
"""Return the Shelly device matching the advertised BLE model ID."""
parsed = parse_shelly_manufacturer_data(manufacturer_data)
if not parsed or not isinstance(model_id := parsed.get("model_id"), int):
return None
return get_device_from_model_id(model_id)
def get_shelly_model_name(
model: str,
sleep_period: int,
+160 -5
View File
@@ -11,6 +11,8 @@ from aioshelly.const import (
DEFAULT_HTTP_PORT,
DEFAULT_HTTPS_PORT,
MODEL_1,
MODEL_4PRO,
MODEL_AZ_HT,
MODEL_PLUS_2PM,
)
from aioshelly.exceptions import (
@@ -132,6 +134,12 @@ BLE_MANUFACTURER_DATA_WITH_MAC_UNKNOWN_MODEL = {
# Flags (0x01, 0x05, 0x00), Model (0x0b, 0x99, 0x99) -
# unknown model ID, MAC (0x0a, 0x70, 0xd6, 0xc2, 0x97, 0xba, 0xcc)
BLE_MANUFACTURER_DATA_UNSUPPORTED_MODEL = {
0x0BA9: bytes.fromhex("0105000b16180a70d6c297bacc")
}
# Flags (0x01, 0x05, 0x00), Model (0x0b, 0x16, 0x18) - Shelly AZ H&T (0x1816),
# unsupported model, MAC (0x0a, 0x70, 0xd6, 0xc2, 0x97, 0xba, 0xcc)
BLE_MANUFACTURER_DATA_FOR_CLEAR_TEST = {
0x0BA9: bytes.fromhex("0105000b30100a00eeddccbbaa")
}
@@ -259,6 +267,26 @@ BLE_DISCOVERY_INFO_MAC_UNKNOWN_MODEL = BluetoothServiceInfoBleak(
tx_power=-127,
)
BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL = BluetoothServiceInfoBleak(
name="CC:BA:97:C2:D6:72", # BLE address as name (newer devices)
address="CC:BA:97:C2:D6:72", # BLE address may differ from device MAC
rssi=-32,
manufacturer_data=BLE_MANUFACTURER_DATA_UNSUPPORTED_MODEL,
service_uuids=[],
service_data={},
source="local",
device=generate_ble_device(
address="CC:BA:97:C2:D6:72",
name="CC:BA:97:C2:D6:72",
),
advertisement=generate_advertisement_data(
manufacturer_data=BLE_MANUFACTURER_DATA_UNSUPPORTED_MODEL,
),
time=0,
connectable=True,
tx_power=-127,
)
BLE_DISCOVERY_INFO_NO_DEVICE = BluetoothServiceInfoBleak(
name="ShellyPlus2PM-C049EF8873E8",
address="00:00:00:00:00:00", # Invalid address that won't be found
@@ -1023,7 +1051,7 @@ async def test_form_errors_test_connection(
with (
patch(
"homeassistant.components.shelly.config_flow.get_info",
return_value={"mac": "test-mac", "auth": False},
return_value={"mac": "test-mac", "type": MODEL_1, "auth": False},
),
patch(
"aioshelly.block_device.BlockDevice.create", new=AsyncMock(side_effect=exc)
@@ -1039,7 +1067,7 @@ async def test_form_errors_test_connection(
with patch(
"homeassistant.components.shelly.config_flow.get_info",
return_value={"mac": "test-mac", "auth": False},
return_value={"mac": "test-mac", "type": MODEL_1, "auth": False},
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -1737,7 +1765,7 @@ async def test_user_flow_zeroconf_device_requires_auth(
"homeassistant.components.shelly.config_flow.get_info",
return_value={
"mac": "AABBCCDDEEFF",
"model": MODEL_1,
"type": MODEL_1,
"auth": True, # Requires auth
"gen": 1,
},
@@ -1757,7 +1785,7 @@ async def test_user_flow_zeroconf_device_requires_auth(
"homeassistant.components.shelly.config_flow.get_info",
return_value={
"mac": "AABBCCDDEEFF",
"model": MODEL_1,
"type": MODEL_1,
"auth": False, # Auth passed with credentials
"gen": 1,
"port": 80,
@@ -2218,7 +2246,7 @@ async def test_form_auth_errors_test_connection_gen1(
with patch(
"homeassistant.components.shelly.config_flow.get_info",
return_value={"mac": "test-mac", "auth": True},
return_value={"mac": "test-mac", "type": MODEL_1, "auth": True},
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
@@ -5777,3 +5805,130 @@ async def test_bluetooth_provision_ble_reconnect_fails_during_ip_fetch(
# Abort flow to reach terminal state
hass.config_entries.flow.async_abort(result["flow_id"])
await hass.async_block_till_done(wait_background_tasks=True)
@pytest.mark.parametrize(
("get_info", "model_name"),
[
(
{"mac": "test-mac", "model": MODEL_AZ_HT, "auth": False, "gen": 3},
"Shelly AZ H&T",
),
(
{"mac": "test-mac", "type": MODEL_4PRO, "auth": False, "gen": 1},
"Shelly 4Pro",
),
],
)
async def test_form_unsupported_device(
hass: HomeAssistant, get_info: dict[str, Any], model_name: str
) -> None:
"""Test user flow aborts for a device model not supported by aioshelly."""
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
with patch(
"homeassistant.components.shelly.config_flow.get_info",
return_value=get_info,
):
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_HOST: "1.1.1.1", CONF_PORT: DEFAULT_HTTP_PORT},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unsupported_device"
assert result["description_placeholders"] == {"model": model_name}
async def test_zeroconf_unsupported_device(hass: HomeAssistant) -> None:
"""Test zeroconf discovery aborts for a device model not supported by aioshelly."""
with patch(
"homeassistant.components.shelly.config_flow.get_info",
return_value={
"mac": "test-mac",
"model": MODEL_AZ_HT,
"auth": False,
"gen": 3,
},
):
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=DISCOVERY_INFO,
context={"source": config_entries.SOURCE_ZEROCONF},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unsupported_device"
assert result["description_placeholders"] == {"model": "Shelly AZ H&T"}
@pytest.mark.usefixtures("mock_zeroconf")
async def test_bluetooth_discovery_unsupported_device(hass: HomeAssistant) -> None:
"""Test bluetooth discovery aborts before provisioning for unsupported model."""
await _async_inject_ble_discovery(hass, BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL)
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL,
context={"source": config_entries.SOURCE_BLUETOOTH},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unsupported_device"
assert result["description_placeholders"] == {"model": "Shelly AZ H&T"}
@pytest.mark.usefixtures("mock_zeroconf")
async def test_bluetooth_discovery_unsupported_device_already_configured(
hass: HomeAssistant,
) -> None:
"""Test already configured check runs before the unsupported model check."""
await _async_inject_ble_discovery(hass, BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL)
entry = MockConfigEntry(
domain=DOMAIN,
unique_id="CCBA97C2D670", # MAC from manufacturer data
data={
CONF_HOST: "1.1.1.1",
CONF_MODEL: MODEL_AZ_HT,
CONF_SLEEP_PERIOD: 0,
CONF_GEN: 3,
},
)
entry.add_to_hass(hass)
result = await hass.config_entries.flow.async_init(
DOMAIN,
data=BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL,
context={"source": config_entries.SOURCE_BLUETOOTH},
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "already_configured"
async def test_user_flow_select_ble_unsupported_device(
hass: HomeAssistant,
mock_discovery: AsyncMock,
) -> None:
"""Test selecting an unsupported BLE device aborts before provisioning."""
mock_discovery.return_value = []
await _async_inject_ble_discovery(hass, BLE_DISCOVERY_INFO_UNSUPPORTED_MODEL)
result = await hass.config_entries.flow.async_init(
DOMAIN, context={"source": config_entries.SOURCE_USER}
)
assert result["type"] is FlowResultType.FORM
assert result["step_id"] == "user"
result = await hass.config_entries.flow.async_configure(
result["flow_id"],
{CONF_DEVICE: "CCBA97C2D670"}, # MAC from manufacturer data
)
assert result["type"] is FlowResultType.ABORT
assert result["reason"] == "unsupported_device"
assert result["description_placeholders"] == {"model": "Shelly AZ H&T"}