Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for buttons."""
2 
3 from __future__ import annotations
4 
5 from homeassistant import config_entries
6 from homeassistant.components.button import ButtonEntity
7 from homeassistant.const import EntityCategory
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import HomeAssistantError
10 from homeassistant.helpers.device_registry import DeviceInfo
11 from homeassistant.helpers.entity_platform import AddEntitiesCallback
12 from homeassistant.helpers.typing import ConfigType
13 
14 from .const import CONF_PROJECT_ID, CONF_SERVICE_ACCOUNT, DATA_CONFIG, DOMAIN
15 from .http import GoogleConfig
16 
17 
19  hass: HomeAssistant,
20  config_entry: config_entries.ConfigEntry,
21  async_add_entities: AddEntitiesCallback,
22 ) -> None:
23  """Set up the platform."""
24  yaml_config: ConfigType = hass.data[DOMAIN][DATA_CONFIG]
25  google_config: GoogleConfig = hass.data[DOMAIN][config_entry.entry_id]
26 
27  entities = []
28 
29  if CONF_SERVICE_ACCOUNT in yaml_config:
30  entities.append(SyncButton(config_entry.data[CONF_PROJECT_ID], google_config))
31 
32  async_add_entities(entities)
33 
34 
36  """Representation of a synchronization button."""
37 
38  _attr_has_entity_name = True
39  _attr_translation_key = "sync_devices"
40 
41  def __init__(self, project_id: str, google_config: GoogleConfig) -> None:
42  """Initialize button."""
43  super().__init__()
44  self._google_config_google_config = google_config
45  self._attr_entity_category_attr_entity_category = EntityCategory.DIAGNOSTIC
46  self._attr_unique_id_attr_unique_id = f"{project_id}_sync"
47  self._attr_device_info_attr_device_info = DeviceInfo(
48  identifiers={(DOMAIN, project_id)},
49  name="Google Assistant",
50  )
51 
52  async def async_press(self) -> None:
53  """Press the button."""
54  assert self._context_context
55  agent_user_id = self._google_config_google_config.get_agent_user_id_from_context(
56  self._context_context
57  )
58  result = await self._google_config_google_config.async_sync_entities(agent_user_id)
59  if result != 200:
60  raise HomeAssistantError(
61  f"Unable to sync devices with result code: {result}, check log for more"
62  " info."
63  )
None __init__(self, str project_id, GoogleConfig google_config)
Definition: button.py:41
None async_setup_entry(HomeAssistant hass, config_entries.ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: button.py:22