From c4be3c4de2782217ceed62a2df9de151cd2a3f3c Mon Sep 17 00:00:00 2001 From: Robin Lintermann Date: Fri, 6 Jun 2025 12:13:06 +0200 Subject: [PATCH] Smarla integration number platform (#145747) Add number platform to smarla integration --- homeassistant/components/smarla/const.py | 2 +- homeassistant/components/smarla/icons.json | 5 + homeassistant/components/smarla/number.py | 62 +++++++++++ homeassistant/components/smarla/strings.json | 5 + tests/components/smarla/conftest.py | 2 + .../smarla/snapshots/test_number.ambr | 58 ++++++++++ tests/components/smarla/test_number.py | 103 ++++++++++++++++++ tests/components/smarla/test_switch.py | 2 +- 8 files changed, 237 insertions(+), 2 deletions(-) create mode 100644 homeassistant/components/smarla/number.py create mode 100644 tests/components/smarla/snapshots/test_number.ambr create mode 100644 tests/components/smarla/test_number.py diff --git a/homeassistant/components/smarla/const.py b/homeassistant/components/smarla/const.py index 7125e3f7270..f81ccd328bc 100644 --- a/homeassistant/components/smarla/const.py +++ b/homeassistant/components/smarla/const.py @@ -6,7 +6,7 @@ DOMAIN = "smarla" HOST = "https://devices.swing2sleep.de" -PLATFORMS = [Platform.SWITCH] +PLATFORMS = [Platform.NUMBER, Platform.SWITCH] DEVICE_MODEL_NAME = "Smarla" MANUFACTURER_NAME = "Swing2Sleep" diff --git a/homeassistant/components/smarla/icons.json b/homeassistant/components/smarla/icons.json index 5a31ec88822..2ba7404cc35 100644 --- a/homeassistant/components/smarla/icons.json +++ b/homeassistant/components/smarla/icons.json @@ -4,6 +4,11 @@ "smart_mode": { "default": "mdi:refresh-auto" } + }, + "number": { + "intensity": { + "default": "mdi:sine-wave" + } } } } diff --git a/homeassistant/components/smarla/number.py b/homeassistant/components/smarla/number.py new file mode 100644 index 00000000000..d2421962b07 --- /dev/null +++ b/homeassistant/components/smarla/number.py @@ -0,0 +1,62 @@ +"""Support for the Swing2Sleep Smarla number entities.""" + +from dataclasses import dataclass + +from pysmarlaapi.federwiege.classes import Property + +from homeassistant.components.number import ( + NumberEntity, + NumberEntityDescription, + NumberMode, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from . import FederwiegeConfigEntry +from .entity import SmarlaBaseEntity, SmarlaEntityDescription + + +@dataclass(frozen=True, kw_only=True) +class SmarlaNumberEntityDescription(SmarlaEntityDescription, NumberEntityDescription): + """Class describing Swing2Sleep Smarla number entities.""" + + +NUMBERS: list[SmarlaNumberEntityDescription] = [ + SmarlaNumberEntityDescription( + key="intensity", + translation_key="intensity", + service="babywiege", + property="intensity", + native_max_value=100, + native_min_value=0, + native_step=1, + mode=NumberMode.SLIDER, + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: FederwiegeConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Smarla numbers from config entry.""" + federwiege = config_entry.runtime_data + async_add_entities(SmarlaNumber(federwiege, desc) for desc in NUMBERS) + + +class SmarlaNumber(SmarlaBaseEntity, NumberEntity): + """Representation of Smarla number.""" + + entity_description: SmarlaNumberEntityDescription + + _property: Property[int] + + @property + def native_value(self) -> float: + """Return the entity value to represent the entity state.""" + return self._property.get() + + def set_native_value(self, value: float) -> None: + """Update to the smarla device.""" + self._property.set(int(value)) diff --git a/homeassistant/components/smarla/strings.json b/homeassistant/components/smarla/strings.json index 8426bc30566..fbe5df4c1d0 100644 --- a/homeassistant/components/smarla/strings.json +++ b/homeassistant/components/smarla/strings.json @@ -23,6 +23,11 @@ "smart_mode": { "name": "Smart Mode" } + }, + "number": { + "intensity": { + "name": "Intensity" + } } } } diff --git a/tests/components/smarla/conftest.py b/tests/components/smarla/conftest.py index 3ee7edaf663..d472e929bcc 100644 --- a/tests/components/smarla/conftest.py +++ b/tests/components/smarla/conftest.py @@ -66,10 +66,12 @@ def mock_federwiege(mock_connection: MagicMock) -> Generator[MagicMock]: mock_babywiege_service.props = { "swing_active": MagicMock(spec=Property), "smart_mode": MagicMock(spec=Property), + "intensity": MagicMock(spec=Property), } mock_babywiege_service.props["swing_active"].get.return_value = False mock_babywiege_service.props["smart_mode"].get.return_value = False + mock_babywiege_service.props["intensity"].get.return_value = 1 federwiege.services = { "babywiege": mock_babywiege_service, diff --git a/tests/components/smarla/snapshots/test_number.ambr b/tests/components/smarla/snapshots/test_number.ambr new file mode 100644 index 00000000000..3232795c277 --- /dev/null +++ b/tests/components/smarla/snapshots/test_number.ambr @@ -0,0 +1,58 @@ +# serializer version: 1 +# name: test_entities[number.smarla_intensity-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': dict({ + 'max': 100, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'number', + 'entity_category': None, + 'entity_id': 'number.smarla_intensity', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Intensity', + 'platform': 'smarla', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'intensity', + 'unique_id': 'ABCD-intensity', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[number.smarla_intensity-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Smarla Intensity', + 'max': 100, + 'min': 0, + 'mode': , + 'step': 1, + }), + 'context': , + 'entity_id': 'number.smarla_intensity', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': '1', + }) +# --- diff --git a/tests/components/smarla/test_number.py b/tests/components/smarla/test_number.py new file mode 100644 index 00000000000..642b39f33fb --- /dev/null +++ b/tests/components/smarla/test_number.py @@ -0,0 +1,103 @@ +"""Test number platform for Swing2Sleep Smarla integration.""" + +from unittest.mock import MagicMock, patch + +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.helpers import entity_registry as er + +from . import setup_integration, update_property_listeners + +from tests.common import MockConfigEntry, snapshot_platform + +NUMBER_ENTITIES = [ + { + "entity_id": "number.smarla_intensity", + "service": "babywiege", + "property": "intensity", + }, +] + + +@pytest.mark.usefixtures("mock_federwiege") +async def test_entities( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + entity_registry: er.EntityRegistry, + snapshot: SnapshotAssertion, +) -> None: + """Test the Smarla entities.""" + with ( + patch("homeassistant.components.smarla.PLATFORMS", [Platform.NUMBER]), + ): + assert await setup_integration(hass, mock_config_entry) + + await snapshot_platform( + hass, entity_registry, snapshot, mock_config_entry.entry_id + ) + + +@pytest.mark.parametrize( + ("service", "parameter"), + [(SERVICE_SET_VALUE, 100)], +) +@pytest.mark.parametrize("entity_info", NUMBER_ENTITIES) +async def test_number_action( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_federwiege: MagicMock, + entity_info: dict[str, str], + service: str, + parameter: int, +) -> None: + """Test Smarla Number set behavior.""" + assert await setup_integration(hass, mock_config_entry) + + mock_number_property = mock_federwiege.get_property( + entity_info["service"], entity_info["property"] + ) + + entity_id = entity_info["entity_id"] + + # Turn on + await hass.services.async_call( + NUMBER_DOMAIN, + service, + {ATTR_ENTITY_ID: entity_id, ATTR_VALUE: parameter}, + blocking=True, + ) + mock_number_property.set.assert_called_once_with(parameter) + + +@pytest.mark.parametrize("entity_info", NUMBER_ENTITIES) +async def test_number_state_update( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_federwiege: MagicMock, + entity_info: dict[str, str], +) -> None: + """Test Smarla Number callback.""" + assert await setup_integration(hass, mock_config_entry) + + mock_number_property = mock_federwiege.get_property( + entity_info["service"], entity_info["property"] + ) + + entity_id = entity_info["entity_id"] + + assert hass.states.get(entity_id).state == "1" + + mock_number_property.get.return_value = 100 + + await update_property_listeners(mock_number_property) + await hass.async_block_till_done() + + assert hass.states.get(entity_id).state == "100" diff --git a/tests/components/smarla/test_switch.py b/tests/components/smarla/test_switch.py index fcab0e0ee95..3f83bce3819 100644 --- a/tests/components/smarla/test_switch.py +++ b/tests/components/smarla/test_switch.py @@ -35,9 +35,9 @@ SWITCH_ENTITIES = [ ] +@pytest.mark.usefixtures("mock_federwiege") async def test_entities( hass: HomeAssistant, - mock_federwiege: MagicMock, mock_config_entry: MockConfigEntry, entity_registry: er.EntityRegistry, snapshot: SnapshotAssertion,