Class: Wemote::Switch

Inherits:
Object
  • Object
show all
Defined in:
lib/wemote/switch.rb

Overview

This class encapsulates an individual Wemo Switch. It provides methods for getting and setting the switch’s state, as well as a #toggle! method for convenience. Finally, it provides the #poll method, which accepts a block to be executed any time the switch changes state.

Direct Known Subclasses

Insight

Constant Summary collapse

GET_HEADERS =
{
  "SOAPACTION"   => '"urn:Belkin:service:basicevent:1#GetBinaryState"',
  "Content-type" => 'text/xml; charset="utf-8"'
}
SET_HEADERS =
{
  "SOAPACTION"   => '"urn:Belkin:service:basicevent:1#SetBinaryState"',
  "Content-type" => 'text/xml; charset="utf-8"'
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port = nil) ⇒ Switch



60
61
62
63
# File 'lib/wemote/switch.rb', line 60

def initialize(host,port=nil)
  @host, @port = host, port
  set_meta
end

Instance Attribute Details

#nameObject

Returns the value of attribute name.



58
59
60
# File 'lib/wemote/switch.rb', line 58

def name
  @name
end

Class Method Details

.all(refresh = false) ⇒ Array

Returns all Switches detected on the local network



34
35
36
37
# File 'lib/wemote/switch.rb', line 34

def all(refresh=false)
  @switches = nil if refresh
  @switches ||= Wemote::Collection::Switch.new(discover)
end

.device_typeObject



26
27
28
# File 'lib/wemote/switch.rb', line 26

def device_type
  'urn:Belkin:device:controllee:1'
end

.find(name) ⇒ Wemote::Switch

Returns a Switch of a given name



43
44
45
# File 'lib/wemote/switch.rb', line 43

def find(name)
  all.detect{|s|s.name == name}
end

Instance Method Details

#device_typeObject



65
66
67
# File 'lib/wemote/switch.rb', line 65

def device_type 
  'urn:Belkin:device:controllee:1'
end

#off!Object

Turn the Switch off



75
76
77
# File 'lib/wemote/switch.rb', line 75

def off!     
  set_state(0)       
end

#off?Boolean

Return whether the Switch is off



87
88
89
# File 'lib/wemote/switch.rb', line 87

def off?     
  get_state == :off
end

#on!Object

Turn the Switch on



80
81
82
# File 'lib/wemote/switch.rb', line 80

def on!      
  set_state(1)   
end

#on?Boolean

Return whether the Switch is on



94
95
96
# File 'lib/wemote/switch.rb', line 94

def on?      
  get_state == :on
end

#poll(rate = 0.25, async = true, &block) ⇒ Thread

Monitors the state of the Switch via polling, and yields to the block given with the updated state.

Examples:

Output when a Switch changes state

light.poll do |state|
  if state == :on
    puts "The switch turned on"
  else
    puts "The switch turned off"
  end
end


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/wemote/switch.rb', line 114

def poll(rate=0.25,async=true,&block)
  old_state = get_state
  poller = Thread.start do
    loop do
      begin
        state = get_state
        if state != old_state
          old_state = state
          yield state
        end
      rescue Exception
      end
      sleep rate
    end
  end
  puts "Monitoring #{@name} for changes"
  async ? poller : poller.join
end

#toggle!Object

Turn the Switch on or off, based on its current state



70
71
72
# File 'lib/wemote/switch.rb', line 70

def toggle!
    on? ? off! : on!
end