Add number platform to SmartThings (#141063)

* Add select platform to SmartThings

* Add number platform to SmartThings

* Fix

* Fix

* Fix

* Fix
This commit is contained in:
Joost Lekkerkerker 2025-03-22 19:03:26 +01:00 committed by GitHub
parent 931ce8951e
commit 4b4d75063c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 285 additions and 0 deletions

View File

@ -85,6 +85,7 @@ PLATFORMS = [
Platform.FAN,
Platform.LIGHT,
Platform.LOCK,
Platform.NUMBER,
Platform.SCENE,
Platform.SELECT,
Platform.SENSOR,

View File

@ -14,6 +14,11 @@
}
}
},
"number": {
"washer_rinse_cycles": {
"default": "mdi:waves-arrow-up"
}
},
"select": {
"operating_state": {
"state": {

View File

@ -0,0 +1,77 @@
"""Support for number entities through the SmartThings cloud API."""
from __future__ import annotations
from pysmartthings import Attribute, Capability, Command, SmartThings
from homeassistant.components.number import NumberEntity
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from . import FullDevice, SmartThingsConfigEntry
from .const import MAIN
from .entity import SmartThingsEntity
async def async_setup_entry(
hass: HomeAssistant,
entry: SmartThingsConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Add number entities for a config entry."""
entry_data = entry.runtime_data
async_add_entities(
SmartThingsWasherRinseCyclesNumberEntity(entry_data.client, device)
for device in entry_data.devices.values()
if Capability.CUSTOM_WASHER_RINSE_CYCLES in device.status[MAIN]
)
class SmartThingsWasherRinseCyclesNumberEntity(SmartThingsEntity, NumberEntity):
"""Define a SmartThings number."""
_attr_translation_key = "washer_rinse_cycles"
_attr_native_step = 1.0
def __init__(self, client: SmartThings, device: FullDevice) -> None:
"""Initialize the instance."""
super().__init__(client, device, {Capability.CUSTOM_WASHER_RINSE_CYCLES})
self._attr_unique_id = (
f"{device.device.device_id}_{MAIN}_{Capability.CUSTOM_WASHER_RINSE_CYCLES}"
)
@property
def options(self) -> list[int]:
"""Return the list of options."""
values = self.get_attribute_value(
Capability.CUSTOM_WASHER_RINSE_CYCLES,
Attribute.SUPPORTED_WASHER_RINSE_CYCLES,
)
return [int(value) for value in values] if values else []
@property
def native_value(self) -> float | None:
"""Return the current value."""
return int(
self.get_attribute_value(
Capability.CUSTOM_WASHER_RINSE_CYCLES, Attribute.WASHER_RINSE_CYCLES
)
)
@property
def native_min_value(self) -> float:
"""Return the minimum value."""
return min(self.options)
@property
def native_max_value(self) -> float:
"""Return the maximum value."""
return max(self.options)
async def async_set_native_value(self, value: float) -> None:
"""Set the value."""
await self.execute_device_command(
Capability.CUSTOM_WASHER_RINSE_CYCLES,
Command.SET_WASHER_RINSE_CYCLES,
str(int(value)),
)

View File

@ -78,6 +78,12 @@
}
}
},
"number": {
"washer_rinse_cycles": {
"name": "Rinse cycles",
"unit_of_measurement": "cycles"
}
},
"select": {
"operating_state": {
"state": {

View File

@ -0,0 +1,115 @@
# serializer version: 1
# name: test_all_entities[da_wm_wm_000001][number.washer_rinse_cycles-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 5,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'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.washer_rinse_cycles',
'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': 'Rinse cycles',
'platform': 'smartthings',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'washer_rinse_cycles',
'unique_id': 'f984b91d-f250-9d42-3436-33f09a422a47_main_custom.washerRinseCycles',
'unit_of_measurement': 'cycles',
})
# ---
# name: test_all_entities[da_wm_wm_000001][number.washer_rinse_cycles-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Washer Rinse cycles',
'max': 5,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': 'cycles',
}),
'context': <ANY>,
'entity_id': 'number.washer_rinse_cycles',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2',
})
# ---
# name: test_all_entities[da_wm_wm_000001_1][number.washing_machine_rinse_cycles-entry]
EntityRegistryEntrySnapshot({
'aliases': set({
}),
'area_id': None,
'capabilities': dict({
'max': 5,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
}),
'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.washing_machine_rinse_cycles',
'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': 'Rinse cycles',
'platform': 'smartthings',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'washer_rinse_cycles',
'unique_id': '63803fae-cbed-f356-a063-2cf148ae3ca7_main_custom.washerRinseCycles',
'unit_of_measurement': 'cycles',
})
# ---
# name: test_all_entities[da_wm_wm_000001_1][number.washing_machine_rinse_cycles-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Washing Machine Rinse cycles',
'max': 5,
'min': 0,
'mode': <NumberMode.AUTO: 'auto'>,
'step': 1.0,
'unit_of_measurement': 'cycles',
}),
'context': <ANY>,
'entity_id': 'number.washing_machine_rinse_cycles',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': '2',
})
# ---

View File

@ -0,0 +1,81 @@
"""Test for the SmartThings number platform."""
from unittest.mock import AsyncMock
from pysmartthings import Attribute, Capability, Command
import pytest
from syrupy import SnapshotAssertion
from homeassistant.components.number import (
ATTR_VALUE,
DOMAIN as NUMBER_DOMAIN,
SERVICE_SET_VALUE,
)
from homeassistant.components.smartthings import MAIN
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, snapshot_smartthings_entities, trigger_update
from tests.common import MockConfigEntry
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
devices: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
await setup_integration(hass, mock_config_entry)
snapshot_smartthings_entities(hass, entity_registry, snapshot, Platform.NUMBER)
@pytest.mark.parametrize("device_fixture", ["da_wm_wm_000001"])
async def test_set_value(
hass: HomeAssistant,
devices: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting a value."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
NUMBER_DOMAIN,
SERVICE_SET_VALUE,
{ATTR_ENTITY_ID: "number.washer_rinse_cycles", ATTR_VALUE: 3},
blocking=True,
)
devices.execute_device_command.assert_called_once_with(
"f984b91d-f250-9d42-3436-33f09a422a47",
Capability.CUSTOM_WASHER_RINSE_CYCLES,
Command.SET_WASHER_RINSE_CYCLES,
MAIN,
argument="3",
)
@pytest.mark.parametrize("device_fixture", ["da_wm_wm_000001"])
async def test_state_update(
hass: HomeAssistant,
devices: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test state update."""
await setup_integration(hass, mock_config_entry)
assert hass.states.get("number.washer_rinse_cycles").state == "2"
await trigger_update(
hass,
devices,
"f984b91d-f250-9d42-3436-33f09a422a47",
Capability.CUSTOM_WASHER_RINSE_CYCLES,
Attribute.WASHER_RINSE_CYCLES,
"3",
)
assert hass.states.get("number.washer_rinse_cycles").state == "3"