-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add Rabbit Air air quality sensor #172993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,11 @@ | |
| } | ||
| } | ||
| } | ||
| }, | ||
| "sensor": { | ||
| "air_quality": { | ||
| "default": "mdi:air-filter" | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| """Support for Rabbit Air sensors.""" | ||
|
|
||
| from rabbitair import Quality | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| ) | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
| from homeassistant.helpers.typing import StateType | ||
|
|
||
| from .coordinator import RabbitAirConfigEntry, RabbitAirDataUpdateCoordinator | ||
| from .entity import RabbitAirBaseEntity | ||
|
|
||
|
|
||
| def _quality_value(quality: Quality | None) -> StateType: | ||
| """Return the air quality state.""" | ||
| return None if quality is None else quality.name.lower() | ||
|
|
||
|
|
||
| AIR_QUALITY_OPTIONS = [quality.name.lower() for quality in Quality] | ||
|
|
||
| AIR_QUALITY_DESCRIPTION = SensorEntityDescription( | ||
| key="air_quality", | ||
| translation_key="air_quality", | ||
| device_class=SensorDeviceClass.ENUM, | ||
| options=AIR_QUALITY_OPTIONS, | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: RabbitAirConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Rabbit Air sensors.""" | ||
| if entry.runtime_data.data.quality is not None: | ||
| async_add_entities([RabbitAirAirQualitySensor(entry.runtime_data, entry)]) | ||
|
|
||
|
|
||
| class RabbitAirAirQualitySensor(RabbitAirBaseEntity, SensorEntity): | ||
| """Rabbit Air air quality sensor.""" | ||
|
|
||
| entity_description = AIR_QUALITY_DESCRIPTION | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: RabbitAirDataUpdateCoordinator, | ||
| entry: RabbitAirConfigEntry, | ||
| ) -> None: | ||
| """Initialize the entity.""" | ||
| super().__init__(coordinator, entry) | ||
| self._attr_unique_id = f"{entry.unique_id}_{self.entity_description.key}" | ||
|
|
||
| @property | ||
| def native_value(self) -> StateType: | ||
| """Return the air quality state.""" | ||
| return _quality_value(self.coordinator.data.quality) | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| """Test the Rabbit Air sensor platform.""" | ||
|
|
||
| from unittest.mock import patch | ||
|
|
||
| import pytest | ||
| from rabbitair import Quality | ||
|
|
||
| from homeassistant.components.rabbitair.const import DOMAIN | ||
| from homeassistant.const import CONF_ACCESS_TOKEN, CONF_HOST, CONF_MAC | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers import entity_registry as er | ||
|
|
||
| from .test_config_flow import ( | ||
| TEST_HOST, | ||
| TEST_MAC, | ||
| TEST_TITLE, | ||
| TEST_TOKEN, | ||
| TEST_UNIQUE_ID, | ||
| get_mock_state, | ||
| ) | ||
|
|
||
| from tests.common import MockConfigEntry | ||
|
|
||
| pytestmark = pytest.mark.usefixtures("mock_async_zeroconf") | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def mock_config_entry(hass: HomeAssistant) -> MockConfigEntry: | ||
| """Return a mock config entry.""" | ||
| entry = MockConfigEntry( | ||
| domain=DOMAIN, | ||
| data={ | ||
| CONF_HOST: TEST_HOST, | ||
| CONF_ACCESS_TOKEN: TEST_TOKEN, | ||
| CONF_MAC: TEST_MAC, | ||
| }, | ||
| title=TEST_TITLE, | ||
| unique_id=TEST_UNIQUE_ID, | ||
| ) | ||
|
Comment on lines
+30
to
+39
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we make this a test fixture?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. The repeated |
||
| entry.add_to_hass(hass) | ||
| return entry | ||
|
|
||
|
|
||
| async def test_air_quality_sensor( | ||
| hass: HomeAssistant, | ||
| entity_registry: er.EntityRegistry, | ||
| mock_config_entry: MockConfigEntry, | ||
| ) -> None: | ||
| """Test the air quality sensor.""" | ||
| with patch( | ||
| "homeassistant.components.rabbitair.coordinator.Client.get_state", | ||
| return_value=get_mock_state(quality=Quality.High), | ||
| ): | ||
|
Comment on lines
+50
to
+53
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we patch it where we use it?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Updated. The tests now patch |
||
| assert await hass.config_entries.async_setup(mock_config_entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| state = hass.states.get("sensor.rabbit_air_air_quality") | ||
| assert state | ||
| assert state.state == "high" | ||
|
|
||
| registry_entry = entity_registry.async_get("sensor.rabbit_air_air_quality") | ||
| assert registry_entry | ||
| assert registry_entry.unique_id == f"{TEST_UNIQUE_ID}_air_quality" | ||
|
|
||
|
|
||
| async def test_no_air_quality_sensor_when_quality_is_none( | ||
| hass: HomeAssistant, mock_config_entry: MockConfigEntry | ||
| ) -> None: | ||
| """Test the air quality sensor is not created when quality is unavailable.""" | ||
| with patch( | ||
| "homeassistant.components.rabbitair.coordinator.Client.get_state", | ||
| return_value=get_mock_state(quality=None), | ||
| ): | ||
| assert await hass.config_entries.async_setup(mock_config_entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert hass.states.get("sensor.rabbit_air_air_quality") is None | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@joostlek - I’m not sure we can definitively know which current and future Rabbit Air models support
quality, though my assumption is that they all will do. Given that, would you prefer that we always create the air quality sensor and let it reportunknownwhen the device returnsquality=None, or keep the current setup-time check and only create the entity when the first fetched state includes a quality value?