kasa 0.1.2 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8358679b94124f72b7015263cf0d9343b24e03012cfce371c8ee910e4d10c0ca
4
- data.tar.gz: 7fdcee5d14a85f0bf16b3fca9ca6f9da17cb033580952257dd9f84a077b748f5
3
+ metadata.gz: b626279712effe805e53a91246d3ecc486de435a2ae2e1398cc99341b24e1bff
4
+ data.tar.gz: a6e06dd229b13f8567dbc446709eb437960706111eec76d42a1cb0312421cf50
5
5
  SHA512:
6
- metadata.gz: b413201f7e11995a88ebb96f724addaf0d1e26d6607295ff8ad0e1bfa43eba8a334ad27efe5d6ce4f82e28d20dcb4df4733a0b473ad1806b529fe5b4c1e11d56
7
- data.tar.gz: ccc90f4a8417797d03fce52c60a5c1f48e8a3bfd3f14c4eb08d78e061d1e15ed5c334ca90d0587171a9c5d10ab76fc5b1da5007d5d2777898ef815a6a3e37b13
6
+ metadata.gz: 9bf6c0701474c148d2b49609c2cdd20b7e9321dad106cf3d7b610d083c3045b0cec8f51a96bdcb8479a8f5afb6f70b3f890f0c7f1c1d1b23b8be51080a90a3a7
7
+ data.tar.gz: 6f60278666990dafe81745b3c534a771df4fcd3cbf81f22a7610ccf72eb962a9807210b55020b4a2399b4b74641babe8860c88b34b92e7fadd2a32c6bc8854f0
data/README.md CHANGED
@@ -28,7 +28,9 @@ kasa.devices.first.on
28
28
 
29
29
  Local network device is detected but can be optionally overridden:
30
30
  ```
31
- Kasa.new(discover_interface_override: 'eth1')
31
+ require 'kasa'
32
+
33
+ kasa = Kasa.new(discover_interface_override: 'eth1')
32
34
  ```
33
35
 
34
36
  To connect to known host and adjust brightness:
@@ -39,6 +41,14 @@ device = Kasa::Factory.new('192.168.1.55')
39
41
  device.brightness=50
40
42
  ```
41
43
 
44
+ To get device summary:
45
+ ```
46
+ require 'kasa'
47
+
48
+ kasa = Kasa.new
49
+ kasa.summary
50
+ ```
51
+
42
52
  ## Development
43
53
 
44
54
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/lib/kasa/devices.rb CHANGED
@@ -6,12 +6,15 @@ class Kasa
6
6
  ON = 1
7
7
  OFF = 0
8
8
 
9
- attr_reader :ip
9
+ attr_reader :ip, :alias, :model, :dev_name, :mac
10
10
 
11
11
  # initialize
12
- def initialize(ip)
12
+ def initialize(ip, args)
13
13
  @ip = ip
14
- @sysinfo = sysinfo
14
+ @alias = args['alias']
15
+ @model = args['model']
16
+ @dev_name = args['dev_name']
17
+ @mac = args['mac']
15
18
  end
16
19
 
17
20
  # Get system information
@@ -70,8 +73,8 @@ class Kasa
70
73
  def brightness=(level)
71
74
  Kasa::Protocol.get(
72
75
  @ip,
73
- location: '/smartlife.iot.dimmer/set_brightness/brightness',
74
- value: level
76
+ location: '/smartlife.iot.dimmer/set_brightness',
77
+ value: { brightness: level }
75
78
  )
76
79
  end
77
80
  end
@@ -80,9 +83,9 @@ class Kasa
80
83
  class SmartStrip < NonDimmable
81
84
  attr_accessor :children
82
85
 
83
- def initialize(ip)
86
+ def initialize(ip, args)
84
87
  super
85
- @children = @sysinfo['children'].map { |c| c['id'] }
88
+ @children = args['children'].map { |c| c['id'] }
86
89
  end
87
90
 
88
91
  # Turn on light
data/lib/kasa/factory.rb CHANGED
@@ -11,6 +11,7 @@ class Kasa
11
11
  NonDimmable => [
12
12
  'HS200(US)',
13
13
  'HS105(US)',
14
+ 'HS103(US)',
14
15
  'HS210(US)'
15
16
  ],
16
17
  SmartStrip => [
@@ -24,13 +25,14 @@ class Kasa
24
25
  class Factory
25
26
  # Factory
26
27
  def self.new(ip)
27
- model = Kasa::Protocol.get(ip, location: '/system/get_sysinfo')['model']
28
+ sysinfo = Kasa::Protocol.get(ip, location: '/system/get_sysinfo')
29
+ model = sysinfo['model']
28
30
  begin
29
31
  object = DEVICE_TYPES.detect { |_k, v| v.include? model }.first.allocate
30
32
  rescue StandardError => _e
31
33
  raise "#{model} not supported"
32
34
  end
33
- object.send :initialize, ip
35
+ object.send :initialize, ip, sysinfo
34
36
  object
35
37
  end
36
38
  end
data/lib/kasa/protocol.rb CHANGED
@@ -19,13 +19,21 @@ class Kasa
19
19
  transport(ip, encode(request.to_json))
20
20
  end
21
21
 
22
- strip_location(location, decode(encoded_response))
22
+ response = JSON.parse decode(encoded_response)
23
+ response = strip_location(location, response)
24
+ validate response
25
+
26
+ response
27
+ end
28
+
29
+ # examine error code
30
+ def self.validate(response)
31
+ raise response.to_json if response.is_a?(Hash) && response['err_code']&.negative?
23
32
  end
24
33
 
25
34
  # strip away the request location from the response
26
35
  def self.strip_location(location, response)
27
36
  location = location.split('/').reject(&:empty?)
28
- response = JSON.parse response
29
37
  location.each do |j|
30
38
  response = (response[j] or response)
31
39
  end
data/lib/kasa/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Kasa
4
- VERSION = '0.1.2'
4
+ VERSION = '0.2.2'
5
5
  end
data/lib/kasa.rb CHANGED
@@ -2,7 +2,9 @@
2
2
 
3
3
  require 'timeout'
4
4
  require 'base64'
5
+ require 'ipaddr'
5
6
  require 'logger'
7
+ require 'terminal-table'
6
8
  require_relative 'kasa/version'
7
9
  require_relative 'kasa/factory'
8
10
 
@@ -33,6 +35,20 @@ class Kasa
33
35
  @devices
34
36
  end
35
37
 
38
+ # human readable devices summary
39
+ def summary
40
+ all_headings = @devices.first.instance_variables
41
+ common_headings = @devices.each_with_object(all_headings) do |d, memo|
42
+ memo & d.instance_variables
43
+ end
44
+ rows = devices.map do |d|
45
+ common_headings.map do |a|
46
+ d.instance_variable_get a
47
+ end
48
+ end
49
+ puts Terminal::Table.new headings: common_headings, rows: rows
50
+ end
51
+
36
52
  private
37
53
 
38
54
  # Return CIDR of first IPv4 interface or user-specified interface
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kasa
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Christopher Jenkins
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-02-19 00:00:00.000000000 Z
11
+ date: 2022-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json
@@ -66,6 +66,20 @@ dependencies:
66
66
  - - "~>"
67
67
  - !ruby/object:Gem::Version
68
68
  version: '1.4'
69
+ - !ruby/object:Gem::Dependency
70
+ name: terminal-table
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
69
83
  - !ruby/object:Gem::Dependency
70
84
  name: rake
71
85
  requirement: !ruby/object:Gem::Requirement