Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Support for Bond generic devices."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from aiohttp.client_exceptions import ClientResponseError
8 from bond_async import Action, DeviceType
9 import voluptuous as vol
10 
11 from homeassistant.components.switch import SwitchEntity
12 from homeassistant.core import HomeAssistant
13 from homeassistant.exceptions import HomeAssistantError
14 from homeassistant.helpers import config_validation as cv, entity_platform
15 from homeassistant.helpers.entity_platform import AddEntitiesCallback
16 
17 from . import BondConfigEntry
18 from .const import ATTR_POWER_STATE, SERVICE_SET_POWER_TRACKED_STATE
19 from .entity import BondEntity
20 
21 
23  hass: HomeAssistant,
24  entry: BondConfigEntry,
25  async_add_entities: AddEntitiesCallback,
26 ) -> None:
27  """Set up Bond generic devices."""
28  data = entry.runtime_data
29  platform = entity_platform.async_get_current_platform()
30  platform.async_register_entity_service(
31  SERVICE_SET_POWER_TRACKED_STATE,
32  {vol.Required(ATTR_POWER_STATE): cv.boolean},
33  "async_set_power_belief",
34  )
35 
37  BondSwitch(data, device)
38  for device in data.hub.devices
39  if DeviceType.is_generic(device.type)
40  )
41 
42 
44  """Representation of a Bond generic device."""
45 
46  def _apply_state(self) -> None:
47  self._attr_is_on_attr_is_on = self._device_device.state.get("power") == 1
48 
49  async def async_turn_on(self, **kwargs: Any) -> None:
50  """Turn the device on."""
51  await self._bond_bond.action(self._device_id_device_id, Action.turn_on())
52 
53  async def async_turn_off(self, **kwargs: Any) -> None:
54  """Turn the device off."""
55  await self._bond_bond.action(self._device_id_device_id, Action.turn_off())
56 
57  async def async_set_power_belief(self, power_state: bool) -> None:
58  """Set switch power belief."""
59  try:
60  await self._bond_bond.action(
61  self._device_id_device_id, Action.set_power_state_belief(power_state)
62  )
63  except ClientResponseError as ex:
64  raise HomeAssistantError(
65  "The bond API returned an error calling set_power_state_belief for"
66  f" {self.entity_id}. Code: {ex.status} Message: {ex.message}"
67  ) from ex
None async_set_power_belief(self, bool power_state)
Definition: switch.py:57
None async_turn_off(self, **Any kwargs)
Definition: switch.py:53
None async_turn_on(self, **Any kwargs)
Definition: switch.py:49
None async_setup_entry(HomeAssistant hass, BondConfigEntry entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:26