diff --git a/homeassistant/components/wmspro/__init__.py b/homeassistant/components/wmspro/__init__.py index 37bf1495a56..ebfdf5b8b34 100644 --- a/homeassistant/components/wmspro/__init__.py +++ b/homeassistant/components/wmspro/__init__.py @@ -15,7 +15,12 @@ from homeassistant.helpers.typing import UNDEFINED from .const import DOMAIN, MANUFACTURER -PLATFORMS: list[Platform] = [Platform.COVER, Platform.LIGHT, Platform.SCENE] +PLATFORMS: list[Platform] = [ + Platform.BUTTON, + Platform.COVER, + Platform.LIGHT, + Platform.SCENE, +] type WebControlProConfigEntry = ConfigEntry[WebControlPro] diff --git a/homeassistant/components/wmspro/button.py b/homeassistant/components/wmspro/button.py new file mode 100644 index 00000000000..f1ab0489b86 --- /dev/null +++ b/homeassistant/components/wmspro/button.py @@ -0,0 +1,40 @@ +"""Identify support for WMS WebControl pro.""" + +from __future__ import annotations + +from wmspro.const import WMS_WebControl_pro_API_actionDescription + +from homeassistant.components.button import ButtonDeviceClass, ButtonEntity +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback + +from . import WebControlProConfigEntry +from .entity import WebControlProGenericEntity + + +async def async_setup_entry( + hass: HomeAssistant, + config_entry: WebControlProConfigEntry, + async_add_entities: AddConfigEntryEntitiesCallback, +) -> None: + """Set up the WMS based identify buttons from a config entry.""" + hub = config_entry.runtime_data + + entities: list[WebControlProGenericEntity] = [ + WebControlProIdentifyButton(config_entry.entry_id, dest) + for dest in hub.dests.values() + if dest.action(WMS_WebControl_pro_API_actionDescription.Identify) + ] + + async_add_entities(entities) + + +class WebControlProIdentifyButton(WebControlProGenericEntity, ButtonEntity): + """Representation of a WMS based identify button.""" + + _attr_device_class = ButtonDeviceClass.IDENTIFY + + async def async_press(self) -> None: + """Handle the button press.""" + action = self._dest.action(WMS_WebControl_pro_API_actionDescription.Identify) + await action() diff --git a/homeassistant/components/wmspro/cover.py b/homeassistant/components/wmspro/cover.py index d46ffa6dab6..97ce540dc0b 100644 --- a/homeassistant/components/wmspro/cover.py +++ b/homeassistant/components/wmspro/cover.py @@ -45,6 +45,7 @@ class WebControlProCover(WebControlProGenericEntity, CoverEntity): """Base representation of a WMS based cover.""" _drive_action_desc: WMS_WebControl_pro_API_actionDescription + _attr_name = None @property def current_cover_position(self) -> int | None: diff --git a/homeassistant/components/wmspro/entity.py b/homeassistant/components/wmspro/entity.py index 0bbbc69a294..758a89b7ed8 100644 --- a/homeassistant/components/wmspro/entity.py +++ b/homeassistant/components/wmspro/entity.py @@ -15,7 +15,6 @@ class WebControlProGenericEntity(Entity): _attr_attribution = ATTRIBUTION _attr_has_entity_name = True - _attr_name = None def __init__(self, config_entry_id: str, dest: Destination) -> None: """Initialize the entity with destination channel.""" diff --git a/homeassistant/components/wmspro/light.py b/homeassistant/components/wmspro/light.py index d181beb1eaa..754e537c34a 100644 --- a/homeassistant/components/wmspro/light.py +++ b/homeassistant/components/wmspro/light.py @@ -42,6 +42,7 @@ class WebControlProLight(WebControlProGenericEntity, LightEntity): """Representation of a WMS based light.""" _attr_color_mode = ColorMode.ONOFF + _attr_name = None _attr_supported_color_modes = {ColorMode.ONOFF} @property diff --git a/tests/components/wmspro/snapshots/test_button.ambr b/tests/components/wmspro/snapshots/test_button.ambr new file mode 100644 index 00000000000..431a92c26d6 --- /dev/null +++ b/tests/components/wmspro/snapshots/test_button.ambr @@ -0,0 +1,16 @@ +# serializer version: 1 +# name: test_button_update + StateSnapshot({ + 'attributes': ReadOnlyDict({ + 'attribution': 'Data provided by WMS WebControl pro API', + 'device_class': 'identify', + 'friendly_name': 'Markise Identify', + }), + 'context': , + 'entity_id': 'button.markise_identify', + 'last_changed': , + 'last_reported': , + 'last_updated': , + 'state': 'unknown', + }) +# --- diff --git a/tests/components/wmspro/test_button.py b/tests/components/wmspro/test_button.py new file mode 100644 index 00000000000..2894399f9f9 --- /dev/null +++ b/tests/components/wmspro/test_button.py @@ -0,0 +1,66 @@ +"""Test the wmspro button support.""" + +from unittest.mock import AsyncMock, patch + +from syrupy import SnapshotAssertion + +from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN, SERVICE_PRESS +from homeassistant.const import ATTR_ENTITY_ID +from homeassistant.core import HomeAssistant + +from . import setup_config_entry + +from tests.common import MockConfigEntry + + +async def test_button_update( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_hub_ping: AsyncMock, + mock_hub_configuration_prod_awning_dimmer: AsyncMock, + mock_hub_status_prod_awning: AsyncMock, + mock_action_call: AsyncMock, + snapshot: SnapshotAssertion, +) -> None: + """Test that a button entity is created and updated correctly.""" + assert await setup_config_entry(hass, mock_config_entry) + assert len(mock_hub_ping.mock_calls) == 1 + assert len(mock_hub_configuration_prod_awning_dimmer.mock_calls) == 1 + assert len(mock_hub_status_prod_awning.mock_calls) == 2 + + entity = hass.states.get("button.markise_identify") + assert entity is not None + assert entity == snapshot + + +async def test_button_press( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_hub_ping: AsyncMock, + mock_hub_configuration_prod_awning_dimmer: AsyncMock, + mock_hub_status_prod_awning: AsyncMock, + mock_action_call: AsyncMock, +) -> None: + """Test that a button entity is pressed correctly.""" + + assert await setup_config_entry(hass, mock_config_entry) + + with patch( + "wmspro.destination.Destination.refresh", + return_value=True, + ): + before = len(mock_hub_status_prod_awning.mock_calls) + entity = hass.states.get("button.markise_identify") + before_state = entity.state + + await hass.services.async_call( + BUTTON_DOMAIN, + SERVICE_PRESS, + {ATTR_ENTITY_ID: entity.entity_id}, + blocking=True, + ) + + entity = hass.states.get("button.markise_identify") + assert entity is not None + assert entity.state != before_state + assert len(mock_hub_status_prod_awning.mock_calls) == before