Skip to content

Add button platform to miele #143508

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Apr 25, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions homeassistant/components/miele/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from .coordinator import MieleConfigEntry, MieleDataUpdateCoordinator

PLATFORMS: list[Platform] = [
Platform.BUTTON,
Platform.SENSOR,
]

Expand Down
166 changes: 166 additions & 0 deletions homeassistant/components/miele/button.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
"""Platform for Miele button integration."""

from __future__ import annotations

from dataclasses import dataclass
import logging
from typing import Final

import aiohttp

from homeassistant.components.button import ButtonEntity, ButtonEntityDescription
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback

from .const import DOMAIN, PROCESS_ACTION, MieleActions, MieleAppliance
from .coordinator import MieleConfigEntry, MieleDataUpdateCoordinator
from .entity import MieleEntity

_LOGGER = logging.getLogger(__name__)


@dataclass(frozen=True, kw_only=True)
class MieleButtonDescription(ButtonEntityDescription):
"""Class describing Miele button entities."""

press_data: dict[str, str | int | bool]


@dataclass
class MieleButtonDefinition:
"""Class for defining button entities."""

types: tuple[MieleAppliance, ...]
description: MieleButtonDescription


BUTTON_TYPES: Final[tuple[MieleButtonDefinition, ...]] = (
MieleButtonDefinition(
types=(
MieleAppliance.WASHING_MACHINE,
MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL,
MieleAppliance.TUMBLE_DRYER,
MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL,
MieleAppliance.DISHWASHER,
MieleAppliance.OVEN,
MieleAppliance.OVEN_MICROWAVE,
MieleAppliance.STEAM_OVEN,
MieleAppliance.MICROWAVE,
MieleAppliance.WASHER_DRYER,
MieleAppliance.STEAM_OVEN_COMBI,
MieleAppliance.STEAM_OVEN_MICRO,
MieleAppliance.STEAM_OVEN_MK2,
MieleAppliance.DIALOG_OVEN,
),
description=MieleButtonDescription(
key="start",
translation_key="start",
press_data={PROCESS_ACTION: MieleActions.START},
),
),
MieleButtonDefinition(
types=(
MieleAppliance.WASHING_MACHINE,
MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL,
MieleAppliance.TUMBLE_DRYER,
MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL,
MieleAppliance.DISHWASHER,
MieleAppliance.OVEN,
MieleAppliance.OVEN_MICROWAVE,
MieleAppliance.STEAM_OVEN,
MieleAppliance.MICROWAVE,
MieleAppliance.HOOD,
MieleAppliance.WASHER_DRYER,
MieleAppliance.STEAM_OVEN_COMBI,
MieleAppliance.STEAM_OVEN_MICRO,
MieleAppliance.STEAM_OVEN_MK2,
MieleAppliance.DIALOG_OVEN,
),
description=MieleButtonDescription(
key="stop",
translation_key="stop",
press_data={PROCESS_ACTION: MieleActions.STOP},
),
),
MieleButtonDefinition(
types=(
MieleAppliance.WASHING_MACHINE,
MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL,
MieleAppliance.TUMBLE_DRYER,
MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL,
MieleAppliance.DISHWASHER,
MieleAppliance.WASHER_DRYER,
),
description=MieleButtonDescription(
key="pause",
translation_key="pause",
press_data={PROCESS_ACTION: MieleActions.PAUSE},
),
),
)


async def async_setup_entry(
hass: HomeAssistant,
config_entry: MieleConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up the button platform."""
coordinator = config_entry.runtime_data

async_add_entities(
MieleButton(coordinator, device_id, definition.description)
for device_id, device in coordinator.data.devices.items()
for definition in BUTTON_TYPES
if device.device_type in definition.types
)


class MieleButton(MieleEntity, ButtonEntity):
"""Representation of a Button."""

entity_description: MieleButtonDescription

def __init__(
self,
coordinator: MieleDataUpdateCoordinator,
device_id: str,
description: MieleButtonDescription,
) -> None:
"""Initialize the button."""
super().__init__(coordinator, device_id, description)
self.api = coordinator.api

def _action_available(self, action: dict[str, str | int | bool]) -> bool:
"""Check if action is available according to API."""
return (
action[PROCESS_ACTION]
in self.coordinator.data.actions[self._device_id].process_actions
)

@property
def available(self) -> bool:
"""Return the availability of the entity."""

return super().available and self._action_available(
self.entity_description.press_data
)

async def async_press(self) -> None:
"""Press the button."""
_LOGGER.debug("Press: %s", self.entity_description.key)
if self._action_available(self.entity_description.press_data):
try:
await self.api.send_action(
self._device_id,
self.entity_description.press_data,
)
except aiohttp.ClientResponseError as ex:
raise HomeAssistantError(
translation_domain=DOMAIN,
translation_key="set_state_error",
translation_placeholders={
"entity": self.entity_id,
},
) from ex
15 changes: 15 additions & 0 deletions homeassistant/components/miele/icons.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"entity": {
"button": {
"start": {
"default": "mdi:play"
},
"stop": {
"default": "mdi:stop"
},
"pause": {
"default": "mdi:pause"
}
}
}
}
13 changes: 12 additions & 1 deletion homeassistant/components/miele/strings.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,17 @@
}
},
"entity": {
"button": {
"start": {
"name": "[%key:common::action::start%]"
},
"stop": {
"name": "[%key:common::action::stop%]"
},
"pause": {
"name": "[%key:common::action::pause%]"
}
},
"sensor": {
"status": {
"name": "Status",
Expand Down Expand Up @@ -147,7 +158,7 @@
"config_entry_not_ready": {
"message": "Error while loading the integration."
},
"set_switch_error": {
"set_state_error": {
"message": "Failed to set state for {entity}."
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"processAction": [],
"processAction": [1, 2, 3],
"light": [],
"ambientLight": [],
"startTime": [],
Expand Down
142 changes: 142 additions & 0 deletions tests/components/miele/snapshots/test_button.ambr
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
# serializer version: 1
# name: test_button_states[platforms0][button.washing_machine_pause-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': None,
'entity_id': 'button.washing_machine_pause',
'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': 'Pause',
'platform': 'miele',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'pause',
'unique_id': 'Dummy_Appliance_3-pause',
'unit_of_measurement': None,
})
# ---
# name: test_button_states[platforms0][button.washing_machine_pause-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Washing machine Pause',
}),
'context': <ANY>,
'entity_id': 'button.washing_machine_pause',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button_states[platforms0][button.washing_machine_start-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': None,
'entity_id': 'button.washing_machine_start',
'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': 'Start',
'platform': 'miele',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'start',
'unique_id': 'Dummy_Appliance_3-start',
'unit_of_measurement': None,
})
# ---
# name: test_button_states[platforms0][button.washing_machine_start-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Washing machine Start',
}),
'context': <ANY>,
'entity_id': 'button.washing_machine_start',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_button_states[platforms0][button.washing_machine_stop-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': None,
'entity_id': 'button.washing_machine_stop',
'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': 'Stop',
'platform': 'miele',
'previous_unique_id': None,
'supported_features': 0,
'translation_key': 'stop',
'unique_id': 'Dummy_Appliance_3-stop',
'unit_of_measurement': None,
})
# ---
# name: test_button_states[platforms0][button.washing_machine_stop-state]
StateSnapshot({
'attributes': ReadOnlyDict({
'friendly_name': 'Washing machine Stop',
}),
'context': <ANY>,
'entity_id': 'button.washing_machine_stop',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
Loading
Loading