Class: Kasa

Inherits:
Object
  • Object
show all
Defined in:
lib/kasa.rb,
lib/kasa/devices.rb,
lib/kasa/factory.rb,
lib/kasa/version.rb,
lib/kasa/protocol.rb

Overview

Control local Kasa devices

Defined Under Namespace

Classes: Device, Dimmable, Factory, NonDimmable, Protocol, SmartStrip

Constant Summary collapse

DEVICE_TYPES =
{
  Dimmable => [
    'HS220(US)'
  ],
  NonDimmable => [
    'HS200(US)',
    'HS105(US)',
    'HS103(US)',
    'HS210(US)'
  ],
  SmartStrip => [
    'HS300(US)',
    'KP303(US)',
    'KP400(US)'
  ]
}.freeze
VERSION =
'0.2.2'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(discover: true, discover_interface_override: false, debug: false) ⇒ Kasa

initialize



16
17
18
19
20
21
22
# File 'lib/kasa.rb', line 16

def initialize(discover: true, discover_interface_override: false, debug: false)
  @logger = Logger.new($stdout)
  @logger.level = debug ? Logger::DEBUG : Logger::INFO
  @devices = []
  @discover_interface_override = discover_interface_override
  refresh if discover
end

Instance Attribute Details

#devicesObject (readonly)

Returns the value of attribute devices.



13
14
15
# File 'lib/kasa.rb', line 13

def devices
  @devices
end

Instance Method Details

#refreshObject

Populate devices hash with Kasa devices



25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/kasa.rb', line 25

def refresh
  threads = []
  ip_range.each do |ip|
    threads << Thread.new do
      @devices << Kasa::Factory.new(ip)
    rescue StandardError => e
      @logger.debug e
    end
  end
  threads.each(&:join)
  @devices
end

#summaryObject

human readable devices summary



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/kasa.rb', line 39

def summary
  all_headings = @devices.first.instance_variables
  common_headings = @devices.each_with_object(all_headings) do |d, memo|
    memo & d.instance_variables
  end
  rows = devices.map do |d|
    common_headings.map do |a|
      d.instance_variable_get a
    end
  end
  puts Terminal::Table.new headings: common_headings, rows: rows
end