diff --git a/homeassistant/components/airgradient/select.py b/homeassistant/components/airgradient/select.py index 41b5a48c686..5e13ee1d0bb 100644 --- a/homeassistant/components/airgradient/select.py +++ b/homeassistant/components/airgradient/select.py @@ -22,7 +22,7 @@ from .entity import AirGradientEntity class AirGradientSelectEntityDescription(SelectEntityDescription): """Describes AirGradient select entity.""" - value_fn: Callable[[Config], str] + value_fn: Callable[[Config], str | None] set_value_fn: Callable[[AirGradientClient, str], Awaitable[None]] requires_display: bool = False @@ -30,9 +30,11 @@ class AirGradientSelectEntityDescription(SelectEntityDescription): CONFIG_CONTROL_ENTITY = AirGradientSelectEntityDescription( key="configuration_control", translation_key="configuration_control", - options=[x.value for x in ConfigurationControl], + options=[ConfigurationControl.CLOUD.value, ConfigurationControl.LOCAL.value], entity_category=EntityCategory.CONFIG, - value_fn=lambda config: config.configuration_control, + value_fn=lambda config: config.configuration_control + if config.configuration_control is not ConfigurationControl.BOTH + else None, set_value_fn=lambda client, value: client.set_configuration_control( ConfigurationControl(value) ), @@ -96,7 +98,7 @@ class AirGradientSelect(AirGradientEntity, SelectEntity): self._attr_unique_id = f"{coordinator.serial_number}-{description.key}" @property - def current_option(self) -> str: + def current_option(self) -> str | None: """Return the state of the select.""" return self.entity_description.value_fn(self.coordinator.data) diff --git a/tests/components/airgradient/conftest.py b/tests/components/airgradient/conftest.py index aa2c1e783a4..d2495c11a79 100644 --- a/tests/components/airgradient/conftest.py +++ b/tests/components/airgradient/conftest.py @@ -42,11 +42,33 @@ def mock_airgradient_client() -> Generator[AsyncMock, None, None]: load_fixture("current_measures.json", DOMAIN) ) client.get_config.return_value = Config.from_json( - load_fixture("get_config.json", DOMAIN) + load_fixture("get_config_local.json", DOMAIN) ) yield client +@pytest.fixture +def mock_new_airgradient_client( + mock_airgradient_client: AsyncMock, +) -> Generator[AsyncMock, None, None]: + """Mock a new AirGradient client.""" + mock_airgradient_client.get_config.return_value = Config.from_json( + load_fixture("get_config.json", DOMAIN) + ) + return mock_airgradient_client + + +@pytest.fixture +def mock_cloud_airgradient_client( + mock_airgradient_client: AsyncMock, +) -> Generator[AsyncMock, None, None]: + """Mock a new AirGradient client.""" + mock_airgradient_client.get_config.return_value = Config.from_json( + load_fixture("get_config_cloud.json", DOMAIN) + ) + return mock_airgradient_client + + @pytest.fixture def mock_config_entry() -> MockConfigEntry: """Mock a config entry.""" diff --git a/tests/components/airgradient/fixtures/get_config_cloud.json b/tests/components/airgradient/fixtures/get_config_cloud.json new file mode 100644 index 00000000000..a5f27957e04 --- /dev/null +++ b/tests/components/airgradient/fixtures/get_config_cloud.json @@ -0,0 +1,13 @@ +{ + "country": "DE", + "pmStandard": "ugm3", + "ledBarMode": "co2", + "displayMode": "on", + "abcDays": 8, + "tvocLearningOffset": 12, + "noxLearningOffset": 12, + "mqttBrokerUrl": "", + "temperatureUnit": "c", + "configurationControl": "cloud", + "postDataToAirGradient": true +} diff --git a/tests/components/airgradient/fixtures/get_config_local.json b/tests/components/airgradient/fixtures/get_config_local.json new file mode 100644 index 00000000000..09e0e982053 --- /dev/null +++ b/tests/components/airgradient/fixtures/get_config_local.json @@ -0,0 +1,13 @@ +{ + "country": "DE", + "pmStandard": "ugm3", + "ledBarMode": "co2", + "displayMode": "on", + "abcDays": 8, + "tvocLearningOffset": 12, + "noxLearningOffset": 12, + "mqttBrokerUrl": "", + "temperatureUnit": "c", + "configurationControl": "local", + "postDataToAirGradient": true +} diff --git a/tests/components/airgradient/snapshots/test_select.ambr b/tests/components/airgradient/snapshots/test_select.ambr index 986e3c6ebb8..fb201b88204 100644 --- a/tests/components/airgradient/snapshots/test_select.ambr +++ b/tests/components/airgradient/snapshots/test_select.ambr @@ -8,7 +8,6 @@ 'options': list([ 'cloud', 'local', - 'both', ]), }), 'config_entry_id': , @@ -45,7 +44,6 @@ 'options': list([ 'cloud', 'local', - 'both', ]), }), 'context': , @@ -53,7 +51,7 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'both', + 'state': 'local', }) # --- # name: test_all_entities[select.airgradient_display_temperature_unit-entry] @@ -120,7 +118,6 @@ 'options': list([ 'cloud', 'local', - 'both', ]), }), 'config_entry_id': , @@ -157,7 +154,6 @@ 'options': list([ 'cloud', 'local', - 'both', ]), }), 'context': , @@ -165,6 +161,6 @@ 'last_changed': , 'last_reported': , 'last_updated': , - 'state': 'both', + 'state': 'local', }) # --- diff --git a/tests/components/airgradient/test_select.py b/tests/components/airgradient/test_select.py index 2988a5918ad..986295bd245 100644 --- a/tests/components/airgradient/test_select.py +++ b/tests/components/airgradient/test_select.py @@ -77,16 +77,12 @@ async def test_setting_value( async def test_setting_protected_value( hass: HomeAssistant, - mock_airgradient_client: AsyncMock, + mock_cloud_airgradient_client: AsyncMock, mock_config_entry: MockConfigEntry, ) -> None: """Test setting protected value.""" await setup_integration(hass, mock_config_entry) - mock_airgradient_client.get_config.return_value.configuration_control = ( - ConfigurationControl.CLOUD - ) - with pytest.raises(ServiceValidationError): await hass.services.async_call( SELECT_DOMAIN, @@ -97,9 +93,9 @@ async def test_setting_protected_value( }, blocking=True, ) - mock_airgradient_client.set_temperature_unit.assert_not_called() + mock_cloud_airgradient_client.set_temperature_unit.assert_not_called() - mock_airgradient_client.get_config.return_value.configuration_control = ( + mock_cloud_airgradient_client.get_config.return_value.configuration_control = ( ConfigurationControl.LOCAL ) @@ -112,4 +108,4 @@ async def test_setting_protected_value( }, blocking=True, ) - mock_airgradient_client.set_temperature_unit.assert_called_once_with("c") + mock_cloud_airgradient_client.set_temperature_unit.assert_called_once_with("c")