Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for Xiaomi buttons."""
2 
3 from __future__ import annotations
4 
5 from dataclasses import dataclass
6 
7 from miio.integrations.vacuum.roborock.vacuum import Consumable
8 
10  ButtonDeviceClass,
11  ButtonEntity,
12  ButtonEntityDescription,
13 )
14 from homeassistant.config_entries import ConfigEntry
15 from homeassistant.const import CONF_MODEL, EntityCategory
16 from homeassistant.core import HomeAssistant
17 from homeassistant.helpers.entity_platform import AddEntitiesCallback
18 
19 from .const import (
20  DOMAIN,
21  KEY_COORDINATOR,
22  KEY_DEVICE,
23  MODEL_AIRFRESH_A1,
24  MODEL_AIRFRESH_T2017,
25  MODELS_VACUUM,
26 )
27 from .entity import XiaomiCoordinatedMiioEntity
28 
29 # Fans
30 ATTR_RESET_DUST_FILTER = "reset_dust_filter"
31 ATTR_RESET_UPPER_FILTER = "reset_upper_filter"
32 
33 # Vacuums
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"
39 
40 
41 @dataclass(frozen=True)
43  """A class that describes button entities."""
44 
45  method_press: str = ""
46  method_press_params: Consumable | None = None
47  method_press_error_message: str = ""
48 
49 
50 BUTTON_TYPES = (
51  # Fans
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,
59  ),
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,
67  ),
68  # Vacuums
70  key=ATTR_RESET_VACUUM_MAIN_BRUSH,
71  translation_key=ATTR_RESET_VACUUM_MAIN_BRUSH,
72  icon="mdi: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,
77  ),
79  key=ATTR_RESET_VACUUM_SIDE_BRUSH,
80  translation_key=ATTR_RESET_VACUUM_SIDE_BRUSH,
81  icon="mdi: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,
86  ),
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,
95  ),
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,
104  ),
105 )
106 
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,
112 )
113 
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,
119  ),
120  **{model: BUTTONS_FOR_VACUUM for model in MODELS_VACUUM},
121 }
122 
123 
125  hass: HomeAssistant,
126  config_entry: ConfigEntry,
127  async_add_entities: AddEntitiesCallback,
128 ) -> None:
129  """Set up the button from a config entry."""
130  model = config_entry.data[CONF_MODEL]
131 
132  if model not in MODEL_TO_BUTTON_MAP:
133  return
134 
135  entities = []
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]
140 
141  for description in BUTTON_TYPES:
142  if description.key not in buttons:
143  continue
144 
145  entities.append(
147  device,
148  config_entry,
149  f"{description.key}_{unique_id}",
150  coordinator,
151  description,
152  )
153  )
154 
155  async_add_entities(entities)
156 
157 
159  """A button implementation for Xiaomi."""
160 
161  entity_description: XiaomiMiioButtonDescription
162 
163  _attr_device_class = ButtonDeviceClass.RESTART
164 
165  def __init__(self, device, entry, unique_id, coordinator, description):
166  """Initialize the plug switch."""
167  super().__init__(device, entry, unique_id, coordinator)
168  self.entity_descriptionentity_description = description
169 
170  async def async_press(self) -> None:
171  """Press the button."""
172  method = getattr(self._device, self.entity_descriptionentity_description.method_press)
173  params = self.entity_descriptionentity_description.method_press_params
174  if params is not None:
175  await self._try_command(
176  self.entity_descriptionentity_description.method_press_error_message, method, params
177  )
178  else:
179  await self._try_command(
180  self.entity_descriptionentity_description.method_press_error_message, method
181  )
def __init__(self, device, entry, unique_id, coordinator, description)
Definition: button.py:165
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:128