Add smarty reset filters timer button (#129637)

This commit is contained in:
Marco 2024-11-09 13:32:00 +01:00 committed by GitHub
parent 1f43dc6676
commit 5f0f29704b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 179 additions and 1 deletions

View File

@ -30,7 +30,13 @@ CONFIG_SCHEMA = vol.Schema(
extra=vol.ALLOW_EXTRA,
)
PLATFORMS = [Platform.BINARY_SENSOR, Platform.FAN, Platform.SENSOR, Platform.SWITCH]
PLATFORMS = [
Platform.BINARY_SENSOR,
Platform.BUTTON,
Platform.FAN,
Platform.SENSOR,
Platform.SWITCH,
]
async def async_setup(hass: HomeAssistant, hass_config: ConfigType) -> bool:

View File

@ -0,0 +1,74 @@
"""Platform to control a Salda Smarty XP/XV ventilation unit."""
from __future__ import annotations
from collections.abc import Callable
from dataclasses import dataclass
import logging
from typing import Any
from pysmarty2 import Smarty
from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .coordinator import SmartyConfigEntry, SmartyCoordinator
from .entity import SmartyEntity
_LOGGER = logging.getLogger(__name__)
@dataclass(frozen=True, kw_only=True)
class SmartyButtonDescription(ButtonEntityDescription):
"""Class describing Smarty button."""
press_fn: Callable[[Smarty], bool | None]
ENTITIES: tuple[SmartyButtonDescription, ...] = (
SmartyButtonDescription(
key="reset_filters_timer",
translation_key="reset_filters_timer",
press_fn=lambda smarty: smarty.reset_filters_timer(),
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: SmartyConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up the Smarty Button Platform."""
coordinator = entry.runtime_data
async_add_entities(
SmartyButton(coordinator, description) for description in ENTITIES
)
class SmartyButton(SmartyEntity, ButtonEntity):
"""Representation of a Smarty Button."""
entity_description: SmartyButtonDescription
def __init__(
self,
coordinator: SmartyCoordinator,
entity_description: SmartyButtonDescription,
) -> None:
"""Initialize the entity."""
super().__init__(coordinator)
self.entity_description = entity_description
self._attr_unique_id = (
f"{coordinator.config_entry.entry_id}_{entity_description.key}"
)
async def async_press(self, **kwargs: Any) -> None:
"""Press the button."""
await self.hass.async_add_executor_job(
self.entity_description.press_fn, self.coordinator.client
)
await self.coordinator.async_refresh()

View File

@ -42,6 +42,11 @@
"name": "Boost state"
}
},
"button": {
"reset_filters_timer": {
"name": "Reset filters timer"
}
},
"sensor": {
"supply_air_temperature": {
"name": "Supply air temperature"

View File

@ -50,6 +50,7 @@ def mock_smarty() -> Generator[AsyncMock]:
client.filter_timer = 31
client.get_configuration_version.return_value = 111
client.get_software_version.return_value = 127
client.reset_filters_timer.return_value = True
yield client

View File

@ -0,0 +1,47 @@
# serializer version: 1
# name: test_all_entities[button.mock_title_reset_filters_timer-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.mock_title_reset_filters_timer',
'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': 'Reset filters timer',
'platform': 'smarty',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'reset_filters_timer',
'unique_id': '01JAZ5DPW8C62D620DGYNG2R8H_reset_filters_timer',
'unit_of_measurement': None,
})
# ---
# name: test_all_entities[button.mock_title_reset_filters_timer-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Mock Title Reset filters timer',
}),
'context': <ANY>,
'entity_id': 'button.mock_title_reset_filters_timer',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---

View File

@ -0,0 +1,45 @@
"""Tests for the Smarty button platform."""
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, 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
async def test_all_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
mock_smarty: AsyncMock,
mock_config_entry: MockConfigEntry,
entity_registry: er.EntityRegistry,
) -> None:
"""Test all entities."""
with patch("homeassistant.components.smarty.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_setting_value(
hass: HomeAssistant,
mock_smarty: AsyncMock,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test setting value."""
await setup_integration(hass, mock_config_entry)
await hass.services.async_call(
BUTTON_DOMAIN,
SERVICE_PRESS,
target={ATTR_ENTITY_ID: "button.mock_title_reset_filters_timer"},
blocking=True,
)
mock_smarty.reset_filters_timer.assert_called_once_with()