Use enum device class in Netatmo health index sensor (#115409)

This commit is contained in:
Joost Lekkerkerker 2024-04-12 08:40:27 +02:00 committed by GitHub
parent 6cc2b1e10a
commit adafdb2b2d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 110 additions and 24 deletions

View file

@ -83,15 +83,12 @@ def process_health(health: StateType) -> str | None:
"""Process health index and return string for display."""
if not isinstance(health, int):
return None
if health == 0:
return "Healthy"
if health == 1:
return "Fine"
if health == 2:
return "Fair"
if health == 3:
return "Poor"
return "Unhealthy"
return {
0: "healthy",
1: "fine",
2: "fair",
3: "poor",
}.get(health, "unhealthy")
def process_rf(strength: StateType) -> str | None:
@ -274,6 +271,8 @@ SENSOR_TYPES: tuple[NetatmoSensorEntityDescription, ...] = (
NetatmoSensorEntityDescription(
key="health_idx",
netatmo_name="health_idx",
device_class=SensorDeviceClass.ENUM,
options=["healthy", "fine", "fair", "poor", "unhealthy"],
value_fn=process_health,
),
NetatmoSensorEntityDescription(

View file

@ -229,7 +229,14 @@
"name": "Wi-Fi"
},
"health_idx": {
"name": "Health index"
"name": "Health index",
"state": {
"healthy": "Healthy",
"fine": "Fine",
"fair": "Fair",
"poor": "Poor",
"unhealthy": "Unhealthy"
}
}
}
}

View file

@ -118,7 +118,15 @@
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'capabilities': dict({
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
@ -135,7 +143,7 @@
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Health index',
'platform': 'netatmo',
@ -150,16 +158,24 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Netatmo',
'device_class': 'enum',
'friendly_name': 'Baby Bedroom Health index',
'latitude': 13.377726,
'longitude': 52.516263,
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'context': <ANY>,
'entity_id': 'sensor.baby_bedroom_health_index',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'Fine',
'state': 'fine',
})
# ---
# name: test_entity[sensor.baby_bedroom_humidity-entry]
@ -638,7 +654,15 @@
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'capabilities': dict({
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
@ -655,7 +679,7 @@
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Health index',
'platform': 'netatmo',
@ -670,7 +694,15 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Netatmo',
'device_class': 'enum',
'friendly_name': 'Bedroom Health index',
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'context': <ANY>,
'entity_id': 'sensor.bedroom_health_index',
@ -2845,7 +2877,15 @@
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'capabilities': dict({
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
@ -2862,7 +2902,7 @@
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Health index',
'platform': 'netatmo',
@ -2877,9 +2917,17 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Netatmo',
'device_class': 'enum',
'friendly_name': 'Kitchen Health index',
'latitude': 13.377726,
'longitude': 52.516263,
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'context': <ANY>,
'entity_id': 'sensor.kitchen_health_index',
@ -3916,7 +3964,15 @@
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'capabilities': dict({
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
@ -3933,7 +3989,7 @@
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Health index',
'platform': 'netatmo',
@ -3948,9 +4004,17 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Netatmo',
'device_class': 'enum',
'friendly_name': 'Livingroom Health index',
'latitude': 13.377726,
'longitude': 52.516263,
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'context': <ANY>,
'entity_id': 'sensor.livingroom_health_index',
@ -4440,7 +4504,15 @@
'aliases': set({
}),
'area_id': None,
'capabilities': None,
'capabilities': dict({
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
@ -4457,7 +4529,7 @@
'name': None,
'options': dict({
}),
'original_device_class': None,
'original_device_class': <SensorDeviceClass.ENUM: 'enum'>,
'original_icon': None,
'original_name': 'Health index',
'platform': 'netatmo',
@ -4472,16 +4544,24 @@
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by Netatmo',
'device_class': 'enum',
'friendly_name': 'Parents Bedroom Health index',
'latitude': 13.377726,
'longitude': 52.516263,
'options': list([
'healthy',
'fine',
'fair',
'poor',
'unhealthy',
]),
}),
'context': <ANY>,
'entity_id': 'sensor.parents_bedroom_health_index',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'Fine',
'state': 'fine',
})
# ---
# name: test_entity[sensor.parents_bedroom_humidity-entry]

View file

@ -136,7 +136,7 @@ async def test_process_rf(strength: int, expected: str) -> None:
@pytest.mark.parametrize(
("health", "expected"),
[(4, "Unhealthy"), (3, "Poor"), (2, "Fair"), (1, "Fine"), (0, "Healthy")],
[(4, "unhealthy"), (3, "poor"), (2, "fair"), (1, "fine"), (0, "healthy")],
)
async def test_process_health(health: int, expected: str) -> None:
"""Test health index translation."""
@ -195,7 +195,7 @@ async def test_process_health(health: int, expected: str) -> None:
(
"12:34:56:26:68:92-health_idx",
"baby_bedroom_health",
"Fine",
"fine",
),
(
"12:34:56:26:68:92-wifi_status",