forked from home-assistant/core
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Replace climate fan speed 'silent' with a button (home-assistant#135075)
- Loading branch information
Showing
12 changed files
with
195 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""Support for Palazzetti buttons.""" | ||
|
||
from __future__ import annotations | ||
|
||
from pypalazzetti.exceptions import CommunicationError | ||
|
||
from homeassistant.components.button import ButtonEntity | ||
from homeassistant.core import HomeAssistant | ||
from homeassistant.exceptions import HomeAssistantError | ||
from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
|
||
from . import PalazzettiConfigEntry | ||
from .const import DOMAIN | ||
from .coordinator import PalazzettiDataUpdateCoordinator | ||
from .entity import PalazzettiEntity | ||
|
||
|
||
async def async_setup_entry( | ||
hass: HomeAssistant, | ||
config_entry: PalazzettiConfigEntry, | ||
async_add_entities: AddEntitiesCallback, | ||
) -> None: | ||
"""Set up Palazzetti button platform.""" | ||
|
||
coordinator = config_entry.runtime_data | ||
if coordinator.client.has_fan_silent: | ||
async_add_entities([PalazzettiSilentButtonEntity(coordinator)]) | ||
|
||
|
||
class PalazzettiSilentButtonEntity(PalazzettiEntity, ButtonEntity): | ||
"""Representation of a Palazzetti Silent button.""" | ||
|
||
_attr_translation_key = "silent" | ||
|
||
def __init__( | ||
self, | ||
coordinator: PalazzettiDataUpdateCoordinator, | ||
) -> None: | ||
"""Initialize a Palazzetti Silent button.""" | ||
super().__init__(coordinator) | ||
self._attr_unique_id = f"{coordinator.config_entry.unique_id}-silent" | ||
|
||
async def async_press(self) -> None: | ||
"""Press the button.""" | ||
try: | ||
await self.coordinator.client.set_fan_silent() | ||
except CommunicationError as err: | ||
raise HomeAssistantError( | ||
translation_domain=DOMAIN, translation_key="cannot_connect" | ||
) from err | ||
|
||
await self.coordinator.async_request_refresh() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
{ | ||
"entity": { | ||
"button": { | ||
"silent": { | ||
"default": "mdi:volume-mute" | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# serializer version: 1 | ||
# name: test_all_entities[button.stove_silent-entry] | ||
EntityRegistryEntrySnapshot({ | ||
'aliases': set({ | ||
}), | ||
'area_id': None, | ||
'capabilities': None, | ||
'config_entry_id': <ANY>, | ||
'device_class': None, | ||
'device_id': <ANY>, | ||
'disabled_by': None, | ||
'domain': 'button', | ||
'entity_category': None, | ||
'entity_id': 'button.stove_silent', | ||
'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': 'Silent', | ||
'platform': 'palazzetti', | ||
'previous_unique_id': None, | ||
'supported_features': 0, | ||
'translation_key': 'silent', | ||
'unique_id': '11:22:33:44:55:66-silent', | ||
'unit_of_measurement': None, | ||
}) | ||
# --- | ||
# name: test_all_entities[button.stove_silent-state] | ||
StateSnapshot({ | ||
'attributes': ReadOnlyDict({ | ||
'friendly_name': 'Stove Silent', | ||
}), | ||
'context': <ANY>, | ||
'entity_id': 'button.stove_silent', | ||
'last_changed': <ANY>, | ||
'last_reported': <ANY>, | ||
'last_updated': <ANY>, | ||
'state': 'unknown', | ||
}) | ||
# --- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
"""Tests for the Palazzetti button platform.""" | ||
|
||
from unittest.mock import AsyncMock, patch | ||
|
||
from pypalazzetti.exceptions import CommunicationError | ||
import pytest | ||
from syrupy 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.exceptions import HomeAssistantError | ||
from homeassistant.helpers import entity_registry as er | ||
|
||
from . import setup_integration | ||
|
||
from tests.common import MockConfigEntry, snapshot_platform | ||
|
||
ENTITY_ID = "button.stove_silent" | ||
|
||
|
||
async def test_all_entities( | ||
hass: HomeAssistant, | ||
snapshot: SnapshotAssertion, | ||
mock_palazzetti_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
entity_registry: er.EntityRegistry, | ||
) -> None: | ||
"""Test all entities.""" | ||
with patch("homeassistant.components.palazzetti.PLATFORMS", [Platform.BUTTON]): | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id) | ||
|
||
|
||
async def test_async_press( | ||
hass: HomeAssistant, | ||
mock_palazzetti_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test pressing via service call.""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
await hass.services.async_call( | ||
BUTTON_DOMAIN, | ||
SERVICE_PRESS, | ||
{ATTR_ENTITY_ID: ENTITY_ID}, | ||
blocking=True, | ||
) | ||
mock_palazzetti_client.set_fan_silent.assert_called_once() | ||
|
||
|
||
async def test_async_press_error( | ||
hass: HomeAssistant, | ||
mock_palazzetti_client: AsyncMock, | ||
mock_config_entry: MockConfigEntry, | ||
) -> None: | ||
"""Test pressing with error via service call.""" | ||
await setup_integration(hass, mock_config_entry) | ||
|
||
mock_palazzetti_client.set_fan_silent.side_effect = CommunicationError() | ||
error_message = "Could not connect to the device" | ||
with pytest.raises(HomeAssistantError, match=error_message): | ||
await hass.services.async_call( | ||
BUTTON_DOMAIN, | ||
SERVICE_PRESS, | ||
{ATTR_ENTITY_ID: ENTITY_ID}, | ||
blocking=True, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters