From 12f5bd2aea2430d2af37d6717a725766408790f8 Mon Sep 17 00:00:00 2001 From: Simone Chemelli Date: Tue, 18 Mar 2025 11:48:18 +0100 Subject: [PATCH] Add dedicated sensors for extra_state_attributes in Shelly integration (#140793) * Add dedicated sensors for extra_state_attributes in Shelly integration * add tests * apply review comment * fix text syntax * add gas test * update strings * add icons --- homeassistant/components/shelly/icons.json | 6 +++ homeassistant/components/shelly/sensor.py | 22 ++++++++++ homeassistant/components/shelly/strings.json | 36 +++++++++++++++++ tests/components/shelly/conftest.py | 2 + tests/components/shelly/test_sensor.py | 42 +++++++++++++++++--- 5 files changed, 103 insertions(+), 5 deletions(-) diff --git a/homeassistant/components/shelly/icons.json b/homeassistant/components/shelly/icons.json index f93abf6b854..08b269a73c5 100644 --- a/homeassistant/components/shelly/icons.json +++ b/homeassistant/components/shelly/icons.json @@ -23,12 +23,18 @@ "gas_concentration": { "default": "mdi:gauge" }, + "gas_detected": { + "default": "mdi:gas-burner" + }, "lamp_life": { "default": "mdi:progress-wrench" }, "operation": { "default": "mdi:cog-transfer" }, + "self_test": { + "default": "mdi:progress-wrench" + }, "tilt": { "default": "mdi:angle-acute" }, diff --git a/homeassistant/components/shelly/sensor.py b/homeassistant/components/shelly/sensor.py index f2c858aeb84..b6820921b4f 100644 --- a/homeassistant/components/shelly/sensor.py +++ b/homeassistant/components/shelly/sensor.py @@ -397,6 +397,28 @@ SENSORS: dict[tuple[str, str], BlockSensorDescription] = { entity_category=EntityCategory.DIAGNOSTIC, removal_condition=lambda _, block: block.valve == "not_connected", ), + ("sensor", "gas"): BlockSensorDescription( + key="sensor|gas", + name="Gas detected", + translation_key="gas_detected", + device_class=SensorDeviceClass.ENUM, + options=[ + "none", + "mild", + "heavy", + "test", + ], + value=lambda value: None if value == "unknown" else value, + entity_category=EntityCategory.DIAGNOSTIC, + ), + ("sensor", "selfTest"): BlockSensorDescription( + key="sensor|selfTest", + name="Self test", + translation_key="self_test", + device_class=SensorDeviceClass.ENUM, + options=["not_completed", "completed", "running", "pending"], + entity_category=EntityCategory.DIAGNOSTIC, + ), } REST_SENSORS: Final = { diff --git a/homeassistant/components/shelly/strings.json b/homeassistant/components/shelly/strings.json index cc511c93afe..ba9a8492194 100644 --- a/homeassistant/components/shelly/strings.json +++ b/homeassistant/components/shelly/strings.json @@ -138,6 +138,24 @@ } }, "sensor": { + "gas_detected": { + "state": { + "none": "None", + "mild": "Mild", + "heavy": "Heavy", + "test": "Test" + }, + "state_attributes": { + "options": { + "state": { + "none": "[%key:component::shelly::entity::sensor::gas_detected::state::none%]", + "mild": "[%key:component::shelly::entity::sensor::gas_detected::state::mild%]", + "heavy": "[%key:component::shelly::entity::sensor::gas_detected::state::heavy%]", + "test": "[%key:component::shelly::entity::sensor::gas_detected::state::test%]" + } + } + } + }, "operation": { "state": { "warmup": "Warm-up", @@ -155,6 +173,24 @@ } } }, + "self_test": { + "state": { + "not_completed": "Not completed", + "completed": "Completed", + "running": "Running", + "pending": "Pending" + }, + "state_attributes": { + "options": { + "state": { + "not_completed": "[%key:component::shelly::entity::sensor::self_test::state::not_completed%]", + "completed": "[%key:component::shelly::entity::sensor::self_test::state::completed%]", + "running": "[%key:component::shelly::entity::sensor::self_test::state::running%]", + "pending": "[%key:component::shelly::entity::sensor::self_test::state::pending%]" + } + } + } + }, "valve_status": { "state": { "checking": "Checking", diff --git a/tests/components/shelly/conftest.py b/tests/components/shelly/conftest.py index c68d52526c5..8030df6e473 100644 --- a/tests/components/shelly/conftest.py +++ b/tests/components/shelly/conftest.py @@ -142,12 +142,14 @@ MOCK_BLOCKS = [ "gas": "mild", "motionActive": 1, "sensorOp": "normal", + "selfTest": "pending", }, channel="0", motion=0, temp=22.1, gas="mild", sensorOp="normal", + selfTest="pending", targetTemp=4, description="sensor_0", type="sensor", diff --git a/tests/components/shelly/test_sensor.py b/tests/components/shelly/test_sensor.py index 00db4ade8ac..5c1f03de3e8 100644 --- a/tests/components/shelly/test_sensor.py +++ b/tests/components/shelly/test_sensor.py @@ -346,13 +346,44 @@ async def test_block_sensor_without_value( @pytest.mark.parametrize( - ("entity", "initial_state", "block_id", "attribute", "value"), + ("entity", "initial_state", "block_id", "attribute", "value", "final_value"), [ - ("test_name_battery", "98", DEVICE_BLOCK_ID, "battery", None), - ("test_name_operation", "normal", SENSOR_BLOCK_ID, "sensorOp", "unknown"), + ("test_name_battery", "98", DEVICE_BLOCK_ID, "battery", None, STATE_UNKNOWN), + ( + "test_name_operation", + "normal", + SENSOR_BLOCK_ID, + "sensorOp", + None, + STATE_UNKNOWN, + ), + ( + "test_name_operation", + "normal", + SENSOR_BLOCK_ID, + "sensorOp", + "normal", + "normal", + ), + ( + "test_name_self_test", + "pending", + SENSOR_BLOCK_ID, + "selfTest", + "completed", + "completed", + ), + ( + "test_name_gas_detected", + "mild", + SENSOR_BLOCK_ID, + "gas", + "heavy", + "heavy", + ), ], ) -async def test_block_sensor_unknown_value( +async def test_block_sensor_values( hass: HomeAssistant, mock_block_device: Mock, monkeypatch: pytest.MonkeyPatch, @@ -361,6 +392,7 @@ async def test_block_sensor_unknown_value( block_id: int, attribute: str, value: str | None, + final_value: str, ) -> None: """Test block sensor unknown value.""" entity_id = f"{SENSOR_DOMAIN}.{entity}" @@ -371,7 +403,7 @@ async def test_block_sensor_unknown_value( monkeypatch.setattr(mock_block_device.blocks[block_id], attribute, value) mock_block_device.mock_update() - assert hass.states.get(entity_id).state == STATE_UNKNOWN + assert hass.states.get(entity_id).state == final_value @pytest.mark.parametrize(