Triggers
Triggers are entities comparable with messages received from the Lifx bus.
If the comparison, made through the is_triggered method, returns True than the entity is considered to be triggered.
When the entity is triggered
if it is used by a Scheduler Trigger than its events are notified to Appliances
if it is used by a Performer its events are used to directly update an Appliance
- class lifx_plugin.trigger.Trigger(description: Any, events: List[home.Event] = None)
>>> import io >>> import json >>> json_trigger = ''' ... { ... "name": "SetColor", ... "fields": {"hue": 20, "saturation": 20, "brightness": 20, "kelvin": 3500, "duration": 1024}, ... "addresses": [["172.31.10.245", 56700]] ... } ... ''' >>> another_trigger = ''' ... { ... "name": "SetColor", ... "fields": {"hue": 20, "saturation": 20, "brightness": 20, "kelvin": 3500, "duration": 1024}, ... "addresses": [["172.31.10.166", 56700]] ... } ... ''' >>> match_trigger = ''' ... { ... "name": "SetColor", ... "fields": {"hue": 40, "saturation": 50, "brightness": 60, "kelvin": 3500, "duration": 1024}, ... "addresses": [["172.31.10.245", 56700]] ... } ... ''' >>> fd = io.StringIO(json_trigger) >>> trigger_data = json.load(fd) >>> trigger = Trigger(trigger_data) >>> trigger.state.hue 20 >>> trigger.state.saturation 20 >>> trigger.state.brightness 20 >>> fd = io.StringIO(another_trigger) >>> another_trigger_data = json.load(fd) >>> another_trigger = Trigger(another_trigger_data) >>> trigger.is_triggered(another_trigger) False >>> fd = io.StringIO(match_trigger) >>> match_trigger_data = json.load(fd) >>> match_trigger = Trigger(match_trigger_data) >>> trigger.is_triggered(match_trigger) True >>> print(match_trigger) Trigger SetColor {hue: 40, saturation: 50, brightness: 60, kelvin: 3500, rgb: (154, 128, 77), duration: 1024} from [172.31.10.245:56700]
>>> Trigger.State["name"] = "SetColor" >>> d = Trigger.make([["172.31.10.245", 56700]], [1, 2, 3]) >>> print(d) Trigger SetColor {hue: 0, saturation: 0, brightness: 0, kelvin: 0, rgb: (0, 0, 0), duration: 0} from [172.31.10.245:56700] >>> d.events [1, 2, 3]
- is_triggered(another_description: lifx_plugin.message.Description) bool
This trigger is triggered by the given protocol message Description?
- Parameters
another_description – a protocol message description
- Returns
bool
- classmethod make()
Make a protocol message Description given the arguments.
- Parameters
args –
kwargs –
- Returns
a protocol message Description
- classmethod make_from_yaml()
Make a protocol message Description given the yaml arguments.
- Parameters
args –
kwargs –
- Returns
a protocol message Description
State
- class lifx_plugin.trigger.State(*args, **kwargs)
>>> import io >>> import json >>> import home >>> import lifx_plugin >>> bus_event = ''' ... { ... "name": "State", ... "fields": {"hue": 8, "saturation": 88, "brightness": 88, "kelvin": 8888, "duration": 88888}, ... "addresses": [["172.31.10.245", 56700]] ... } ... ''' >>> fd = io.StringIO(bus_event) >>> description = lifx_plugin.Description(json.load(fd)) >>> trigger = lifx_plugin.trigger.State.make([["172.31.10.245", 56700]], [home.appliance.light.event.forced.Event.On]) >>> trigger.is_triggered(description) True >>> old_state = home.appliance.light.indoor.hue.state.on.State() >>> state = trigger.make_new_state_from(description, old_state) >>> "8" in str(state) True >>> "88" in str(state) True >>> "8888" in str(state) True
- State = {'addresses': [], 'fields': {}, 'name': 'State', 'type': 'lifx'}
- make_new_state_from(another_description: lifx_plugin.message.Description, old_state: lifx_plugin.Mixin) home.appliance.state.State
Given the protocol message Description, if this Trigger is triggered, notify Trigger’s Events to the given state and return new state.
- Parameters
another_description – a protocol message Description
old_state – an Appliance State to be updated
- Returns
an updated Appliance State