renstar 0.4.3 → 0.4.5

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: 617260529f9b3d241c8b6f0a4489d9eac7f871a998b9f2e0b42c0ee54e322491
4
- data.tar.gz: 3a7560d9559b8c40272a991449bf6d06916fccd4dbc7914e98a5c58d0c84bdd9
3
+ metadata.gz: 0af30640855c7d5568a2a8fbd16ac0f554f5bdf20415c5085974354120aedfd0
4
+ data.tar.gz: 04ddf28ef495cb77a1bfebe74d2ae508c9bac487f4eeb86bceddab6cbd2dd228
5
5
  SHA512:
6
- metadata.gz: 83fd7c65c7162f8c6c9582a6c8525aa49f14c4c578b56e866581fff21fcfa554afe387396ef399d800993787ec378a1066f3da5cb39ee086a5675e9cc188c119
7
- data.tar.gz: 1bcf749dad62edddb0d6be3b4c8e081eb08bdef1ad4389bc86b372ad7fb6e3bef6d3369764f8c157d19c6b76f1de21b10bd1444c0098edd2e211f30cfb4b9b7b
6
+ metadata.gz: 2b8c6e5eb3c7e8e31481d9689c29f01e26f945f89a95342460d2f0864b74c51f9b2003116af4644c5e7f796213db8d016db4706b513b630c70f4a005b41c84ec
7
+ data.tar.gz: 6afab4dc745a6a18da67f1ecfff3e5047aed24930c23a7d5208864947a780f758ee0fab0f733e5824262b62ae0e198e4663b0b200b835df580ed6ccfcc39e5c2
data/README.md CHANGED
@@ -4,6 +4,8 @@ Renstar is a Ruby SDK for interacting with the Venstar Thermostat Local API.
4
4
 
5
5
  You can control your thermostat programmatically!
6
6
 
7
+ [![Maintainability](https://api.codeclimate.com/v1/badges/bf690d0fd84e34b70080/maintainability)](https://codeclimate.com/github/mikerodrigues/renstar/maintainability)
8
+
7
9
  ## Installation
8
10
 
9
11
  Add this line to your application's Gemfile:
data/bin/renstar.rb CHANGED
@@ -83,7 +83,13 @@ command = if ARGV.nil? || ARGV.empty?
83
83
  ARGV
84
84
  end
85
85
 
86
- response = thermo.send(*command)
86
+ begin
87
+ response = thermo.send(*command)
88
+ rescue NoMethodError
89
+ puts "'#{command.join(' ')}' is not a valid command"
90
+ exit 1
91
+ end
92
+
87
93
  case response
88
94
  when String
89
95
  puts response
@@ -13,16 +13,26 @@ module Renstar
13
13
  # The actual client that handles getting, posting, and parsing API responses
14
14
  #
15
15
  module APIClient
16
+ class APIError < StandardError; end
17
+ class APIUnknownResponseError < StandardError; end
18
+
16
19
  def get(endpoint)
17
20
  uri = URI(location + endpoint)
18
- response = Net::HTTP.get(uri)
19
- JSON.parse(response)
21
+ response = Net::HTTP.get_response(uri)
22
+ JSON.parse(response.body)
20
23
  end
21
24
 
22
25
  def post(endpoint, options = {})
23
26
  uri = URI(location + endpoint)
24
27
  response = Net::HTTP.post_form(uri, options)
25
- JSON.parse(response.body)
28
+ json = JSON.parse(response.body)
29
+ if json['error']
30
+ raise APIError, json['reason']
31
+ elsif json['success']
32
+ json
33
+ else
34
+ raise APIUnknownResponseError, response.body
35
+ end
26
36
  end
27
37
 
28
38
  include Query
@@ -8,17 +8,8 @@ module Renstar
8
8
  # the network
9
9
  module Discovery
10
10
  # Discovery timeout when searching for thermostats
11
- DEFAULT_TIMEOUT = 3
12
11
  SERVICE = 'venstar:thermostat:ecp'
13
12
 
14
- def search(timeout = DEFAULT_TIMEOUT)
15
- all_thermos = []
16
- ips.each do |ip|
17
- all_thermos << ssdp_search(ip, timeout)
18
- end
19
- all_thermos.flatten
20
- end
21
-
22
13
  private
23
14
 
24
15
  def ips
@@ -28,15 +19,10 @@ module Renstar
28
19
  end
29
20
 
30
21
  # do an SSDP search using the given IP from a found local interface
31
- def ssdp_search(ip, timeout = DEFAULT_TIMEOUT)
22
+ def ssdp_search(ip, timeout = 5)
32
23
  puts "Searching subnet associated with #{ip.ip_address}"
33
24
  ssdp = SSDP::Consumer.new({ bind: ip.ip_address })
34
- thermos = ssdp.search(service: SERVICE, timeout: timeout)
35
- thermos.map do |thermo|
36
- location = thermo[:params]['Location']
37
- usn = thermo[:params]['USN']
38
- Renstar::Thermostat.new(location, usn)
39
- end
25
+ ssdp.search(service: SERVICE, timeout: timeout)
40
26
  end
41
27
  end
42
28
  end
@@ -30,6 +30,16 @@ module Renstar
30
30
  @cached_info = info
31
31
  end
32
32
 
33
+ def self.search(timeout = 3)
34
+ all_thermos = ips.each do |ip|
35
+ all_thermos << ssdp_search(ip, timeout)
36
+ end
37
+
38
+ all_thermos.flatten.map do |thermo|
39
+ Renstar::Thermostat.new(thermo[:params]['Location'], thermo[:params]['USN'])
40
+ end
41
+ end
42
+
33
43
  def update
34
44
  @cache_timestamp = Time.now
35
45
  @cached_info = info
@@ -42,27 +52,14 @@ module Renstar
42
52
  end
43
53
 
44
54
  def heat(heattemp = nil)
45
- update
46
- if heattemp
47
- cooltemp = heattemp.to_i + 1
48
- else
49
- cooltemp = @cached_info.cooltemp
50
- heattemp = @cached_info.heattemp
51
- end
55
+ cooltemp, heattemp = set_temps(heattemp, 1)
52
56
  response = control("mode": 1, "cooltemp": cooltemp, "heattemp": heattemp)
53
57
  update
54
58
  response
55
59
  end
56
60
 
57
61
  def cool(cooltemp = nil)
58
- update
59
- if cooltemp
60
- heattemp = cooltemp.to_i - 1
61
- # heattemp = cooltemp - @cached_info.setpointdelta
62
- else
63
- cooltemp = @cached_info.cooltemp
64
- heattemp = @cached_info.heattemp
65
- end
62
+ cooltemp, heattemp = set_temps(cooltemp, 2)
66
63
  response = control("mode": 2, "cooltemp": cooltemp, "heattemp": heattemp)
67
64
  update
68
65
  response
@@ -122,5 +119,24 @@ module Renstar
122
119
  update
123
120
  response
124
121
  end
122
+
123
+ private
124
+
125
+ def set_temps(temp, mode)
126
+ update
127
+ if temp
128
+ if mode == 1
129
+ cooltemp = temp.to_i + 1
130
+ heattemp = temp.to_i
131
+ else
132
+ cooltemp = temp.to_i
133
+ heattemp = temp.to_i - 1
134
+ end
135
+ else
136
+ cooltemp = @cached_info.cooltemp
137
+ heattemp = @cached_info.heattemp
138
+ end
139
+ [cooltemp, heattemp]
140
+ end
125
141
  end
126
142
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Renstar
4
- VERSION = '0.4.3'
4
+ VERSION = '0.4.5'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: renstar
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.3
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Rodrigues
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-10-31 00:00:00.000000000 Z
11
+ date: 2024-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: json