class Wemote::Switch

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.

Constants

GET_HEADERS
SET_HEADERS

Attributes

name[RW]

Public Class Methods

all(refresh=false) click to toggle source

Returns all Switches detected on the local network

@param [Boolean] refresh Refresh and redetect Switches @return [Array] all Switches on the network

# File lib/wemote/switch.rb, line 34
def all(refresh=false)
  @switches = nil if refresh
  @switches ||= Wemote::Collection::Switch.new(discover)
end
device_type() click to toggle source
# File lib/wemote/switch.rb, line 26
def device_type
  'urn:Belkin:device:controllee:1'
end
find(name) click to toggle source

Returns a Switch of a given name

@param name [String] the friendly name of the Switch @return [Wemote::Switch] a Switch object

# File lib/wemote/switch.rb, line 43
def find(name)
  all.detect{|s|s.name == name}
end
new(host,port=nil) click to toggle source
# File lib/wemote/switch.rb, line 60
def initialize(host,port=nil)
  @host, @port = host, port
  set_meta
end

Protected Class Methods

discover() click to toggle source
# File lib/wemote/switch.rb, line 49
def discover
  finder = SSDP::Consumer.new timeout: 3, first_only: false
  finder.search(service: self.device_type).map do |device|
    self.new(device[:address], device[:params]['LOCATION'].match(/:([0-9]{1,5})\//)[1])
  end
end

Public Instance Methods

device_type() click to toggle source
# File lib/wemote/switch.rb, line 65
def device_type 
  'urn:Belkin:device:controllee:1'
end
off!() click to toggle source

Turn the Switch off

# File lib/wemote/switch.rb, line 75
def off!     
  set_state(0)       
end
off?() click to toggle source

Return whether the Switch is off

@return [Boolean]

# File lib/wemote/switch.rb, line 87
def off?     
  get_state == :off
end
on!() click to toggle source

Turn the Switch on

# File lib/wemote/switch.rb, line 80
def on!      
  set_state(1)   
end
on?() click to toggle source

Return whether the Switch is on

@return [Boolean]

# File lib/wemote/switch.rb, line 94
def on?      
  get_state == :on
end
poll(rate=0.25,async=true) { |state| ... } click to toggle source

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

@example 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

@param rate [Float] The rate in seconds at which to poll the switch @param async [Boolean] Whether or not to poll the switch in a separate thread

@return [Thread] if the method call was asynchronous

# 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!() click to toggle source

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

# File lib/wemote/switch.rb, line 70
def toggle!
    on? ? off! : on!
end

Protected Instance Methods

client() click to toggle source
# File lib/wemote/switch.rb, line 156
def client
  @client ||= Wemote::Client.new
end
get_binary_state() click to toggle source
# File lib/wemote/switch.rb, line 139
def get_binary_state
  response = begin
    client.post("http://#{@host}:#{@port}/upnp/control/basicevent1",Wemote::XML.get_binary_state,GET_HEADERS)
  rescue Exception
    client.post("http://#{@host}:#{@port}/upnp/control/basicevent1",Wemote::XML.get_binary_state,GET_HEADERS)
  end
  response.body.match(/<BinaryState>(\d)<\/BinaryState>/)[1]
end
get_state() click to toggle source
# File lib/wemote/switch.rb, line 135
def get_state
  self.get_binary_state() == '1' ? :on : :off
end
set_meta() click to toggle source
# File lib/wemote/switch.rb, line 160
def set_meta
  response = client.get("http://#{@host}:#{@port}/setup.xml")
  @name = response.body.match(/<friendlyName>([^<]+)<\/friendlyName>/)[1]
end
set_state(state) click to toggle source
# File lib/wemote/switch.rb, line 148
def set_state(state)
  begin
    client.post("http://#{@host}:#{@port}/upnp/control/basicevent1",Wemote::XML.set_binary_state(state),SET_HEADERS)
  rescue Exception
    client.post("http://#{@host}:#{@port}/upnp/control/basicevent1",Wemote::XML.set_binary_state(state),SET_HEADERS)
  end
end