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
This commit is contained in:
Simone Chemelli 2025-03-18 11:48:18 +01:00 committed by GitHub
parent 30c19ec373
commit 12f5bd2aea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 103 additions and 5 deletions

View File

@ -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"
},

View File

@ -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 = {

View File

@ -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",

View File

@ -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",

View File

@ -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(