sensibo-api 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 9d4bb676a37a1c8f87ba804b4e9be133db0d992a
4
+ data.tar.gz: 6758c2639a9448b16651547484f6420911dec82c
5
+ SHA512:
6
+ metadata.gz: 7bd3b5ec0a5148617ec17c18569015ec7edfd0ecf3b2416df9477484fa4df5858155227b8d2f9248a0f88912fbb05444d53452c91b6f352513958b9df4bf0b48
7
+ data.tar.gz: 38300218945b1e62c75823616d8b0cde20d87e49e040dcb90f704ad74ab89058d8dd08dc8c128d640b65678e2a2cd1925e06e564713add5cefd44838c6af7e47
@@ -0,0 +1,2 @@
1
+ .env
2
+ *.gem
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ nest-api (0.0.1)
5
+ awesome_print
6
+ cistern (~> 2.8.2)
7
+ excon (~> 0.45.5)
8
+ faraday (~> 0.15.4)
9
+ faraday_middleware
10
+ oj
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ awesome_print (1.8.0)
16
+ byebug (11.0.1)
17
+ cistern (2.8.2)
18
+ coderay (1.1.2)
19
+ diff-lcs (1.3)
20
+ dotenv (2.7.1)
21
+ excon (0.45.5)
22
+ faraday (0.15.4)
23
+ multipart-post (>= 1.2, < 3)
24
+ faraday_middleware (0.13.1)
25
+ faraday (>= 0.7.4, < 1.0)
26
+ method_source (0.9.2)
27
+ multipart-post (2.0.0)
28
+ oj (3.6.2)
29
+ pry (0.12.2)
30
+ coderay (~> 1.1.0)
31
+ method_source (~> 0.9.0)
32
+ pry-byebug (3.7.0)
33
+ byebug (~> 11.0)
34
+ pry (~> 0.10)
35
+ rspec (3.8.0)
36
+ rspec-core (~> 3.8.0)
37
+ rspec-expectations (~> 3.8.0)
38
+ rspec-mocks (~> 3.8.0)
39
+ rspec-core (3.8.0)
40
+ rspec-support (~> 3.8.0)
41
+ rspec-expectations (3.8.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.8.0)
44
+ rspec-mocks (3.8.0)
45
+ diff-lcs (>= 1.2.0, < 2.0)
46
+ rspec-support (~> 3.8.0)
47
+ rspec-support (3.8.0)
48
+
49
+ PLATFORMS
50
+ ruby
51
+
52
+ DEPENDENCIES
53
+ byebug
54
+ dotenv
55
+ nest-api!
56
+ pry-byebug
57
+ rspec
58
+
59
+ BUNDLED WITH
60
+ 2.0.1
@@ -0,0 +1,23 @@
1
+ # Sensibo API Client
2
+
3
+ Rewrite of the sensibo API client using Cistern.
4
+
5
+ ## Usage
6
+
7
+ ### Installation
8
+ `gem install sensibo-api`
9
+
10
+ `require 'sensibo'`
11
+
12
+ ###
13
+ 1. Initialize the client
14
+
15
+ `client = Sensibo.new(api_key: API_KEY)`
16
+
17
+ 2. Get a device
18
+
19
+ `device = client.devices.first`
20
+
21
+ 3. Set a param
22
+
23
+ `device.update(fan_level: "strong")`
@@ -0,0 +1,22 @@
1
+ require "awesome_print"
2
+ require "cistern"
3
+ require "excon"
4
+ require "faraday"
5
+ require "faraday_middleware"
6
+ require "oj"
7
+
8
+ require "dotenv/load" if File.exists?(".env")
9
+
10
+ class Sensibo
11
+ include Cistern::Client.with(interface: :module)
12
+
13
+ recognizes :url, :api_key
14
+
15
+ require_relative "sensibo/real"
16
+
17
+ require_relative "sensibo/models/device"
18
+ require_relative "sensibo/models/devices"
19
+
20
+ require_relative "sensibo/requests/get_devices"
21
+ require_relative "sensibo/requests/set_device_state"
22
+ end
@@ -0,0 +1,55 @@
1
+ class Sensibo::Device
2
+ include Sensibo::Model
3
+
4
+ identity :id
5
+
6
+ attribute :battery_voltage, type: :float, squash: ["measurements", "batteryVoltage"]
7
+ attribute :config_group, aliases: :configGroup
8
+ attribute :fan_level, squash: ["acState", "fanLevel"]
9
+ attribute :geofencing, aliases: :isGeofenceOnExitEnabled, type: :boolean
10
+ attribute :humidity, type: :float, squash: ["measurements", "humidity"]
11
+ attribute :mac_address, aliases: :macAddress
12
+ attribute :mode, squash: ["acState", "mode"]
13
+ attribute :model, aliases: :productModel
14
+ attribute :native_temperature_unit, squash: ["acState", "nativeTemperatureUnit"]
15
+ attribute :power_on, type: :boolean, squash: ["acState", "on"]
16
+ attribute :swing, squash: ["acState", "swing"]
17
+ attribute :target_temperature, type: :float, squash: ["acState", "targetTemperature"]
18
+ attribute :temperature, type: :float, squash: ["measurements", "temperature"]
19
+ attribute :temperature_unit, squash: ["acState", "temperatureUnit"]
20
+
21
+ def save
22
+ requires :identity
23
+
24
+ temp = if self.temperature_unit == self.native_temperature_unit
25
+ self.target_temperature
26
+ elsif self.temperature_unit == "F"
27
+ c_to_f(f_to_c(self.target_temperature))
28
+ elsif self.temperature_unit == "C"
29
+ f_to_c(c_to_f(self.target_temperature))
30
+ end
31
+
32
+ body = {
33
+ "acState" => {
34
+ "fanLevel" => self.fan_level,
35
+ "mode" => self.mode,
36
+ "on" => self.power_on,
37
+ "swing" => self.swing,
38
+ "targetTemperature" => temp,
39
+ "temperatureUnit" => self.temperature_unit,
40
+ }
41
+ }
42
+
43
+ body = cistern.set_device_state(self.id, body)["result"]
44
+
45
+ raise RuntimeError.new("Save Failed") unless body["status"] == "Success"
46
+ end
47
+
48
+ def f_to_c(temp)
49
+ ((temp - 32) * (5.0/9.0)).round
50
+ end
51
+
52
+ def c_to_f(temp)
53
+ ((temp * (9.0/5.0)) + 32).round
54
+ end
55
+ end
@@ -0,0 +1,10 @@
1
+ class Sensibo::Devices
2
+ include Sensibo::Collection
3
+
4
+ model Sensibo::Device
5
+
6
+ def all
7
+ data = cistern.get_devices["result"]
8
+ load(data)
9
+ end
10
+ end
@@ -0,0 +1,21 @@
1
+ class Sensibo::Real
2
+ attr_reader :url, :connection, :api_key
3
+
4
+ def initialize(attributes={})
5
+ @url = attributes[:url] || "https://home.sensibo.com"
6
+ @api_key = attributes[:api_key] || ENV["API_KEY"]
7
+ @connection = Faraday.new(url: @url, headers: { "Content-Type" => "application/json" }) do |b|
8
+ b.adapter :excon
9
+ end
10
+ end
11
+
12
+ def request(method, path, args={})
13
+ res = connection.send(method) do |req|
14
+ req.url "/api/v2#{path}"
15
+ req.params = { apiKey: self.api_key, fields: "*" }
16
+ req.body = Oj.dump(args[:body]) if args[:body]
17
+ end
18
+
19
+ Oj.load(res.body)
20
+ end
21
+ end
@@ -0,0 +1,7 @@
1
+ class Sensibo::GetDevices
2
+ include Sensibo::Request
3
+
4
+ def real
5
+ cistern.request(:get, "/users/me/pods")
6
+ end
7
+ end
@@ -0,0 +1,7 @@
1
+ class Sensibo::SetDeviceState
2
+ include Sensibo::Request
3
+
4
+ def real(id, body)
5
+ cistern.request(:post, "/pods/#{id}/acStates", body: body)
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ class Sensibo
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,28 @@
1
+ require_relative "./lib/sensibo/version"
2
+
3
+ Gem::Specification.new do |gem|
4
+ gem.authors = ["Eugene Howe"]
5
+ gem.email = ["eugene@xtreme-computers.net"]
6
+ gem.description = %q{}
7
+ gem.summary = %q{}
8
+ gem.homepage = "https://github.com/ehowe/sensibo-api.git"
9
+
10
+ gem.files = `git ls-files`.split($\)
11
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
12
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
13
+ gem.name = "sensibo-api"
14
+ gem.require_paths = ["lib"]
15
+ gem.version = Sensibo::VERSION
16
+
17
+ gem.add_dependency "awesome_print", "~> 1.8"
18
+ gem.add_dependency "cistern", "~> 2.8"
19
+ gem.add_dependency "excon", "~> 0.45"
20
+ gem.add_dependency "faraday", "~> 0.15"
21
+ gem.add_dependency "faraday_middleware", "~> 0.13"
22
+ gem.add_dependency "oj", "~> 3.6"
23
+
24
+ gem.add_development_dependency "byebug", "~> 11.0"
25
+ gem.add_development_dependency "dotenv", "~> 2.7"
26
+ gem.add_development_dependency "pry-byebug", "~> 3.7"
27
+ gem.add_development_dependency "rspec", "~> 3.8"
28
+ end
@@ -0,0 +1,21 @@
1
+ require "test_helper"
2
+
3
+ describe "Devices" do
4
+ let(:client) { Sensibo.new }
5
+
6
+ it "has a collection of devices" do
7
+ expect(client.devices.all).to be_a(Sensibo::Devices)
8
+ end
9
+
10
+ context "with a device" do
11
+ let!(:device) { client.devices.first }
12
+
13
+ it "changes the device fan speed" do
14
+ original = device.fan_level
15
+
16
+ expect { device.update(fan_level: "strong") }.to change { device.fan_level }.from(original).to("strong")
17
+
18
+ device.update(fan_level: original)
19
+ end
20
+ end
21
+ end
@@ -0,0 +1 @@
1
+ require_relative "../lib/sensibo"
metadata ADDED
@@ -0,0 +1,199 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sensibo-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Eugene Howe
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-03-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: awesome_print
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.8'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.8'
27
+ - !ruby/object:Gem::Dependency
28
+ name: cistern
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '2.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '2.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: excon
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.45'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.45'
55
+ - !ruby/object:Gem::Dependency
56
+ name: faraday
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '0.15'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '0.15'
69
+ - !ruby/object:Gem::Dependency
70
+ name: faraday_middleware
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '0.13'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '0.13'
83
+ - !ruby/object:Gem::Dependency
84
+ name: oj
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.6'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.6'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '11.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '11.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: dotenv
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '2.7'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '2.7'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-byebug
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.7'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.7'
139
+ - !ruby/object:Gem::Dependency
140
+ name: rspec
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '3.8'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '3.8'
153
+ description: ''
154
+ email:
155
+ - eugene@xtreme-computers.net
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - Gemfile
162
+ - Gemfile.lock
163
+ - README.md
164
+ - lib/sensibo.rb
165
+ - lib/sensibo/models/device.rb
166
+ - lib/sensibo/models/devices.rb
167
+ - lib/sensibo/real.rb
168
+ - lib/sensibo/requests/get_devices.rb
169
+ - lib/sensibo/requests/set_device_state.rb
170
+ - lib/sensibo/version.rb
171
+ - sensibo-api.gemspec
172
+ - spec/devices_spec.rb
173
+ - spec/test_helper.rb
174
+ homepage: https://github.com/ehowe/sensibo-api.git
175
+ licenses: []
176
+ metadata: {}
177
+ post_install_message:
178
+ rdoc_options: []
179
+ require_paths:
180
+ - lib
181
+ required_ruby_version: !ruby/object:Gem::Requirement
182
+ requirements:
183
+ - - ">="
184
+ - !ruby/object:Gem::Version
185
+ version: '0'
186
+ required_rubygems_version: !ruby/object:Gem::Requirement
187
+ requirements:
188
+ - - ">="
189
+ - !ruby/object:Gem::Version
190
+ version: '0'
191
+ requirements: []
192
+ rubyforge_project:
193
+ rubygems_version: 2.6.14
194
+ signing_key:
195
+ specification_version: 4
196
+ summary: ''
197
+ test_files:
198
+ - spec/devices_spec.rb
199
+ - spec/test_helper.rb