-
-
Notifications
You must be signed in to change notification settings - Fork 34.2k
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
Add button platform to miele #143508
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
563dd9b
WIP Button platform
astrandb a9ffb31
Add button platform
astrandb 5a0aa6c
Merge branch 'dev' into MieleButton
astrandb c2ad3f0
Disable by default, Address review , update tests
astrandb de4ee6a
Merge branch 'dev' into MieleButton
astrandb 2373332
Follow review comments
astrandb a148cc9
Merge branch 'dev' into MieleButton
astrandb File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,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): | ||
astrandb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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 |
This file contains hidden or 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,15 @@ | ||
{ | ||
"entity": { | ||
"button": { | ||
"start": { | ||
"default": "mdi:play" | ||
}, | ||
"stop": { | ||
"default": "mdi:stop" | ||
}, | ||
"pause": { | ||
"default": "mdi:pause" | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 hidden or 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"processAction": [], | ||
"processAction": [1, 2, 3], | ||
"light": [], | ||
"ambientLight": [], | ||
"startTime": [], | ||
|
This file contains hidden or 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,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', | ||
}) | ||
# --- |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.