kasa 0.1.2 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8358679b94124f72b7015263cf0d9343b24e03012cfce371c8ee910e4d10c0ca
4
- data.tar.gz: 7fdcee5d14a85f0bf16b3fca9ca6f9da17cb033580952257dd9f84a077b748f5
3
+ metadata.gz: 25195b0def3dbeb1694fbc6ffead20ad21aa32e4d95721f5fdc698549474c96b
4
+ data.tar.gz: 326a7ccf19efefb2f1edf6dbe25ea644e5062bb600d18424020b90386900444e
5
5
  SHA512:
6
- metadata.gz: b413201f7e11995a88ebb96f724addaf0d1e26d6607295ff8ad0e1bfa43eba8a334ad27efe5d6ce4f82e28d20dcb4df4733a0b473ad1806b529fe5b4c1e11d56
7
- data.tar.gz: ccc90f4a8417797d03fce52c60a5c1f48e8a3bfd3f14c4eb08d78e061d1e15ed5c334ca90d0587171a9c5d10ab76fc5b1da5007d5d2777898ef815a6a3e37b13
6
+ metadata.gz: 7a44f2a6b231f50f3ca98ea0d484528cf445ba6d608b0f5211e72ab414e7ed9c9ae421834a4256fd290bbf4572dd52f9c39dbdf5f996126468d1c888614f08bf
7
+ data.tar.gz: a1c2a2911abb04bfc4f6aa961e5e5d714c5b9963ea258c7d79ff18229e2705b46cfbf706dd11c14947aa4bb00f7d02eebd255b949c9fe492d097f7551b963767
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.0'
5
5
  end
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.0
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-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json