Class: TPLink::SmartHome

Inherits:
Object
  • Object
show all
Defined in:
lib/tp_link/smart_home.rb

Overview

Main class for TPLink. This is likely the only class you will initialize.

Examples:

sh = TPLink::SmartHome.new

# Get array of TPLink Devices (currently only dimmable lights work).
sh.devices

# Find a device by name:
light = sh.find("kitchen")

# Turn light on
light.on

# Turn light off
light.off

# Dim light to 50%
light.on(50)

Instance Method Summary collapse

Constructor Details

#initialize(config = {}) ⇒ SmartHome

Returns a new instance of SmartHome.

Examples:

smarthome = TPLink::SmartHome.new(user: kasa@example.com, password: password: 1234)
kitchen_light = smarthome.find("kitchen")
kitchen_light.on
kitchen_light.off

Parameters:

  • config (Hash, String) (defaults to: {})

    options for TPLink

Options Hash (config):

  • :user (String)

    Your Kasa user name

  • :password (String)

    Your Kasa password

  • :uuid (String)

    Your “device” (program) uuid.


34
35
36
37
# File 'lib/tp_link/smart_home.rb', line 34

def initialize(config = {})
  @api = TPLink::API.new(config)
  reload
end

Instance Method Details

#devicesArray<TPLink::Light,TPLink::RGBLight,TPLlink::Plug>

Find a device by it’s alias.

Examples:

Turn everything off

smarthome.devices.each { |device| device.off }

Returns:


55
56
57
# File 'lib/tp_link/smart_home.rb', line 55

def devices
  @devices ||= raw_devices.map { |d| dev_to_class(d) }.compact
end

#find(a) ⇒ TPLink::Light, ...

Find a device by it’s alias. Search is case insensitive.

Examples:

Find your kitchen light

smarthome.find("kitchen")

Parameters:

  • a (String)

    device alias.

Returns:


47
48
49
# File 'lib/tp_link/smart_home.rb', line 47

def find(a)
  devices.find { |d| d.alias.match(/^#{a}$/i) }
end

#raw_devicesObject


63
64
65
66
# File 'lib/tp_link/smart_home.rb', line 63

def raw_devices
  return @raw_devices if @raw_devices
  @raw_devices = @api.device_list
end

#reloadObject

Reload devices from TPLink api.


69
70
71
72
# File 'lib/tp_link/smart_home.rb', line 69

def reload
  @raw_devices = nil
  @devices = nil
end

#send_data(device, data) ⇒ Object


59
60
61
# File 'lib/tp_link/smart_home.rb', line 59

def send_data(device, data)
  @api.send_data(device, data)
end