-
-
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 2 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,63 @@ | ||
| """Support for Rabbit Air sensors.""" | ||
|
|
||
| from rabbitair import Quality | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| ) | ||
| from homeassistant.core import HomeAssistant, callback | ||
| 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)]) | ||
|
Comment on lines
+39
to
+40
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. @joostlek - I’m not sure we can definitively know which current and future Rabbit Air models support |
||
|
|
||
|
|
||
| class RabbitAirAirQualitySensor(RabbitAirBaseEntity, SensorEntity): | ||
| """Rabbit Air air quality sensor.""" | ||
|
|
||
| entity_description = AIR_QUALITY_DESCRIPTION | ||
| _attr_has_entity_name = True | ||
|
|
||
| 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}" | ||
| self._attr_native_value = _quality_value(coordinator.data.quality) | ||
|
|
||
| @callback | ||
| def _handle_coordinator_update(self) -> None: | ||
| """Handle updated data from the coordinator.""" | ||
| self._attr_native_value = _quality_value(self.coordinator.data.quality) | ||
| super()._handle_coordinator_update() | ||
|
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. Instead I'd implement the
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 air quality sensor now implements |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| """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 | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("mock_async_zeroconf") | ||
| async def test_air_quality_sensor( | ||
| hass: HomeAssistant, entity_registry: er.EntityRegistry | ||
| ) -> None: | ||
|
MagikalUnicorn marked this conversation as resolved.
Outdated
|
||
| """Test the air quality sensor.""" | ||
| 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) | ||
|
|
||
| with patch( | ||
| "rabbitair.UdpClient.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(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" | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("mock_async_zeroconf") | ||
| async def test_no_air_quality_sensor_when_quality_is_none(hass: HomeAssistant) -> None: | ||
| """Test the air quality sensor is not created when quality is unavailable.""" | ||
| 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, | ||
| ) | ||
| entry.add_to_hass(hass) | ||
|
|
||
| with patch( | ||
| "rabbitair.UdpClient.get_state", | ||
| return_value=get_mock_state(quality=None), | ||
| ): | ||
| assert await hass.config_entries.async_setup(entry.entry_id) | ||
| await hass.async_block_till_done() | ||
|
|
||
| assert hass.states.get("sensor.rabbit_air_air_quality") is None | ||
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.
Hmm, shouldn't we set
_attr_has_entity_name = Trueat the base entity and then set_attr_name = Nonehere?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.
Updated.
_attr_has_entity_name = Trueis now set onRabbitAirBaseEntity, and the fan entity sets_attr_name = Noneto preserve the primary device entity naming pattern.