Class: OJElectronics::Client
- Inherits:
-
Object
- Object
- OJElectronics::Client
- Defined in:
- lib/oj_electronics/client.rb
Defined Under Namespace
Classes: AuthenticationError, IncorrectPasswordError, InvalidUsernameError
Constant Summary collapse
- BRANDS =
{ oj_electronics: 0, schluter: 8 }.freeze
Instance Method Summary collapse
-
#api ⇒ Object
!@visibility private.
- #expired? ⇒ Boolean
-
#initialize(username, password, session_id: nil, expires: nil, brand: :oj_electronics) ⇒ Client
constructor
A new instance of Client.
-
#long_poll ⇒ Object
returns nil if nothing changed; otherwise the thermostat that changed.
-
#refresh ⇒ Object
refresh all thermostats.
Constructor Details
#initialize(username, password, session_id: nil, expires: nil, brand: :oj_electronics) ⇒ Client
Returns a new instance of Client.
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/oj_electronics/client.rb', line 40 def initialize(username, password, session_id: nil, expires: nil, brand: :oj_electronics) raise ArgumentError, "unrecognized brand #{brand.inspect}" unless BRANDS.key?(brand) @brand = brand @username = username @password = password @session_id = session_id @expires = expires @api = Faraday.new(url: "https://mythermostat.info/api/") do |f| f.request :json f.request :retry f.response :raise_error f.response :json f.adapter :net_http_persistent end @thermostats = {} refresh end |
Instance Method Details
#api ⇒ Object
!@visibility private
93 94 95 96 |
# File 'lib/oj_electronics/client.rb', line 93 def api reauth @api end |
#expired? ⇒ Boolean
60 61 62 |
# File 'lib/oj_electronics/client.rb', line 60 def expired? @session_id.nil? || @expires.nil? || @expires < Time.now end |
#long_poll ⇒ Object
returns nil if nothing changed; otherwise the thermostat that changed
84 85 86 87 88 89 90 |
# File 'lib/oj_electronics/client.rb', line 84 def long_poll response = api.get("notification", sessionid: session_id).body json = response["Thermostat"] return if json.nil? @thermostats[json["SerialNumber"]].tap { |t| t.refresh(json) } end |
#refresh ⇒ Object
refresh all thermostats
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/oj_electronics/client.rb', line 65 def refresh thermostats = api.get("thermostats", sessionid: session_id) .body["Groups"] .flat_map { |g| g["Thermostats"] } .index_by { |t| t["SerialNumber"] } missing = @thermostats.keys - thermostats.keys missing.each { |sn| @thermostats.delete(sn) } additional = thermostats.keys - @thermostats.keys additional.each do |sn| @thermostats[sn] = Thermostat.new(self, sn) end thermostats.each do |sn, json| @thermostats[sn].refresh(json) end end |