1 """Support for Xiaomi buttons."""
3 from __future__
import annotations
5 from dataclasses
import dataclass
7 from miio.integrations.vacuum.roborock.vacuum
import Consumable
12 ButtonEntityDescription,
27 from .entity
import XiaomiCoordinatedMiioEntity
30 ATTR_RESET_DUST_FILTER =
"reset_dust_filter"
31 ATTR_RESET_UPPER_FILTER =
"reset_upper_filter"
34 METHOD_VACUUM_RESET_CONSUMABLE =
"consumable_reset"
35 ATTR_RESET_VACUUM_MAIN_BRUSH =
"reset_vacuum_main_brush"
36 ATTR_RESET_VACUUM_SIDE_BRUSH =
"reset_vacuum_side_brush"
37 ATTR_RESET_VACUUM_FILTER =
"reset_vacuum_filter"
38 ATTR_RESET_VACUUM_SENSOR_DIRTY =
"reset_vacuum_sensor_dirty"
41 @dataclass(frozen=True)
43 """A class that describes button entities."""
45 method_press: str =
""
46 method_press_params: Consumable |
None =
None
47 method_press_error_message: str =
""
53 key=ATTR_RESET_DUST_FILTER,
54 translation_key=ATTR_RESET_DUST_FILTER,
55 icon=
"mdi:air-filter",
56 method_press=
"reset_dust_filter",
57 method_press_error_message=
"Resetting the dust filter lifetime failed",
58 entity_category=EntityCategory.CONFIG,
61 key=ATTR_RESET_UPPER_FILTER,
62 translation_key=ATTR_RESET_UPPER_FILTER,
63 icon=
"mdi:air-filter",
64 method_press=
"reset_upper_filter",
65 method_press_error_message=
"Resetting the upper filter lifetime failed.",
66 entity_category=EntityCategory.CONFIG,
70 key=ATTR_RESET_VACUUM_MAIN_BRUSH,
71 translation_key=ATTR_RESET_VACUUM_MAIN_BRUSH,
73 method_press=METHOD_VACUUM_RESET_CONSUMABLE,
74 method_press_params=Consumable.MainBrush,
75 method_press_error_message=
"Resetting the main brush lifetime failed.",
76 entity_category=EntityCategory.CONFIG,
79 key=ATTR_RESET_VACUUM_SIDE_BRUSH,
80 translation_key=ATTR_RESET_VACUUM_SIDE_BRUSH,
82 method_press=METHOD_VACUUM_RESET_CONSUMABLE,
83 method_press_params=Consumable.SideBrush,
84 method_press_error_message=
"Resetting the side brush lifetime failed.",
85 entity_category=EntityCategory.CONFIG,
88 key=ATTR_RESET_VACUUM_FILTER,
89 translation_key=ATTR_RESET_VACUUM_FILTER,
90 icon=
"mdi:air-filter",
91 method_press=METHOD_VACUUM_RESET_CONSUMABLE,
92 method_press_params=Consumable.Filter,
93 method_press_error_message=
"Resetting the filter lifetime failed.",
94 entity_category=EntityCategory.CONFIG,
97 key=ATTR_RESET_VACUUM_SENSOR_DIRTY,
98 translation_key=ATTR_RESET_VACUUM_SENSOR_DIRTY,
99 icon=
"mdi:eye-outline",
100 method_press=METHOD_VACUUM_RESET_CONSUMABLE,
101 method_press_params=Consumable.SensorDirty,
102 method_press_error_message=
"Resetting the sensor lifetime failed.",
103 entity_category=EntityCategory.CONFIG,
107 BUTTONS_FOR_VACUUM = (
108 ATTR_RESET_VACUUM_MAIN_BRUSH,
109 ATTR_RESET_VACUUM_SIDE_BRUSH,
110 ATTR_RESET_VACUUM_FILTER,
111 ATTR_RESET_VACUUM_SENSOR_DIRTY,
114 MODEL_TO_BUTTON_MAP: dict[str, tuple[str, ...]] = {
115 MODEL_AIRFRESH_A1: (ATTR_RESET_DUST_FILTER,),
116 MODEL_AIRFRESH_T2017: (
117 ATTR_RESET_DUST_FILTER,
118 ATTR_RESET_UPPER_FILTER,
120 **{model: BUTTONS_FOR_VACUUM
for model
in MODELS_VACUUM},
126 config_entry: ConfigEntry,
127 async_add_entities: AddEntitiesCallback,
129 """Set up the button from a config entry."""
130 model = config_entry.data[CONF_MODEL]
132 if model
not in MODEL_TO_BUTTON_MAP:
136 buttons = MODEL_TO_BUTTON_MAP[model]
137 unique_id = config_entry.unique_id
138 device = hass.data[DOMAIN][config_entry.entry_id][KEY_DEVICE]
139 coordinator = hass.data[DOMAIN][config_entry.entry_id][KEY_COORDINATOR]
141 for description
in BUTTON_TYPES:
142 if description.key
not in buttons:
149 f
"{description.key}_{unique_id}",
159 """A button implementation for Xiaomi."""
161 entity_description: XiaomiMiioButtonDescription
163 _attr_device_class = ButtonDeviceClass.RESTART
165 def __init__(self, device, entry, unique_id, coordinator, description):
166 """Initialize the plug switch."""
167 super().
__init__(device, entry, unique_id, coordinator)
171 """Press the button."""
172 method = getattr(self._device, self.
entity_descriptionentity_description.method_press)
174 if params
is not None:
175 await self._try_command(
179 await self._try_command(