from caspia.meadow.value import InRange
from .base import Characteristic, ServiceBase
[docs]class LightBase(ServiceBase):
""" Represents a light (of any type, lightbulb, LED etc). """
type = 'light'
is_on = Characteristic('bool', 'RWN')
toggle = Characteristic('void', 'W')
optional = {'brightness', 'hue', 'saturation'}
brightness = Characteristic('float', 'RWN', validate=InRange(0, 1))
hue = Characteristic('float', 'RWN', validate=InRange(0, 360))
saturation = Characteristic('float', 'RWN', validate=InRange(0, 1))
# TODO: color_temperature characteristic
[docs]class LightGroup(LightBase):
""" Utility class for light grouping. """
def __init__(self, name):
super().__init__(name)
self.lights = []
[docs] def add(self, *lights):
self.lights.extend(lights)
[docs] async def characteristic_write(self, characteristic, value, **kwargs):
if characteristic == self.is_on:
for light in self.lights:
await light.is_on.write(value)
elif characteristic == self.toggle:
is_on = await self.is_on.read()
await self.is_on.write(not is_on)
[docs] async def characteristic_read(self, characteristic, **kwargs):
if characteristic == self.is_on:
if len(self.lights) == 0:
return False
else:
return await self.lights[0].is_on.read()