1 """Support for lights through the SmartThings cloud API."""
3 from __future__
import annotations
6 from collections.abc
import Sequence
9 from pysmartthings
import Capability
26 from .const
import DATA_BROKERS, DOMAIN
27 from .entity
import SmartThingsEntity
32 config_entry: ConfigEntry,
33 async_add_entities: AddEntitiesCallback,
35 """Add lights for a config entry."""
36 broker = hass.data[DOMAIN][DATA_BROKERS][config_entry.entry_id]
40 for device
in broker.devices.values()
41 if broker.any_assigned(device.device_id,
"light")
48 """Return all capabilities supported if minimum required are present."""
51 Capability.switch_level,
52 Capability.color_control,
53 Capability.color_temperature,
56 if Capability.switch
not in capabilities:
59 light_capabilities = [
60 Capability.color_control,
61 Capability.color_temperature,
62 Capability.switch_level,
64 if any(capability
in capabilities
for capability
in light_capabilities):
70 """Convert a value to a different scale."""
71 return round(value * target_scale / value_scale, round_digits)
75 """Define a SmartThings Light."""
77 _attr_supported_color_modes: set[ColorMode]
82 _attr_max_mireds = 500
87 _attr_min_mireds = 111
90 """Initialize a SmartThingsLight."""
96 """Get features supported by the device."""
99 if Capability.color_temperature
in self.
_device_device.capabilities:
100 color_modes.add(ColorMode.COLOR_TEMP)
102 if Capability.color_control
in self.
_device_device.capabilities:
103 color_modes.add(ColorMode.HS)
105 if not color_modes
and Capability.switch_level
in self.
_device_device.capabilities:
106 color_modes.add(ColorMode.BRIGHTNESS)
108 color_modes.add(ColorMode.ONOFF)
113 """Get features supported by the device."""
116 if Capability.switch_level
in self.
_device_device.capabilities:
117 features |= LightEntityFeature.TRANSITION
122 """Turn the light on."""
125 if ATTR_COLOR_TEMP
in kwargs:
128 if ATTR_HS_COLOR
in kwargs:
129 tasks.append(self.
async_set_colorasync_set_color(kwargs[ATTR_HS_COLOR]))
132 await asyncio.gather(*tasks)
135 if ATTR_BRIGHTNESS
in kwargs:
137 kwargs[ATTR_BRIGHTNESS], kwargs.get(ATTR_TRANSITION, 0)
140 await self.
_device_device.switch_on(set_status=
True)
147 """Turn the light off."""
149 if ATTR_TRANSITION
in kwargs:
152 await self.
_device_device.switch_off(set_status=
True)
159 """Update entity attributes when the device status has changed."""
168 self.
_device_device.status.color_temperature
174 self.
_device_device.status.saturation,
178 """Set the color of the device."""
180 hue =
max(
min(hue, 100.0), 0.0)
181 saturation =
max(
min(
float(hs_color[1]), 100.0), 0.0)
182 await self.
_device_device.set_color(hue, saturation, set_status=
True)
185 """Set the color temperature of the device."""
186 kelvin = color_util.color_temperature_mired_to_kelvin(value)
187 kelvin =
max(
min(kelvin, 30000), 1)
188 await self.
_device_device.set_color_temperature(kelvin, set_status=
True)
191 """Set the brightness of the light over transition."""
195 level = 1
if level == 0
and brightness > 0
else level
196 level =
max(
min(level, 100), 0)
197 duration =
int(transition)
198 await self.
_device_device.set_level(level, duration, set_status=
True)
202 """Return the color mode of the light."""
210 return ColorMode.COLOR_TEMP
214 """Return true if light is on."""
215 return self.
_device_device.status.switch
def async_set_color(self, hs_color)
None async_turn_off(self, **Any kwargs)
def _determine_color_modes(self)
LightEntityFeature _determine_features(self)
None async_turn_on(self, **Any kwargs)
ColorMode color_mode(self)
def async_set_level(self, int brightness, int transition)
def async_set_color_temp(self, float value)
_attr_supported_color_modes
def __init__(self, device)
None async_schedule_update_ha_state(self, bool force_refresh=False)
bool brightness_supported(Iterable[ColorMode|str]|None color_modes)
None async_setup_entry(HomeAssistant hass, ConfigEntry config_entry, AddEntitiesCallback async_add_entities)
Sequence[str]|None get_capabilities(Sequence[str] capabilities)
def convert_scale(value, value_scale, target_scale, round_digits=4)