Home Assistant Unofficial Reference 2024.12.1
light.py
Go to the documentation of this file.
1 """Support for FutureNow Ethernet unit outputs as Lights."""
2 
3 from __future__ import annotations
4 
5 from typing import Any
6 
7 import pyfnip
8 import voluptuous as vol
9 
11  ATTR_BRIGHTNESS,
12  PLATFORM_SCHEMA as LIGHT_PLATFORM_SCHEMA,
13  ColorMode,
14  LightEntity,
15 )
16 from homeassistant.const import CONF_DEVICES, CONF_HOST, CONF_NAME, CONF_PORT
17 from homeassistant.core import HomeAssistant
19 from homeassistant.helpers.entity_platform import AddEntitiesCallback
20 from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
21 
22 CONF_DRIVER = "driver"
23 CONF_DRIVER_FNIP6X10AD = "FNIP6x10ad"
24 CONF_DRIVER_FNIP8X10A = "FNIP8x10a"
25 CONF_DRIVER_TYPES = [CONF_DRIVER_FNIP6X10AD, CONF_DRIVER_FNIP8X10A]
26 
27 DEVICE_SCHEMA = vol.Schema(
28  {
29  vol.Required(CONF_NAME): cv.string,
30  vol.Optional("dimmable", default=False): cv.boolean,
31  }
32 )
33 
34 PLATFORM_SCHEMA = LIGHT_PLATFORM_SCHEMA.extend(
35  {
36  vol.Required(CONF_DRIVER): vol.In(CONF_DRIVER_TYPES),
37  vol.Required(CONF_HOST): cv.string,
38  vol.Required(CONF_PORT): cv.port,
39  vol.Required(CONF_DEVICES): {cv.string: DEVICE_SCHEMA},
40  }
41 )
42 
43 
45  hass: HomeAssistant,
46  config: ConfigType,
47  add_entities: AddEntitiesCallback,
48  discovery_info: DiscoveryInfoType | None = None,
49 ) -> None:
50  """Set up the light platform for each FutureNow unit."""
51  lights = []
52  for channel, device_config in config[CONF_DEVICES].items():
53  device = {}
54  device["name"] = device_config[CONF_NAME]
55  device["dimmable"] = device_config["dimmable"]
56  device["channel"] = channel
57  device["driver"] = config[CONF_DRIVER]
58  device["host"] = config[CONF_HOST]
59  device["port"] = config[CONF_PORT]
60  lights.append(FutureNowLight(device))
61 
62  add_entities(lights, True)
63 
64 
65 def to_futurenow_level(level):
66  """Convert the given Home Assistant light level (0-255) to FutureNow (0-100)."""
67  return round((level * 100) / 255)
68 
69 
70 def to_hass_level(level):
71  """Convert the given FutureNow (0-100) light level to Home Assistant (0-255)."""
72  return int((level * 255) / 100)
73 
74 
76  """Representation of an FutureNow light."""
77 
78  def __init__(self, device):
79  """Initialize the light."""
80  self._name_name = device["name"]
81  self._dimmable_dimmable = device["dimmable"]
82  self._channel_channel = device["channel"]
83  self._brightness_brightness = None
84  self._last_brightness_last_brightness = 255
85  self._state_state = None
86 
87  if device["driver"] == CONF_DRIVER_FNIP6X10AD:
88  self._light_light = pyfnip.FNIP6x2adOutput(
89  device["host"], device["port"], self._channel_channel
90  )
91  if device["driver"] == CONF_DRIVER_FNIP8X10A:
92  self._light_light = pyfnip.FNIP8x10aOutput(
93  device["host"], device["port"], self._channel_channel
94  )
95 
96  @property
97  def name(self):
98  """Return the name of the device if any."""
99  return self._name_name
100 
101  @property
102  def is_on(self):
103  """Return true if device is on."""
104  return self._state_state
105 
106  @property
107  def brightness(self):
108  """Return the brightness of this light between 0..255."""
109  return self._brightness_brightness
110 
111  @property
112  def color_mode(self) -> ColorMode:
113  """Return the color mode of the light."""
114  if self._dimmable_dimmable:
115  return ColorMode.BRIGHTNESS
116  return ColorMode.ONOFF
117 
118  @property
119  def supported_color_modes(self) -> set[ColorMode]:
120  """Flag supported color modes."""
121  return {self.color_modecolor_modecolor_mode}
122 
123  def turn_on(self, **kwargs: Any) -> None:
124  """Turn the light on."""
125  if self._dimmable_dimmable:
126  level = kwargs.get(ATTR_BRIGHTNESS, self._last_brightness_last_brightness)
127  else:
128  level = 255
129  self._light_light.turn_on(to_futurenow_level(level))
130 
131  def turn_off(self, **kwargs: Any) -> None:
132  """Turn the light off."""
133  self._light_light.turn_off()
134  if self._brightness_brightness:
135  self._last_brightness_last_brightness = self._brightness_brightness
136 
137  def update(self) -> None:
138  """Fetch new state data for this light."""
139  state = int(self._light_light.is_on())
140  self._state_state = bool(state)
141  self._brightness_brightness = to_hass_level(state)
ColorMode|str|None color_mode(self)
Definition: __init__.py:909
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:49