class ExampleIfRule ( Rule ): def register_triggers ( self ): return ( self .Philips_HUE_01,) def action ( self , * args ): if self .Philips_HUE_01.on: self .Philips_HUE_02.on = True self .Philips_HUE_03.on = True self .Philips_HUE_04.on = True (see this code in situ in the example_if_rule.py file in the pywot rule system demo directory

Rule

register_triggers

action

initial_state

register_triggers

action

registered_triggers

action

action

self.Philips_HUE_01

"

self.Philips_HUE_01.on

self.Philips_HUE_04.on = True

name

_01_turns_on_02__03

class ExampleWhileRule ( Rule ): def register_triggers ( self ): return ( self .Philips_HUE_01,) def action ( self , the_triggering_thing , the_changed_property_name , the_new_value ): if the_changed_property_name == ' on ' : self .Philips_HUE_02.on = the_new_value self .Philips_HUE_03.on = the_new_value self .Philips_HUE_04.on = the_new_value (see this code in situ in the example_while_rule.py file in the pywot rule system demo directory

action

action

on

the_new_value

on

class BondedBulbsRule ( Rule ): def register_triggers ( self ): return ( self .Philips_HUE_01, self .Philips_HUE_02, self .Philips_HUE_03, self .Philips_HUE_04, ) def action ( self , the_triggering_thing , the_changed_property_name , the_new_value ): for a_thing in self .triggering_things.values(): setattr (a_thing, the_changed_property_name, the_new_value) (see this code in situ in the bonded_rule.py file in the pywot rule system demo directory

action

the_changed_property_name

action

class HeartBeat ( TimeBasedTrigger ): def __init__ ( self , config , name , period_str # duration should be a integer in string form with an optional # H, h, M, m, S, s, D, d as a suffix to indicate units - default S ): super (HeartBeat, self ). __init__ (name) self .period = self .duration_str_to_seconds(period_str) async def trigger_dection_loop ( self ): logging.debug( ' Starting heartbeat timer %s ' , self .period) while True : await asyncio.sleep( self .period) logging.info( ' %s beats ' , self .name) self ._apply_rules() (see this code in situ in the rule_triggers.py file in the pywot directory

participating_rules

_apply_rules

action

HeartBeat

_apply_rules

period_str

the_rainbow_of_colors = deque([ ' #ff0000 ' , ' #ffaa00 ' , ' #aaff00 ' , ' #00ff00 ' , ' #0000ff ' , ' #aa00ff ' ]) class RainbowRule ( Rule ): def initial_state ( self ): self .participating_bulbs = ( self .Philips_HUE_01, self .Philips_HUE_02, self .Philips_HUE_03, self .Philips_HUE_04, self .Philips_HUE_05, self .Philips_HUE_06, ) for a_bulb, initial_color in zip ( self .participating_bulbs, the_rainbow_of_colors): a_bulb.on = True a_bulb.color = initial_color def register_triggers ( self ): self .heartbeat = HeartBeat( self .config, ' the heart ' , " 2s " ) return ( self .heartbeat, ) def action ( self , * args ): the_rainbow_of_colors.rotate( 1 ) for a_bulb, new_color in zip ( self .participating_bulbs, the_rainbow_of_colors): a_bulb.color = new_color (see this code in situ in the rainbow_rule.py file in the pywot rule system demo directory

intial_state

register_triggers

Heartbeat

action