Home Assistant Unofficial Reference 2024.12.1
button.py
Go to the documentation of this file.
1 """Support for SwitchBee scenario button."""
2 
3 from switchbee.api.central_unit import SwitchBeeError
4 from switchbee.device import ApiStateCommand, DeviceType
5 
6 from homeassistant.components.button import ButtonEntity
7 from homeassistant.config_entries import ConfigEntry
8 from homeassistant.core import HomeAssistant
9 from homeassistant.exceptions import HomeAssistantError
10 from homeassistant.helpers.entity_platform import AddEntitiesCallback
11 
12 from .const import DOMAIN
13 from .coordinator import SwitchBeeCoordinator
14 from .entity import SwitchBeeEntity
15 
16 
18  hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
19 ) -> None:
20  """Set up Switchbee button."""
21  coordinator: SwitchBeeCoordinator = hass.data[DOMAIN][entry.entry_id]
23  SwitchBeeButton(switchbee_device, coordinator)
24  for switchbee_device in coordinator.data.values()
25  if switchbee_device.type == DeviceType.Scenario
26  )
27 
28 
30  """Representation of an Switchbee button."""
31 
32  async def async_press(self) -> None:
33  """Fire the scenario in the SwitchBee hub."""
34  try:
35  await self.coordinator.api.set_state(self._device.id, ApiStateCommand.ON)
36  except SwitchBeeError as exp:
37  raise HomeAssistantError(
38  f"Failed to fire scenario {self.name}, {exp!s}"
39  ) from exp
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: button.py:19