Disable both option in Airgradient select (#118702)

This commit is contained in:
Joost Lekkerkerker 2024-06-03 15:49:51 +02:00 committed by GitHub
parent 6d02453c8a
commit c32eb97ac0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 61 additions and 19 deletions

View File

@ -22,7 +22,7 @@ from .entity import AirGradientEntity
class AirGradientSelectEntityDescription(SelectEntityDescription): class AirGradientSelectEntityDescription(SelectEntityDescription):
"""Describes AirGradient select entity.""" """Describes AirGradient select entity."""
value_fn: Callable[[Config], str] value_fn: Callable[[Config], str | None]
set_value_fn: Callable[[AirGradientClient, str], Awaitable[None]] set_value_fn: Callable[[AirGradientClient, str], Awaitable[None]]
requires_display: bool = False requires_display: bool = False
@ -30,9 +30,11 @@ class AirGradientSelectEntityDescription(SelectEntityDescription):
CONFIG_CONTROL_ENTITY = AirGradientSelectEntityDescription( CONFIG_CONTROL_ENTITY = AirGradientSelectEntityDescription(
key="configuration_control", key="configuration_control",
translation_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, 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( set_value_fn=lambda client, value: client.set_configuration_control(
ConfigurationControl(value) ConfigurationControl(value)
), ),
@ -96,7 +98,7 @@ class AirGradientSelect(AirGradientEntity, SelectEntity):
self._attr_unique_id = f"{coordinator.serial_number}-{description.key}" self._attr_unique_id = f"{coordinator.serial_number}-{description.key}"
@property @property
def current_option(self) -> str: def current_option(self) -> str | None:
"""Return the state of the select.""" """Return the state of the select."""
return self.entity_description.value_fn(self.coordinator.data) return self.entity_description.value_fn(self.coordinator.data)

View File

@ -42,11 +42,33 @@ def mock_airgradient_client() -> Generator[AsyncMock, None, None]:
load_fixture("current_measures.json", DOMAIN) load_fixture("current_measures.json", DOMAIN)
) )
client.get_config.return_value = Config.from_json( client.get_config.return_value = Config.from_json(
load_fixture("get_config.json", DOMAIN) load_fixture("get_config_local.json", DOMAIN)
) )
yield client 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 @pytest.fixture
def mock_config_entry() -> MockConfigEntry: def mock_config_entry() -> MockConfigEntry:
"""Mock a config entry.""" """Mock a config entry."""

View File

@ -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
}

View File

@ -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
}

View File

@ -8,7 +8,6 @@
'options': list([ 'options': list([
'cloud', 'cloud',
'local', 'local',
'both',
]), ]),
}), }),
'config_entry_id': <ANY>, 'config_entry_id': <ANY>,
@ -45,7 +44,6 @@
'options': list([ 'options': list([
'cloud', 'cloud',
'local', 'local',
'both',
]), ]),
}), }),
'context': <ANY>, 'context': <ANY>,
@ -53,7 +51,7 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'both', 'state': 'local',
}) })
# --- # ---
# name: test_all_entities[select.airgradient_display_temperature_unit-entry] # name: test_all_entities[select.airgradient_display_temperature_unit-entry]
@ -120,7 +118,6 @@
'options': list([ 'options': list([
'cloud', 'cloud',
'local', 'local',
'both',
]), ]),
}), }),
'config_entry_id': <ANY>, 'config_entry_id': <ANY>,
@ -157,7 +154,6 @@
'options': list([ 'options': list([
'cloud', 'cloud',
'local', 'local',
'both',
]), ]),
}), }),
'context': <ANY>, 'context': <ANY>,
@ -165,6 +161,6 @@
'last_changed': <ANY>, 'last_changed': <ANY>,
'last_reported': <ANY>, 'last_reported': <ANY>,
'last_updated': <ANY>, 'last_updated': <ANY>,
'state': 'both', 'state': 'local',
}) })
# --- # ---

View File

@ -77,16 +77,12 @@ async def test_setting_value(
async def test_setting_protected_value( async def test_setting_protected_value(
hass: HomeAssistant, hass: HomeAssistant,
mock_airgradient_client: AsyncMock, mock_cloud_airgradient_client: AsyncMock,
mock_config_entry: MockConfigEntry, mock_config_entry: MockConfigEntry,
) -> None: ) -> None:
"""Test setting protected value.""" """Test setting protected value."""
await setup_integration(hass, mock_config_entry) await setup_integration(hass, mock_config_entry)
mock_airgradient_client.get_config.return_value.configuration_control = (
ConfigurationControl.CLOUD
)
with pytest.raises(ServiceValidationError): with pytest.raises(ServiceValidationError):
await hass.services.async_call( await hass.services.async_call(
SELECT_DOMAIN, SELECT_DOMAIN,
@ -97,9 +93,9 @@ async def test_setting_protected_value(
}, },
blocking=True, 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 ConfigurationControl.LOCAL
) )
@ -112,4 +108,4 @@ async def test_setting_protected_value(
}, },
blocking=True, 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")