mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 14:27:07 +00:00
Smarla integration number platform (#145747)
Add number platform to smarla integration
This commit is contained in:
parent
626591f832
commit
c4be3c4de2
@ -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"
|
||||
|
@ -4,6 +4,11 @@
|
||||
"smart_mode": {
|
||||
"default": "mdi:refresh-auto"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"intensity": {
|
||||
"default": "mdi:sine-wave"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
62
homeassistant/components/smarla/number.py
Normal file
62
homeassistant/components/smarla/number.py
Normal file
@ -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))
|
@ -23,6 +23,11 @@
|
||||
"smart_mode": {
|
||||
"name": "Smart Mode"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"intensity": {
|
||||
"name": "Intensity"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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,
|
||||
|
58
tests/components/smarla/snapshots/test_number.ambr
Normal file
58
tests/components/smarla/snapshots/test_number.ambr
Normal file
@ -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': <NumberMode.SLIDER: 'slider'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'config_entry_id': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'number',
|
||||
'entity_category': None,
|
||||
'entity_id': 'number.smarla_intensity',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <NumberMode.SLIDER: 'slider'>,
|
||||
'step': 1,
|
||||
}),
|
||||
'context': <ANY>,
|
||||
'entity_id': 'number.smarla_intensity',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': '1',
|
||||
})
|
||||
# ---
|
103
tests/components/smarla/test_number.py
Normal file
103
tests/components/smarla/test_number.py
Normal file
@ -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"
|
@ -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,
|
||||
|
Loading…
x
Reference in New Issue
Block a user