From 2bd64dba6ba578fc337f671b3c7f8653111585bd Mon Sep 17 00:00:00 2001 From: Maciej Bieniek Date: Mon, 12 Feb 2024 20:13:11 +0100 Subject: [PATCH] Add `removal_condition` for Shelly analog input sensors (#110331) * Add remove condition for analog input sensors * xpercent key is not present in the payload if it has not been configured --------- Co-authored-by: Maciej Bieniek <478555+bieniu@users.noreply.github.com> --- homeassistant/components/shelly/sensor.py | 4 ++ tests/components/shelly/conftest.py | 11 ++--- tests/components/shelly/test_sensor.py | 50 +++++++++++++++++++++-- 3 files changed, 55 insertions(+), 10 deletions(-) diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index 49c4befb2cf..399ae27e853 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -957,11 +957,15 @@ RPC_SENSORS: Final = { name="Analog input", native_unit_of_measurement=PERCENTAGE, state_class=SensorStateClass.MEASUREMENT, + removal_condition=lambda config, _status, key: (config[key]["enable"] is False), ), "analoginput_xpercent": RpcSensorDescription( key="input", sub_key="xpercent", name="Analog value", + removal_condition=lambda config, status, key: ( + config[key]["enable"] is False or status[key].get("xpercent") is None + ), ), "pulse_counter": RpcSensorDescription( key="input", diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index 0d73b3823ff..1c09f870ae4 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -165,12 +165,8 @@ MOCK_BLOCKS = [ MOCK_CONFIG = { "input:0": {"id": 0, "name": "Test name input 0", "type": "button"}, - "input:2": { - "id": 2, - "name": "Gas", - "type": "count", - "enable": True, - }, + "input:1": {"id": 1, "type": "analog", "enable": True}, + "input:2": {"id": 2, "name": "Gas", "type": "count", "enable": True}, "light:0": {"name": "test light_0"}, "switch:0": {"name": "test switch_0"}, "cover:0": {"name": "test cover_0"}, @@ -223,7 +219,8 @@ MOCK_STATUS_COAP = { MOCK_STATUS_RPC = { "switch:0": {"output": True}, - "input:0": {"id": 0, "state": None, "xpercent": 8.9}, + "input:0": {"id": 0, "state": None}, + "input:1": {"id": 1, "percent": 89, "xpercent": 8.9}, "input:2": {"id": 2, "counts": {"total": 56174, "xtotal": 561.74}}, "light:0": {"output": True, "brightness": 53.0}, "cloud": {"connected": False}, diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 46d26a1c218..3a3afa27e94 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -638,18 +638,62 @@ async def test_block_sleeping_update_entity_service( ) -async def test_rpc_analog_input_xpercent_sensor( +async def test_rpc_analog_input_sensors( hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry ) -> None: """Test RPC analog input xpercent sensor.""" - entity_id = f"{SENSOR_DOMAIN}.test_name_input_0_analog_value" await init_integration(hass, 2) + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_input" + assert hass.states.get(entity_id).state == "89" + + entry = entity_registry.async_get(entity_id) + assert entry + assert entry.unique_id == "123456789ABC-input:1-analoginput" + + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_value" assert hass.states.get(entity_id).state == "8.9" entry = entity_registry.async_get(entity_id) assert entry - assert entry.unique_id == "123456789ABC-input:0-analoginput_xpercent" + assert entry.unique_id == "123456789ABC-input:1-analoginput_xpercent" + + +async def test_rpc_disabled_analog_input_sensors( + hass: HomeAssistant, mock_rpc_device: Mock, monkeypatch: pytest.MonkeyPatch +) -> None: + """Test RPC disabled counter sensor.""" + new_config = deepcopy(mock_rpc_device.config) + new_config["input:1"]["enable"] = False + monkeypatch.setattr(mock_rpc_device, "config", new_config) + + await init_integration(hass, 2) + + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_input" + assert hass.states.get(entity_id) is None + + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_value" + assert hass.states.get(entity_id) is None + + +async def test_rpc_disabled_xpercent( + hass: HomeAssistant, mock_rpc_device: Mock, monkeypatch: pytest.MonkeyPatch +) -> None: + """Test RPC empty xpercent value.""" + mutate_rpc_device_status( + monkeypatch, + mock_rpc_device, + "input:1", + "xpercent", + None, + ) + await init_integration(hass, 2) + + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_input" + assert hass.states.get(entity_id).state == "89" + + entity_id = f"{SENSOR_DOMAIN}.test_name_analog_value" + assert hass.states.get(entity_id) is None async def test_rpc_pulse_counter_sensors(