Home Assistant Unofficial Reference 2024.12.1
switch.py
Go to the documentation of this file.
1 """Philips TV menu switches."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 from homeassistant.components.switch import SwitchEntity
8 from homeassistant.core import HomeAssistant
9 from homeassistant.helpers.entity_platform import AddEntitiesCallback
10 
11 from . import PhilipsTVConfigEntry
12 from .coordinator import PhilipsTVDataUpdateCoordinator
13 from .entity import PhilipsJsEntity
14 
15 HUE_POWER_OFF = "Off"
16 HUE_POWER_ON = "On"
17 
18 
20  hass: HomeAssistant,
21  config_entry: PhilipsTVConfigEntry,
22  async_add_entities: AddEntitiesCallback,
23 ) -> None:
24  """Set up the configuration entry."""
25  coordinator = config_entry.runtime_data
26 
28 
29  if coordinator.api.json_feature_supported("ambilight", "Hue"):
31 
32 
34  """A Philips TV screen state switch."""
35 
36  _attr_translation_key = "screen_state"
37 
38  def __init__(
39  self,
40  coordinator: PhilipsTVDataUpdateCoordinator,
41  ) -> None:
42  """Initialize entity."""
43 
44  super().__init__(coordinator)
45 
46  self._attr_unique_id_attr_unique_id = f"{coordinator.unique_id}_screenstate"
47 
48  @property
49  def available(self) -> bool:
50  """Return true if entity is available."""
51  if not super().available:
52  return False
53  if not self.coordinator.api.on:
54  return False
55  return self.coordinator.api.powerstate == "On"
56 
57  @property
58  def is_on(self) -> bool:
59  """Return True if entity is on."""
60  return self.coordinator.api.screenstate == "On"
61 
62  async def async_turn_on(self, **kwargs: Any) -> None:
63  """Turn the entity on."""
64  await self.coordinator.api.setScreenState("On")
65 
66  async def async_turn_off(self, **kwargs: Any) -> None:
67  """Turn the entity off."""
68  await self.coordinator.api.setScreenState("Off")
69 
70 
72  """A Philips TV Ambi+Hue switch."""
73 
74  _attr_translation_key = "ambilight_hue"
75 
76  def __init__(
77  self,
78  coordinator: PhilipsTVDataUpdateCoordinator,
79  ) -> None:
80  """Initialize entity."""
81 
82  super().__init__(coordinator)
83 
84  self._attr_unique_id_attr_unique_id = f"{coordinator.unique_id}_ambi_hue"
85 
86  @property
87  def available(self) -> bool:
88  """Return true if entity is available."""
89  if not super().available:
90  return False
91  if not self.coordinator.api.on:
92  return False
93  return self.coordinator.api.powerstate == "On"
94 
95  @property
96  def is_on(self) -> bool:
97  """Return True if entity is on."""
98  return self.coordinator.api.huelamp_power == HUE_POWER_ON
99 
100  async def async_turn_on(self, **kwargs: Any) -> None:
101  """Turn the entity on."""
102  await self.coordinator.api.setHueLampPower(HUE_POWER_ON)
103  self.async_write_ha_stateasync_write_ha_state()
104 
105  async def async_turn_off(self, **kwargs: Any) -> None:
106  """Turn the entity off."""
107  await self.coordinator.api.setHueLampPower(HUE_POWER_OFF)
108  self.async_write_ha_stateasync_write_ha_state()
None __init__(self, PhilipsTVDataUpdateCoordinator coordinator)
Definition: switch.py:79
None __init__(self, PhilipsTVDataUpdateCoordinator coordinator)
Definition: switch.py:41
None async_setup_entry(HomeAssistant hass, PhilipsTVConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Definition: switch.py:23