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>
This commit is contained in:
Maciej Bieniek 2024-02-12 20:13:11 +01:00 committed by GitHub
parent 32e3f6c5a7
commit 2bd64dba6b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 55 additions and 10 deletions

View file

@ -957,11 +957,15 @@ RPC_SENSORS: Final = {
name="Analog input", name="Analog input",
native_unit_of_measurement=PERCENTAGE, native_unit_of_measurement=PERCENTAGE,
state_class=SensorStateClass.MEASUREMENT, state_class=SensorStateClass.MEASUREMENT,
removal_condition=lambda config, _status, key: (config[key]["enable"] is False),
), ),
"analoginput_xpercent": RpcSensorDescription( "analoginput_xpercent": RpcSensorDescription(
key="input", key="input",
sub_key="xpercent", sub_key="xpercent",
name="Analog value", name="Analog value",
removal_condition=lambda config, status, key: (
config[key]["enable"] is False or status[key].get("xpercent") is None
),
), ),
"pulse_counter": RpcSensorDescription( "pulse_counter": RpcSensorDescription(
key="input", key="input",

View file

@ -165,12 +165,8 @@ MOCK_BLOCKS = [
MOCK_CONFIG = { MOCK_CONFIG = {
"input:0": {"id": 0, "name": "Test name input 0", "type": "button"}, "input:0": {"id": 0, "name": "Test name input 0", "type": "button"},
"input:2": { "input:1": {"id": 1, "type": "analog", "enable": True},
"id": 2, "input:2": {"id": 2, "name": "Gas", "type": "count", "enable": True},
"name": "Gas",
"type": "count",
"enable": True,
},
"light:0": {"name": "test light_0"}, "light:0": {"name": "test light_0"},
"switch:0": {"name": "test switch_0"}, "switch:0": {"name": "test switch_0"},
"cover:0": {"name": "test cover_0"}, "cover:0": {"name": "test cover_0"},
@ -223,7 +219,8 @@ MOCK_STATUS_COAP = {
MOCK_STATUS_RPC = { MOCK_STATUS_RPC = {
"switch:0": {"output": True}, "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}}, "input:2": {"id": 2, "counts": {"total": 56174, "xtotal": 561.74}},
"light:0": {"output": True, "brightness": 53.0}, "light:0": {"output": True, "brightness": 53.0},
"cloud": {"connected": False}, "cloud": {"connected": False},

View file

@ -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 hass: HomeAssistant, mock_rpc_device: Mock, entity_registry: EntityRegistry
) -> None: ) -> None:
"""Test RPC analog input xpercent sensor.""" """Test RPC analog input xpercent sensor."""
entity_id = f"{SENSOR_DOMAIN}.test_name_input_0_analog_value"
await init_integration(hass, 2) 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" assert hass.states.get(entity_id).state == "8.9"
entry = entity_registry.async_get(entity_id) entry = entity_registry.async_get(entity_id)
assert entry 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( async def test_rpc_pulse_counter_sensors(