diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 399ae27e853dd..b88b6886b8480 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -51,7 +51,11 @@ async_setup_entry_rest, async_setup_entry_rpc, ) -from .utils import get_device_entry_gen, get_device_uptime +from .utils import ( + get_device_entry_gen, + get_device_uptime, + is_rpc_wifi_stations_disabled, +) @dataclass(frozen=True) @@ -905,9 +909,7 @@ class RestSensorDescription(RestEntityDescription, SensorEntityDescription): device_class=SensorDeviceClass.SIGNAL_STRENGTH, state_class=SensorStateClass.MEASUREMENT, entity_registry_enabled_default=False, - removal_condition=lambda config, _status, key: ( - config[key]["sta"]["enable"] is False - ), + removal_condition=is_rpc_wifi_stations_disabled, entity_category=EntityCategory.DIAGNOSTIC, use_polling_coordinator=True, ), diff --git a/homeassistant/components/shelly/utils.py b/homeassistant/components/shelly/utils.py index 652b6cf99ed4c..9389f4e150790 100644 --- a/homeassistant/components/shelly/utils.py +++ b/homeassistant/components/shelly/utils.py @@ -452,3 +452,13 @@ def async_create_issue_unsupported_firmware( "ip_address": entry.data["host"], }, ) + + +def is_rpc_wifi_stations_disabled( + config: dict[str, Any], _status: dict[str, Any], key: str +) -> bool: + """Return true if rpc all WiFi stations are disabled.""" + if config[key]["sta"]["enable"] is True or config[key]["sta1"]["enable"] is True: + return False + + return True diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 72a587df5799a..1d4a00f34ca38 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -179,7 +179,7 @@ def mock_white_light_set_state( "ui_data": {}, "device": {"name": "Test name"}, }, - "wifi": {"sta": {"enable": True}}, + "wifi": {"sta": {"enable": True}, "sta1": {"enable": False}}, } MOCK_SHELLY_COAP = { diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 3a3afa27e94b5..03bcc545d1598 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -31,6 +31,7 @@ from homeassistant.setup import async_setup_component from . import ( + get_entity_state, init_integration, mock_polling_rpc_update, mock_rest_update, @@ -353,6 +354,32 @@ async def test_rpc_sensor( assert hass.states.get(entity_id).state == STATE_UNKNOWN +async def test_rpc_rssi_sensor_removal( + hass: HomeAssistant, + mock_rpc_device: Mock, + monkeypatch: pytest.MonkeyPatch, + entity_registry_enabled_by_default: None, +) -> None: + """Test RPC RSSI sensor removal if no WiFi stations enabled.""" + entity_id = f"{SENSOR_DOMAIN}.test_name_rssi" + entry = await init_integration(hass, 2) + + # WiFi1 enabled, do not remove sensor + assert get_entity_state(hass, entity_id) == "-63" + + # WiFi1 & WiFi2 disabled - remove sensor + monkeypatch.setitem(mock_rpc_device.config["wifi"]["sta"], "enable", False) + await hass.config_entries.async_reload(entry.entry_id) + await hass.async_block_till_done() + assert hass.states.get(entity_id) is None + + # WiFi2 enabled, do not remove sensor + monkeypatch.setitem(mock_rpc_device.config["wifi"]["sta1"], "enable", True) + await hass.config_entries.async_reload(entry.entry_id) + await hass.async_block_till_done() + assert get_entity_state(hass, entity_id) == "-63" + + async def test_rpc_illuminance_sensor( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry ) -> None: