-
-
Notifications
You must be signed in to change notification settings - Fork 37.6k
Add number entity to Airobot integration #159595
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
Merged
joostlek
merged 6 commits into
home-assistant:dev
from
mettolen:airobot_add_number_entity
Dec 23, 2025
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd2edb3
Add number entity to Airobot integration
mettolen 0e7ff26
Merge branch 'dev' into airobot_add_number_entity
mettolen eefdd45
Code review fixes
mettolen 928c096
Merge branch 'dev' into airobot_add_number_entity
mettolen ef16ee0
Fix unit tests
mettolen fca9e4d
Merge branch 'dev' into airobot_add_number_entity
mettolen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "entity": { | ||
| "number": { | ||
| "hysteresis_band": { | ||
| "default": "mdi:delta" | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| """Number platform for Airobot thermostat.""" | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Awaitable, Callable | ||
| from dataclasses import dataclass | ||
|
|
||
| from pyairobotrest.const import HYSTERESIS_BAND_MAX, HYSTERESIS_BAND_MIN | ||
| from pyairobotrest.exceptions import AirobotError | ||
|
|
||
| from homeassistant.components.number import ( | ||
| NumberDeviceClass, | ||
| NumberEntity, | ||
| NumberEntityDescription, | ||
| ) | ||
| from homeassistant.const import EntityCategory, UnitOfTemperature | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ServiceValidationError | ||
| from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback | ||
|
|
||
| from . import AirobotConfigEntry | ||
| from .const import DOMAIN | ||
| from .coordinator import AirobotDataUpdateCoordinator | ||
| from .entity import AirobotEntity | ||
|
|
||
| PARALLEL_UPDATES = 0 | ||
|
|
||
|
|
||
| @dataclass(frozen=True, kw_only=True) | ||
| class AirobotNumberEntityDescription(NumberEntityDescription): | ||
| """Describes Airobot number entity.""" | ||
|
|
||
| value_fn: Callable[[AirobotDataUpdateCoordinator], float] | ||
| set_value_fn: Callable[[AirobotDataUpdateCoordinator, float], Awaitable[None]] | ||
|
|
||
|
|
||
| NUMBERS: tuple[AirobotNumberEntityDescription, ...] = ( | ||
| AirobotNumberEntityDescription( | ||
| key="hysteresis_band", | ||
| translation_key="hysteresis_band", | ||
| device_class=NumberDeviceClass.TEMPERATURE, | ||
| entity_category=EntityCategory.CONFIG, | ||
| entity_registry_enabled_default=False, | ||
| native_unit_of_measurement=UnitOfTemperature.CELSIUS, | ||
| native_min_value=HYSTERESIS_BAND_MIN / 10.0, | ||
| native_max_value=HYSTERESIS_BAND_MAX / 10.0, | ||
|
mettolen marked this conversation as resolved.
|
||
| native_step=0.1, | ||
| value_fn=lambda coordinator: coordinator.data.settings.hysteresis_band, | ||
| set_value_fn=lambda coordinator, value: coordinator.client.set_hysteresis_band( | ||
| value | ||
| ), | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| entry: AirobotConfigEntry, | ||
| async_add_entities: AddConfigEntryEntitiesCallback, | ||
| ) -> None: | ||
| """Set up Airobot number platform.""" | ||
| coordinator = entry.runtime_data | ||
| async_add_entities( | ||
| AirobotNumber(coordinator, description) for description in NUMBERS | ||
| ) | ||
|
|
||
|
|
||
| class AirobotNumber(AirobotEntity, NumberEntity): | ||
| """Representation of an Airobot number entity.""" | ||
|
|
||
| entity_description: AirobotNumberEntityDescription | ||
|
|
||
| def __init__( | ||
| self, | ||
| coordinator: AirobotDataUpdateCoordinator, | ||
| description: AirobotNumberEntityDescription, | ||
| ) -> None: | ||
| """Initialize the number entity.""" | ||
| super().__init__(coordinator) | ||
| self.entity_description = description | ||
| self._attr_unique_id = f"{coordinator.data.status.device_id}_{description.key}" | ||
|
|
||
| @property | ||
| def native_value(self) -> float: | ||
| """Return the current value.""" | ||
| return self.entity_description.value_fn(self.coordinator) | ||
|
|
||
| async def async_set_native_value(self, value: float) -> None: | ||
| """Set the value.""" | ||
| try: | ||
| await self.entity_description.set_value_fn(self.coordinator, value) | ||
| except AirobotError as err: | ||
| raise ServiceValidationError( | ||
| translation_domain=DOMAIN, | ||
| translation_key="set_value_failed", | ||
| translation_placeholders={"error": str(err)}, | ||
| ) from err | ||
| else: | ||
| await self.coordinator.async_request_refresh() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # serializer version: 1 | ||
| # name: test_number_entities[number.test_thermostat_hysteresis_band-entry] | ||
| EntityRegistryEntrySnapshot({ | ||
| 'aliases': set({ | ||
| }), | ||
| 'area_id': None, | ||
| 'capabilities': dict({ | ||
| 'max': 0.5, | ||
| 'min': 0.0, | ||
| 'mode': <NumberMode.AUTO: 'auto'>, | ||
| 'step': 0.1, | ||
| }), | ||
| 'config_entry_id': <ANY>, | ||
| 'config_subentry_id': <ANY>, | ||
| 'device_class': None, | ||
| 'device_id': <ANY>, | ||
| 'disabled_by': None, | ||
| 'domain': 'number', | ||
| 'entity_category': <EntityCategory.CONFIG: 'config'>, | ||
| 'entity_id': 'number.test_thermostat_hysteresis_band', | ||
| 'has_entity_name': True, | ||
| 'hidden_by': None, | ||
| 'icon': None, | ||
| 'id': <ANY>, | ||
| 'labels': set({ | ||
| }), | ||
| 'name': None, | ||
| 'options': dict({ | ||
| }), | ||
| 'original_device_class': <NumberDeviceClass.TEMPERATURE: 'temperature'>, | ||
| 'original_icon': None, | ||
| 'original_name': 'Hysteresis band', | ||
| 'platform': 'airobot', | ||
| 'previous_unique_id': None, | ||
| 'suggested_object_id': None, | ||
| 'supported_features': 0, | ||
| 'translation_key': 'hysteresis_band', | ||
| 'unique_id': 'T01A1B2C3_hysteresis_band', | ||
| 'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>, | ||
| }) | ||
| # --- | ||
| # name: test_number_entities[number.test_thermostat_hysteresis_band-state] | ||
| StateSnapshot({ | ||
| 'attributes': ReadOnlyDict({ | ||
| 'device_class': 'temperature', | ||
| 'friendly_name': 'Test Thermostat Hysteresis band', | ||
| 'max': 0.5, | ||
| 'min': 0.0, | ||
| 'mode': <NumberMode.AUTO: 'auto'>, | ||
| 'step': 0.1, | ||
| 'unit_of_measurement': <UnitOfTemperature.CELSIUS: '°C'>, | ||
| }), | ||
| 'context': <ANY>, | ||
| 'entity_id': 'number.test_thermostat_hysteresis_band', | ||
| 'last_changed': <ANY>, | ||
| 'last_reported': <ANY>, | ||
| 'last_updated': <ANY>, | ||
| 'state': '0.1', | ||
| }) | ||
| # --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| """Test the Airobot number platform.""" | ||
|
|
||
| from unittest.mock import AsyncMock | ||
|
|
||
| from pyairobotrest.exceptions import AirobotError | ||
| import pytest | ||
| from syrupy.assertion import SnapshotAssertion | ||
|
|
||
| from homeassistant.components.number import ( | ||
| ATTR_VALUE, | ||
| DOMAIN as NUMBER_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| ) | ||
| from homeassistant.const import ATTR_ENTITY_ID, Platform | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.exceptions import ServiceValidationError | ||
| import homeassistant.helpers.entity_registry as er | ||
|
|
||
| from tests.common import MockConfigEntry, snapshot_platform | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def platforms() -> list[Platform]: | ||
| """Fixture to specify platforms to test.""" | ||
| return [Platform.NUMBER] | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration") | ||
| async def test_number_entities( | ||
| hass: HomeAssistant, | ||
| snapshot: SnapshotAssertion, | ||
| entity_registry: er.EntityRegistry, | ||
| mock_config_entry: MockConfigEntry, | ||
| ) -> None: | ||
| """Test the number entities.""" | ||
| await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration") | ||
| async def test_number_set_hysteresis_band( | ||
| hass: HomeAssistant, | ||
| mock_airobot_client: AsyncMock, | ||
| ) -> None: | ||
| """Test setting hysteresis band value.""" | ||
| await hass.services.async_call( | ||
| NUMBER_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| { | ||
| ATTR_ENTITY_ID: "number.test_thermostat_hysteresis_band", | ||
| ATTR_VALUE: 0.3, | ||
| }, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| mock_airobot_client.set_hysteresis_band.assert_called_once_with(0.3) | ||
|
|
||
|
|
||
| @pytest.mark.usefixtures("entity_registry_enabled_by_default", "init_integration") | ||
| async def test_number_set_value_error( | ||
| hass: HomeAssistant, | ||
| mock_airobot_client: AsyncMock, | ||
| ) -> None: | ||
| """Test error handling when setting number value fails.""" | ||
| mock_airobot_client.set_hysteresis_band.side_effect = AirobotError("Device error") | ||
|
|
||
| with pytest.raises(ServiceValidationError) as exc_info: | ||
| await hass.services.async_call( | ||
| NUMBER_DOMAIN, | ||
| SERVICE_SET_VALUE, | ||
| { | ||
| ATTR_ENTITY_ID: "number.test_thermostat_hysteresis_band", | ||
| ATTR_VALUE: 0.3, | ||
| }, | ||
| blocking=True, | ||
| ) | ||
|
|
||
| assert exc_info.value.translation_domain == "airobot" | ||
| assert exc_info.value.translation_key == "set_value_failed" |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.