Skip to content

Commit

Permalink
Add new sensors to Stookwijzer (#131587)
Browse files Browse the repository at this point in the history
  • Loading branch information
frenck authored Nov 25, 2024
1 parent 442a270 commit af29bfc
Show file tree
Hide file tree
Showing 5 changed files with 135 additions and 2 deletions.
2 changes: 2 additions & 0 deletions homeassistant/components/stookwijzer/diagnostics.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,6 @@ async def async_get_config_entry_diagnostics(
client = entry.runtime_data.client
return {
"advice": client.advice,
"air_quality_index": client.lki,
"windspeed_ms": client.windspeed_ms,
}
21 changes: 19 additions & 2 deletions homeassistant/components/stookwijzer/sensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
SensorDeviceClass,
SensorEntity,
SensorEntityDescription,
SensorStateClass,
)
from homeassistant.const import UnitOfSpeed
from homeassistant.core import HomeAssistant
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
Expand All @@ -25,10 +27,25 @@
class StookwijzerSensorDescription(SensorEntityDescription):
"""Class describing Stookwijzer sensor entities."""

value_fn: Callable[[Stookwijzer], str | None]
value_fn: Callable[[Stookwijzer], int | float | str | None]


STOOKWIJZER_SENSORS = [
StookwijzerSensorDescription(
key="windspeed",
native_unit_of_measurement=UnitOfSpeed.METERS_PER_SECOND,
suggested_unit_of_measurement=UnitOfSpeed.BEAUFORT,
device_class=SensorDeviceClass.WIND_SPEED,
suggested_display_precision=0,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda client: client.windspeed_ms,
),
StookwijzerSensorDescription(
key="air_quality_index",
device_class=SensorDeviceClass.AQI,
state_class=SensorStateClass.MEASUREMENT,
value_fn=lambda client: client.lki,
),
StookwijzerSensorDescription(
key="advice",
translation_key="advice",
Expand Down Expand Up @@ -74,6 +91,6 @@ def __init__(
)

@property
def native_value(self) -> str | None:
def native_value(self) -> int | float | str | None:
"""Return the state of the device."""
return self.entity_description.value_fn(self.coordinator.client)
3 changes: 3 additions & 0 deletions tests/components/stookwijzer/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ def mock_stookwijzer() -> Generator[MagicMock]:
)

client = stookwijzer_mock.return_value
client.lki = 2
client.windspeed_ms = 2.5
client.windspeed_bft = 2
client.advice = "code_yellow"

yield stookwijzer_mock
Expand Down
2 changes: 2 additions & 0 deletions tests/components/stookwijzer/snapshots/test_diagnostics.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,7 @@
# name: test_get_diagnostics
dict({
'advice': 'code_yellow',
'air_quality_index': 2,
'windspeed_ms': 2.5,
})
# ---
109 changes: 109 additions & 0 deletions tests/components/stookwijzer/snapshots/test_sensor.ambr
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,112 @@
'state': 'code_yellow',
})
# ---
# name: test_entities[sensor.stookwijzer_air_quality_index-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.stookwijzer_air_quality_index',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
}),
'original_device_class': <SensorDeviceClass.AQI: 'aqi'>,
'original_icon': None,
'original_name': 'Air quality index',
'platform': 'stookwijzer',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '12345_air_quality_index',
'unit_of_measurement': None,
})
# ---
# name: test_entities[sensor.stookwijzer_air_quality_index-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by atlasleefomgeving.nl',
'device_class': 'aqi',
'friendly_name': 'Stookwijzer Air quality index',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'context': <ANY>,
'entity_id': 'sensor.stookwijzer_air_quality_index',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2',
})
# ---
# name: test_entities[sensor.stookwijzer_wind_speed-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
}),
'config_entry_id': <ANY>,
'device_class': None,
'device_id': <ANY>,
'disabled_by': None,
'domain': 'sensor',
'entity_category': None,
'entity_id': 'sensor.stookwijzer_wind_speed',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'options': dict({
'sensor': dict({
'suggested_display_precision': 0,
}),
'sensor.private': dict({
'suggested_unit_of_measurement': <UnitOfSpeed.BEAUFORT: 'Beaufort'>,
}),
}),
'original_device_class': <SensorDeviceClass.WIND_SPEED: 'wind_speed'>,
'original_icon': None,
'original_name': 'Wind speed',
'platform': 'stookwijzer',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': None,
'unique_id': '12345_windspeed',
'unit_of_measurement': <UnitOfSpeed.BEAUFORT: 'Beaufort'>,
})
# ---
# name: test_entities[sensor.stookwijzer_wind_speed-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'attribution': 'Data provided by atlasleefomgeving.nl',
'device_class': 'wind_speed',
'friendly_name': 'Stookwijzer Wind speed',
'state_class': <SensorStateClass.MEASUREMENT: 'measurement'>,
'unit_of_measurement': <UnitOfSpeed.BEAUFORT: 'Beaufort'>,
}),
'context': <ANY>,
'entity_id': 'sensor.stookwijzer_wind_speed',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2.0',
})
# ---

0 comments on commit af29bfc

Please sign in to comment.