|
| 1 | +"""Platform for Miele button integration.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from dataclasses import dataclass |
| 6 | +import logging |
| 7 | +from typing import Final |
| 8 | + |
| 9 | +import aiohttp |
| 10 | + |
| 11 | +from homeassistant.components.button import ButtonEntity, ButtonEntityDescription |
| 12 | +from homeassistant.core import HomeAssistant |
| 13 | +from homeassistant.exceptions import HomeAssistantError |
| 14 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 15 | + |
| 16 | +from .const import DOMAIN, PROCESS_ACTION, MieleActions, MieleAppliance |
| 17 | +from .coordinator import MieleConfigEntry, MieleDataUpdateCoordinator |
| 18 | +from .entity import MieleEntity |
| 19 | + |
| 20 | +_LOGGER = logging.getLogger(__name__) |
| 21 | + |
| 22 | + |
| 23 | +@dataclass(frozen=True, kw_only=True) |
| 24 | +class MieleButtonDescription(ButtonEntityDescription): |
| 25 | + """Class describing Miele button entities.""" |
| 26 | + |
| 27 | + press_data: MieleActions |
| 28 | + |
| 29 | + |
| 30 | +@dataclass |
| 31 | +class MieleButtonDefinition: |
| 32 | + """Class for defining button entities.""" |
| 33 | + |
| 34 | + types: tuple[MieleAppliance, ...] |
| 35 | + description: MieleButtonDescription |
| 36 | + |
| 37 | + |
| 38 | +BUTTON_TYPES: Final[tuple[MieleButtonDefinition, ...]] = ( |
| 39 | + MieleButtonDefinition( |
| 40 | + types=( |
| 41 | + MieleAppliance.WASHING_MACHINE, |
| 42 | + MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL, |
| 43 | + MieleAppliance.TUMBLE_DRYER, |
| 44 | + MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL, |
| 45 | + MieleAppliance.DISHWASHER, |
| 46 | + MieleAppliance.OVEN, |
| 47 | + MieleAppliance.OVEN_MICROWAVE, |
| 48 | + MieleAppliance.STEAM_OVEN, |
| 49 | + MieleAppliance.MICROWAVE, |
| 50 | + MieleAppliance.WASHER_DRYER, |
| 51 | + MieleAppliance.STEAM_OVEN_COMBI, |
| 52 | + MieleAppliance.STEAM_OVEN_MICRO, |
| 53 | + MieleAppliance.STEAM_OVEN_MK2, |
| 54 | + MieleAppliance.DIALOG_OVEN, |
| 55 | + ), |
| 56 | + description=MieleButtonDescription( |
| 57 | + key="start", |
| 58 | + translation_key="start", |
| 59 | + press_data=MieleActions.START, |
| 60 | + entity_registry_enabled_default=False, |
| 61 | + ), |
| 62 | + ), |
| 63 | + MieleButtonDefinition( |
| 64 | + types=( |
| 65 | + MieleAppliance.WASHING_MACHINE, |
| 66 | + MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL, |
| 67 | + MieleAppliance.TUMBLE_DRYER, |
| 68 | + MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL, |
| 69 | + MieleAppliance.DISHWASHER, |
| 70 | + MieleAppliance.OVEN, |
| 71 | + MieleAppliance.OVEN_MICROWAVE, |
| 72 | + MieleAppliance.STEAM_OVEN, |
| 73 | + MieleAppliance.MICROWAVE, |
| 74 | + MieleAppliance.HOOD, |
| 75 | + MieleAppliance.WASHER_DRYER, |
| 76 | + MieleAppliance.STEAM_OVEN_COMBI, |
| 77 | + MieleAppliance.STEAM_OVEN_MICRO, |
| 78 | + MieleAppliance.STEAM_OVEN_MK2, |
| 79 | + MieleAppliance.DIALOG_OVEN, |
| 80 | + ), |
| 81 | + description=MieleButtonDescription( |
| 82 | + key="stop", |
| 83 | + translation_key="stop", |
| 84 | + press_data=MieleActions.STOP, |
| 85 | + entity_registry_enabled_default=False, |
| 86 | + ), |
| 87 | + ), |
| 88 | + MieleButtonDefinition( |
| 89 | + types=( |
| 90 | + MieleAppliance.WASHING_MACHINE, |
| 91 | + MieleAppliance.WASHING_MACHINE_SEMI_PROFESSIONAL, |
| 92 | + MieleAppliance.TUMBLE_DRYER, |
| 93 | + MieleAppliance.TUMBLE_DRYER_SEMI_PROFESSIONAL, |
| 94 | + MieleAppliance.DISHWASHER, |
| 95 | + MieleAppliance.WASHER_DRYER, |
| 96 | + ), |
| 97 | + description=MieleButtonDescription( |
| 98 | + key="pause", |
| 99 | + translation_key="pause", |
| 100 | + press_data=MieleActions.PAUSE, |
| 101 | + entity_registry_enabled_default=False, |
| 102 | + ), |
| 103 | + ), |
| 104 | +) |
| 105 | + |
| 106 | + |
| 107 | +async def async_setup_entry( |
| 108 | + hass: HomeAssistant, |
| 109 | + config_entry: MieleConfigEntry, |
| 110 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 111 | +) -> None: |
| 112 | + """Set up the button platform.""" |
| 113 | + coordinator = config_entry.runtime_data |
| 114 | + |
| 115 | + async_add_entities( |
| 116 | + MieleButton(coordinator, device_id, definition.description) |
| 117 | + for device_id, device in coordinator.data.devices.items() |
| 118 | + for definition in BUTTON_TYPES |
| 119 | + if device.device_type in definition.types |
| 120 | + ) |
| 121 | + |
| 122 | + |
| 123 | +class MieleButton(MieleEntity, ButtonEntity): |
| 124 | + """Representation of a Button.""" |
| 125 | + |
| 126 | + entity_description: MieleButtonDescription |
| 127 | + |
| 128 | + def __init__( |
| 129 | + self, |
| 130 | + coordinator: MieleDataUpdateCoordinator, |
| 131 | + device_id: str, |
| 132 | + description: MieleButtonDescription, |
| 133 | + ) -> None: |
| 134 | + """Initialize the button.""" |
| 135 | + super().__init__(coordinator, device_id, description) |
| 136 | + self.api = coordinator.api |
| 137 | + |
| 138 | + @property |
| 139 | + def available(self) -> bool: |
| 140 | + """Return the availability of the entity.""" |
| 141 | + |
| 142 | + return ( |
| 143 | + super().available |
| 144 | + and self.entity_description.press_data |
| 145 | + in self.coordinator.data.actions[self._device_id].process_actions |
| 146 | + ) |
| 147 | + |
| 148 | + async def async_press(self) -> None: |
| 149 | + """Press the button.""" |
| 150 | + _LOGGER.debug("Press: %s", self.entity_description.key) |
| 151 | + try: |
| 152 | + await self.api.send_action( |
| 153 | + self._device_id, |
| 154 | + {PROCESS_ACTION: self.entity_description.press_data}, |
| 155 | + ) |
| 156 | + except aiohttp.ClientResponseError as ex: |
| 157 | + raise HomeAssistantError( |
| 158 | + translation_domain=DOMAIN, |
| 159 | + translation_key="set_state_error", |
| 160 | + translation_placeholders={ |
| 161 | + "entity": self.entity_id, |
| 162 | + }, |
| 163 | + ) from ex |
0 commit comments