Class: Ansible::Switch

Inherits:
Device
  • Object
show all
Defined in:
lib/ansible/devices/ansible_switch.rb

Overview

a Switch is a device controlled by a boolean state: False/Off/0 and True/On/1

Direct Known Subclasses

Dimmer

Instance Method Summary collapse

Methods inherited from Device

#initialize

Constructor Details

This class inherits a constructor from Ansible::Device

Instance Method Details



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/ansible/devices/ansible_switch.rb', line 33

def link
    switch = @hashmap[:switch]
    status = @hashmap[:switch_status]
    master = @hashmap[:master_control]
    unless [switch, master].select{ |v| not v.is_a? AnsibleValue }.empty?
        raise "#{self}.link: must supply AnsibleValues for :master_control and :switch!"
    end
    # map switch value updates to master_control 
    switch.add_callback(:onUpdate, self) { |sender, cb, args| 
        puts "   (#{sender.class}) #{sender} input value updated! args=#{args}"
        # convert value domains 
        cv = sender.as_canonical_value
        newval = master.to_protocol_value(cv)
        puts "   #{self} setting master #{master} +++ cv=#{cv} newval=#{newval}"
        master.set(newval)
    }
    # also update status value, if defined
    if status.is_a?AnsibleValue then
        puts "...also adding status feedback command #{status}"
        master.add_callback(:onUpdate, self) { |sender, cb, args|
            # convert value domains 
            cv = sender.as_canonical_value
            newval = master.to_protocol_value(cv)
            #
            status = @hashmap[:switch_status]
            puts "   updating on/off status value (#{status}) new val=#{newval}!"
            status.set(newval)
        }
    end
end