Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for Greenwave Reality (TCP Connected) lights."""
2 
3 from __future__ import annotations
4 
5 from datetime import timedelta
6 import logging
7 import os
8 from typing import Any
9 
10 import greenwavereality as greenwave
11 import voluptuous as vol
12 
14  ATTR_BRIGHTNESS,
15  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
16  ColorMode,
17  LightEntity,
18 )
19 from homeassistant.const import CONF_HOST
20 from homeassistant.core import HomeAssistant
22 from homeassistant.helpers.entity_platform import AddEntitiesCallback
23 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
24 from homeassistant.util import Throttle
25 
26 _LOGGER = logging.getLogger(__name__)
27 
28 CONF_VERSION = "version"
29 
30 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
31  {vol.Required(CONF_HOST): cv.string, vol.Required(CONF_VERSION): cv.positive_int}
32 )
33 
34 MIN_TIME_BETWEEN_UPDATES = timedelta(minutes=1)
35 
36 
38  hass: HomeAssistant,
39  config: ConfigType,
40  add_entities: AddEntitiesCallback,
41  discovery_info: DiscoveryInfoType | None = None,
42 ) -> None:
43  """Set up the Greenwave Reality Platform."""
44  host = config.get(CONF_HOST)
45  tokenfilename = hass.config.path(".greenwave")
46  if config.get(CONF_VERSION) == 3:
47  if os.path.exists(tokenfilename):
48  with open(tokenfilename, encoding="utf8") as tokenfile:
49  token = tokenfile.read()
50  else:
51  try:
52  token = greenwave.grab_token(host, "hass", "homeassistant")
53  except PermissionError:
54  _LOGGER.error("The Gateway Is Not In Sync Mode")
55  raise
56  with open(tokenfilename, "w+", encoding="utf8") as tokenfile:
57  tokenfile.write(token)
58  else:
59  token = None
60  bulbs = greenwave.grab_bulbs(host, token)
62  GreenwaveLight(device, host, token, GatewayData(host, token))
63  for device in bulbs.values()
64  )
65 
66 
68  """Representation of an Greenwave Reality Light."""
69 
70  _attr_color_mode = ColorMode.BRIGHTNESS
71  _attr_supported_color_modes = {ColorMode.BRIGHTNESS}
72 
73  def __init__(self, light, host, token, gatewaydata):
74  """Initialize a Greenwave Reality Light."""
75  self._did_did = int(light["did"])
76  self._attr_name_attr_name = light["name"]
77  self._state_state = int(light["state"])
78  self._attr_brightness_attr_brightness = greenwave.hass_brightness(light)
79  self._host_host = host
80  self._attr_available_attr_available = greenwave.check_online(light)
81  self._token_token = token
82  self._gatewaydata_gatewaydata = gatewaydata
83 
84  @property
85  def is_on(self):
86  """Return true if light is on."""
87  return self._state_state
88 
89  def turn_on(self, **kwargs: Any) -> None:
90  """Instruct the light to turn on."""
91  temp_brightness = int((kwargs.get(ATTR_BRIGHTNESS, 255) / 255) * 100)
92  greenwave.set_brightness(self._host_host, self._did_did, temp_brightness, self._token_token)
93  greenwave.turn_on(self._host_host, self._did_did, self._token_token)
94 
95  def turn_off(self, **kwargs: Any) -> None:
96  """Instruct the light to turn off."""
97  greenwave.turn_off(self._host_host, self._did_did, self._token_token)
98 
99  def update(self) -> None:
100  """Fetch new state data for this light."""
101  self._gatewaydata_gatewaydata.update()
102  bulbs = self._gatewaydata_gatewaydata.greenwave
103 
104  self._state_state = int(bulbs[self._did_did]["state"])
105  self._attr_brightness_attr_brightness = greenwave.hass_brightness(bulbs[self._did_did])
106  self._attr_available_attr_available = greenwave.check_online(bulbs[self._did_did])
107  self._attr_name_attr_name = bulbs[self._did_did]["name"]
108 
109 
111  """Handle Gateway data and limit updates."""
112 
113  def __init__(self, host, token):
114  """Initialize the data object."""
115  self._host_host = host
116  self._token_token = token
117  self._greenwave_greenwave = greenwave.grab_bulbs(host, token)
118 
119  @property
120  def greenwave(self):
121  """Return Gateway API object."""
122  return self._greenwave_greenwave
123 
124  @Throttle(MIN_TIME_BETWEEN_UPDATES)
125  def update(self):
126  """Get the latest data from the gateway."""
127  self._greenwave_greenwave = greenwave.grab_bulbs(self._host_host, self._token_token)
128  return self._greenwave_greenwave
def __init__(self, light, host, token, gatewaydata)
Definition: light.py:73
None add_entities(AsusWrtRouter router, AddEntitiesCallback async_add_entities, set[str] tracked)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities, DiscoveryInfoType|None discovery_info=None)
Definition: light.py:42
None open(self, **Any kwargs)
Definition: lock.py:86