Created
January 16, 2012 00:19
-
-
Save JamesRyanATX/1618181 to your computer and use it in GitHub Desktop.
Basic Ruby client for 3M's Radio Thermostat
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'rubygems' | |
require 'httparty' | |
require 'json' | |
# Ruby client for 3M's Radio Thermostat (http://www.radiothermostat.com/) | |
# James Ryan <[email protected]> | |
# -- | |
# | |
# Examples: | |
# | |
# > require 'radio_thermostat' | |
# > rt = RadioThermostat.new('192.168.1.14') | |
# | |
# Get current tempurature: | |
# > rt.current_temperature | |
# 64.5 | |
# | |
# Set mode to "heat" and assign new target tempurature | |
# > rt.heat_target_tempurature = 65 | |
# | |
# Set mode to "cool" and assign new target tempurature | |
# > rt.cool_target_tempurature = 73 | |
# | |
# Turn on fan | |
# > rt.turn_on_fan | |
# | |
# Turn off fan | |
# > rt.turn_off_fan | |
# | |
# Turn off system entirely | |
# > rt.turn_off | |
class RadioThermostat | |
include HTTParty | |
TMODE_OFF = 0 | |
TMODE_HEAT = 1 | |
TMODE_COOL = 2 | |
FMODE_ON = 2 | |
FMODE_OFF = 1 | |
HOLD = 1 | |
format :json | |
def initialize(ip, opt = {}) | |
self.class.base_uri ip | |
@opt = { :units => :f, :headers => { 'User-Agent' => 'foo' } }.merge opt | |
end | |
# Turn off system | |
def turn_off | |
self.class.post '/tstat', :body => package({ 'tmode' => TMODE_OFF, 'hold' => HOLD}), :headers => @opt[:headers] | |
end | |
# Return the current temperature | |
def current_temperature | |
status['temp'].to_f | |
end | |
# Turn on the fan | |
def turn_on_fan | |
self.class.post '/tstat', :body => package({ 'fmode' => FMODE_ON }), :headers => @opt[:headers] | |
end | |
# Turn off the fan | |
def turn_off_fan | |
self.class.post '/tstat', :body => package({ 'fmode' => FMODE_OFF }), :headers => @opt[:headers] | |
end | |
# Get raw status data | |
def status | |
self.class.get '/tstat', :headers => @opt[:headers] | |
end | |
# Get model | |
def model | |
self.class.get '/tstat/model' | |
end | |
# Set cool target tempurature | |
def cool_target_tempurature=(target) | |
self.class.post '/tstat', :body => package({ 'tmode' => TMODE_COOL, 't_cool' => units(target), 'hold' => HOLD}), :headers => @opt[:headers] | |
end | |
# Set heat target tempurature | |
def heat_target_tempurature=(target) | |
self.class.post '/tstat', :body => package({ 'tmode' => TMODE_HEAT, 't_heat' => units(target), 'hold' => HOLD}), :headers => @opt[:headers] | |
end | |
private | |
# Convert to F if needed | |
def units(value) | |
@opt[:units] == :f ? value : value * 9 / 5 + 32 | |
end | |
# Prepare hash for API call (convert it to JSON, placeholder for addl' translation) | |
def package(value) | |
value.to_json | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment