mirror of
https://github.com/home-assistant/core.git
synced 2026-04-02 16:36:08 +01:00
Add send diagnostics button to smarla (#164335)
This commit is contained in:
53
homeassistant/components/smarla/button.py
Normal file
53
homeassistant/components/smarla/button.py
Normal file
@@ -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")
|
||||
@@ -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"
|
||||
|
||||
@@ -30,6 +30,11 @@
|
||||
}
|
||||
},
|
||||
"entity": {
|
||||
"button": {
|
||||
"send_diagnostics": {
|
||||
"name": "Send diagnostics"
|
||||
}
|
||||
},
|
||||
"number": {
|
||||
"intensity": {
|
||||
"name": "Intensity"
|
||||
|
||||
@@ -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
|
||||
|
||||
50
tests/components/smarla/snapshots/test_button.ambr
Normal file
50
tests/components/smarla/snapshots/test_button.ambr
Normal file
@@ -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': <ANY>,
|
||||
'config_subentry_id': <ANY>,
|
||||
'device_class': None,
|
||||
'device_id': <ANY>,
|
||||
'disabled_by': None,
|
||||
'domain': 'button',
|
||||
'entity_category': <EntityCategory.CONFIG: 'config'>,
|
||||
'entity_id': 'button.smarla_send_diagnostics',
|
||||
'has_entity_name': True,
|
||||
'hidden_by': None,
|
||||
'icon': None,
|
||||
'id': <ANY>,
|
||||
'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': <ANY>,
|
||||
'entity_id': 'button.smarla_send_diagnostics',
|
||||
'last_changed': <ANY>,
|
||||
'last_reported': <ANY>,
|
||||
'last_updated': <ANY>,
|
||||
'state': 'unknown',
|
||||
})
|
||||
# ---
|
||||
67
tests/components/smarla/test_button.py
Normal file
67
tests/components/smarla/test_button.py
Normal file
@@ -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()
|
||||
Reference in New Issue
Block a user