diff --git a/homeassistant/components/smarla/button.py b/homeassistant/components/smarla/button.py new file mode 100644 index 00000000000..c4ebbf3486c --- /dev/null +++ b/homeassistant/components/smarla/button.py @@ -0,0 +1,53 @@ +"""Support for the Swing2Sleep Smarla button entities.""" + +from dataclasses import dataclass + +from pysmarlaapi.federwiege.services.classes import Property + +from homeassistant.components.button import ButtonEntity, ButtonEntityDescription +from homeassistant.const import EntityCategory +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from . import FederwiegeConfigEntry +from .entity import SmarlaBaseEntity, SmarlaEntityDescription + +PARALLEL_UPDATES = 0 + + +@dataclass(frozen=True, kw_only=True) +class SmarlaButtonEntityDescription(SmarlaEntityDescription, ButtonEntityDescription): + """Class describing Swing2Sleep Smarla button entity.""" + + +BUTTONS: list[SmarlaButtonEntityDescription] = [ + SmarlaButtonEntityDescription( + key="send_diagnostics", + translation_key="send_diagnostics", + service="system", + property="send_diagnostic_data", + entity_category=EntityCategory.CONFIG, + ), +] + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: FederwiegeConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the Smarla buttons from config entry.""" + federwiege = config_entry.runtime_data + async_add_entities(SmarlaButton(federwiege, desc) for desc in BUTTONS) + + +class SmarlaButton(SmarlaBaseEntity, ButtonEntity): + """Representation of a Smarla button.""" + + entity_description: SmarlaButtonEntityDescription + + _property: Property[str] + + def press(self) -> None: + """Press the button.""" + self._property.set("Sent from Home Assistant") diff --git a/homeassistant/components/smarla/const.py b/homeassistant/components/smarla/const.py index 7f0572d0ecb..96f814a34cd 100644 --- a/homeassistant/components/smarla/const.py +++ b/homeassistant/components/smarla/const.py @@ -6,7 +6,13 @@ DOMAIN = "smarla" HOST = "https://devices.swing2sleep.de" -PLATFORMS = [Platform.NUMBER, Platform.SENSOR, Platform.SWITCH, Platform.UPDATE] +PLATFORMS = [ + Platform.BUTTON, + Platform.NUMBER, + Platform.SENSOR, + Platform.SWITCH, + Platform.UPDATE, +] DEVICE_MODEL_NAME = "Smarla" MANUFACTURER_NAME = "Swing2Sleep" diff --git a/homeassistant/components/smarla/strings.json b/homeassistant/components/smarla/strings.json index 0427bd04d11..a73aa80e166 100644 --- a/homeassistant/components/smarla/strings.json +++ b/homeassistant/components/smarla/strings.json @@ -30,6 +30,11 @@ } }, "entity": { + "button": { + "send_diagnostics": { + "name": "Send diagnostics" + } + }, "number": { "intensity": { "name": "Intensity" diff --git a/tests/components/smarla/conftest.py b/tests/components/smarla/conftest.py index cd626174ce2..38fef2f0968 100644 --- a/tests/components/smarla/conftest.py +++ b/tests/components/smarla/conftest.py @@ -105,6 +105,7 @@ def _mock_system_service() -> MagicMock: mock_system_service.props = { "firmware_update": MagicMock(spec=Property), "firmware_update_status": MagicMock(spec=Property), + "send_diagnostic_data": MagicMock(spec=Property), } mock_system_service.props["firmware_update"].get.return_value = 0 diff --git a/tests/components/smarla/snapshots/test_button.ambr b/tests/components/smarla/snapshots/test_button.ambr new file mode 100644 index 00000000000..7c0394f4f61 --- /dev/null +++ b/tests/components/smarla/snapshots/test_button.ambr @@ -0,0 +1,50 @@ +# serializer version: 1 +# name: test_entities[button.smarla_send_diagnostics-entry] + EntityRegistryEntrySnapshot({ + 'aliases': set({ + }), + 'area_id': None, + 'capabilities': None, + 'config_entry_id': , + 'config_subentry_id': , + 'device_class': None, + 'device_id': , + 'disabled_by': None, + 'domain': 'button', + 'entity_category': , + 'entity_id': 'button.smarla_send_diagnostics', + 'has_entity_name': True, + 'hidden_by': None, + 'icon': None, + 'id': , + 'labels': set({ + }), + 'name': None, + 'object_id_base': 'Send diagnostics', + 'options': dict({ + }), + 'original_device_class': None, + 'original_icon': None, + 'original_name': 'Send diagnostics', + 'platform': 'smarla', + 'previous_unique_id': None, + 'suggested_object_id': None, + 'supported_features': 0, + 'translation_key': 'send_diagnostics', + 'unique_id': 'ABCD-send_diagnostics', + 'unit_of_measurement': None, + }) +# --- +# name: test_entities[button.smarla_send_diagnostics-state] + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'friendly_name': 'Smarla Send diagnostics', + }), + 'context': , + 'entity_id': 'button.smarla_send_diagnostics', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/smarla/test_button.py b/tests/components/smarla/test_button.py new file mode 100644 index 00000000000..2abe094ac8a --- /dev/null +++ b/tests/components/smarla/test_button.py @@ -0,0 +1,67 @@ +"""Test button platform for Swing2Sleep Smarla integration.""" + +from unittest.mock import MagicMock, patch + +import pytest +from syrupy.assertion import SnapshotAssertion + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +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 + +from tests.common import MockConfigEntry, snapshot_platform + +BUTTON_ENTITIES = [ + { + "entity_id": "button.smarla_send_diagnostics", + "service": "system", + "property": "send_diagnostic_data", + }, +] + + +@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.BUTTON]), + ): + assert await setup_integration(hass, mock_config_entry) + + await snapshot_platform( + hass, entity_registry, snapshot, mock_config_entry.entry_id + ) + + +@pytest.mark.parametrize("entity_info", BUTTON_ENTITIES) +async def test_button_action( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_federwiege: MagicMock, + entity_info: dict[str, str], +) -> None: + """Test Smarla Button press behavior.""" + assert await setup_integration(hass, mock_config_entry) + + mock_button_property = mock_federwiege.get_property( + entity_info["service"], entity_info["property"] + ) + + entity_id = entity_info["entity_id"] + + # Turn on + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity_id}, + blocking=True, + ) + mock_button_property.set.assert_called_once()