domoticz 0.0.0 → 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d599c5d8d26450b7373eb1caf407e4b7b1d29b19
4
- data.tar.gz: 23ff38346cdb0ca2694e0ea979eb0d15ae16dae0
3
+ metadata.gz: ad30fb52aba106a7ece5773561ade61c12712a69
4
+ data.tar.gz: a2d60e4682cfbb0491d08364a2006a3edeb69623
5
5
  SHA512:
6
- metadata.gz: ed79decb12a6c75fdfdae4a42ddf24fac049ec352ff6c3fa5badf7dec04e47bab256abd3a2c2a8cd7fe0a38827ebde67e75538e8e71b9a8b25a4a9a0c30e5e52
7
- data.tar.gz: a25cdc96bee5df854896a0fc1799bcbb9fb5215738b42e35af4dcc0e42ef983f657c886a9d50544023aab92f2c7c3e1083bb33126d8d6ca50a05ffb49172f9b1
6
+ metadata.gz: eb56015813ff18e597c48580dd5d4b12aac23b2afcb6e820fabe3d65e399ebbd2df1937d5b6647b616cb5bfc8f36154bbc4140677f6198dca007d7c30c2bcbe5
7
+ data.tar.gz: a65cd8b0816fa0bdf5661e334848c2ff0a949b1dc56a97e53aaed92e741151b8cd560bf89b4d96a8e00d416f9ec2c6eef592f32dfa33d3e4967f081df486fe57
@@ -0,0 +1 @@
1
+ 2.1.5
data/README.md CHANGED
@@ -1,10 +1,37 @@
1
- # Domoticz
1
+ # Domoticz [![Circle CI](https://circleci.com/gh/jankeesvw/domoticz.svg?style=svg)](https://circleci.com/gh/jankeesvw/domoticz)
2
2
 
3
3
  Manage your Domoticz server from Ruby
4
4
 
5
- This gem is work in progress, you cannot yet use this in production.
5
+ This gem is work in progress!
6
6
 
7
- ## Contributing
7
+ ## Connect to your server
8
+
9
+ ```
10
+ Domoticz.configure do |config|
11
+ config.server = "http://127.0.0.1/"
12
+ config.username = "user"
13
+ config.password = "password"
14
+ end
15
+ ```
16
+
17
+ ## Devices
18
+
19
+ ### List all devices
20
+
21
+ ```ruby
22
+ devices = Domoticz::Switch.all
23
+ # => [#<Domoticz::Switch:0x007fc51e203420 @name="Energy", @dimmer=nil, @idx="6", @type="P1 Smart Meter", @subtype="Energy">, #<Domoticz::Switch:0x007fc51e203308 @name="Gas", @dimmer=nil, @idx="7", @type="P1 Smart Meter", @subtype="Gas">, #<Domoticz::Switch:0x007fc51e2031c8 @name="Test switch", @dimmer=nil, @idx="8", @type="Lighting 1", @subtype="X10">]
24
+ ```
25
+
26
+ ### Interact with a switch
27
+ ```ruby
28
+ switch = Domoticz::Devices.all.first
29
+ switch.on! # turn it on
30
+ switch.off! # turn it off
31
+ switch.toggle! # toggle switch
32
+ ```
33
+
34
+ # Contributing
8
35
 
9
36
  1. Fork it ( https://github.com/jankeesvw/domoticz/fork )
10
37
  2. Create your feature branch (`git checkout -b my-new-feature`)
@@ -20,4 +20,5 @@ Gem::Specification.new do |spec|
20
20
  spec.add_development_dependency "bundler", "~> 1.6"
21
21
  spec.add_development_dependency "rake"
22
22
  spec.add_development_dependency "rspec"
23
+ spec.add_development_dependency "timecop"
23
24
  end
@@ -1,7 +1,30 @@
1
1
  require "domoticz/version"
2
+ require "domoticz/configuration"
3
+ require "domoticz/device"
4
+ require "json"
2
5
 
3
6
  module Domoticz
4
- def self.hello
5
- "Hello Domoticz"
7
+ def self.configuration
8
+ @configuration ||= Configuration.new
9
+ end
10
+
11
+ def self.configure
12
+ yield(configuration) if block_given?
13
+ end
14
+
15
+ def self.reset
16
+ @configuration = Configuration.new
17
+ end
18
+
19
+ def self.perform_api_request(params)
20
+ username = Domoticz.configuration.username
21
+ password = Domoticz.configuration.password
22
+
23
+ uri = URI(Domoticz.configuration.server + "json.htm?" + params)
24
+ request = Net::HTTP::Get.new(uri)
25
+ request.basic_auth(username, password) if username && password
26
+ response = Net::HTTP.start(uri.hostname, uri.port) { |http| http.request(request) }
27
+
28
+ JSON.parse(response.body)
6
29
  end
7
30
  end
@@ -0,0 +1,11 @@
1
+ module Domoticz
2
+ class Configuration
3
+ attr_accessor :server
4
+ attr_accessor :username
5
+ attr_accessor :password
6
+
7
+ def initialize
8
+ self.server = "http://127.0.0.1:8080/"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,58 @@
1
+ module Domoticz
2
+ class Device
3
+ attr_accessor :idx
4
+ attr_accessor :data
5
+
6
+ def seconds_since_update
7
+ Time.now - Time.parse(lastupdate)
8
+ end
9
+
10
+ def on!
11
+ Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=On")
12
+ end
13
+
14
+ def off!
15
+ Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=Off")
16
+ end
17
+
18
+ def toggle!
19
+ Domoticz.perform_api_request("type=command&param=switchlight&idx=#{idx}&switchcmd=Toggle")
20
+ end
21
+
22
+ def temperature
23
+ temp
24
+ end
25
+
26
+ def dimmer?
27
+ isDimmer
28
+ end
29
+
30
+ def method_missing(method_sym, *arguments, &block)
31
+ hash = Hash[@data.map { |k, v| [k.downcase, v] }]
32
+ key = method_sym.to_s.downcase
33
+
34
+ if hash.has_key?(key)
35
+ hash[key]
36
+ else
37
+ super
38
+ end
39
+ end
40
+
41
+ def self.find_by_id(id)
42
+ all.find { |d| d.idx == id.to_s }
43
+ end
44
+
45
+ def self.all
46
+ Domoticz.perform_api_request("type=devices&filter=all&used=true")["result"].map do |json|
47
+ Device.new_from_json(json)
48
+ end
49
+ end
50
+
51
+ def self.new_from_json(json)
52
+ device = self.new
53
+ device.data = json
54
+ device.idx = json["idx"]
55
+ device
56
+ end
57
+ end
58
+ end
@@ -1,3 +1,3 @@
1
1
  module Domoticz
2
- VERSION = "0.0.0"
2
+ VERSION = "0.0.1"
3
3
  end
@@ -0,0 +1,107 @@
1
+ require "spec_helper"
2
+
3
+ describe Domoticz::Device do
4
+ it "gets all available devices" do
5
+ stub_server_with_fixture(params: "type=devices&filter=all&used=true", fixture: "switches.json")
6
+
7
+ devices = Domoticz::Device.all
8
+ expect(devices.count).to eq 2
9
+
10
+ expect(devices.first.name).to eq "Switch 1"
11
+ expect(devices.first.dimmer?).to be_truthy
12
+ expect(devices.first.idx).to eq "1"
13
+ expect(devices.first.type).to eq "Lighting 1"
14
+ expect(devices.first.subtype).to eq "X10"
15
+ end
16
+
17
+ it "gets a specific device" do
18
+ stub_server_with_fixture(params: "type=devices&filter=all&used=true", fixture: "switches.json")
19
+
20
+ device = Domoticz::Device.find_by_id(1)
21
+ expect(device).to be_a Domoticz::Device
22
+
23
+ expect(device.name).to eq "Switch 1"
24
+ expect(device.dimmer?).to be_truthy
25
+ expect(device.idx).to eq "1"
26
+ expect(device.type).to eq "Lighting 1"
27
+ expect(device.subtype).to eq "X10"
28
+ end
29
+
30
+ it "turns on a switch" do
31
+ stub_server_with_fixture(params: "type=command&param=switchlight&idx=8&switchcmd=On", fixture: "switch_turn_on.json", required: true)
32
+
33
+ switch = Domoticz::Device.new
34
+ switch.idx = "8"
35
+ switch.on!
36
+ end
37
+
38
+ it "turns off a switch" do
39
+ stub_server_with_fixture(params: "type=command&param=switchlight&idx=8&switchcmd=Off", fixture: "switch_turn_off.json", required: true)
40
+
41
+ switch = Domoticz::Device.new
42
+ switch.idx = "8"
43
+ switch.off!
44
+ end
45
+
46
+ it "toggles a switch" do
47
+ stub_server_with_fixture(params: "type=command&param=switchlight&idx=8&switchcmd=Toggle", fixture: "switch_toggle.json", required: true)
48
+
49
+ switch = Domoticz::Device.new
50
+ switch.idx = "8"
51
+ switch.toggle!
52
+ end
53
+
54
+ it "tells us how old this data point is" do
55
+ # "LastUpdate": "2015-12-13 14:02:47",
56
+ stub_server_with_fixture(params: "type=devices&filter=all&used=true", fixture: "temperature_device.json")
57
+
58
+ Timecop.freeze(2015, 12, 13, 14, 2, 51)
59
+
60
+ device = Domoticz::Device.find_by_id(47)
61
+ expect(device.seconds_since_update).to eq 4
62
+ end
63
+
64
+ it "gets the raw json data" do
65
+ stub_server_with_fixture(params: "type=devices&filter=all&used=true", fixture: "temperature_device.json")
66
+
67
+ switches = Domoticz::Device.all
68
+ expect(switches.first.data).to eq ({
69
+ "AddjMulti" => 1.0,
70
+ "AddjMulti2" => 1.0,
71
+ "AddjValue" => 0.0,
72
+ "AddjValue2" => 0.0,
73
+ "BatteryLevel" => 100,
74
+ "CustomImage" => 0,
75
+ "Data" => "20.6 C, 45 %",
76
+ "Description" => "",
77
+ "DewPoint" => "8.25",
78
+ "Favorite" => 1,
79
+ "HardwareID" => 3,
80
+ "HardwareName" => "razberry",
81
+ "HardwareType" => "OpenZWave USB",
82
+ "HardwareTypeVal" => 21,
83
+ "HaveTimeout" => false,
84
+ "Humidity" => 45,
85
+ "HumidityStatus" => "Comfortable",
86
+ "ID" => "0601",
87
+ "LastUpdate" => "2015-12-13 14:02:47",
88
+ "Name" => "Woonkamer",
89
+ "Notifications" => "false",
90
+ "PlanID" => "5",
91
+ "PlanIDs" => [5],
92
+ "Protected" => false,
93
+ "ShowNotifications" => true,
94
+ "SignalLevel" => "-",
95
+ "SubType" => "WTGR800",
96
+ "Temp" => 20.6,
97
+ "Timers" => "false",
98
+ "Type" => "Temp + Humidity",
99
+ "TypeImg" => "temperature",
100
+ "Unit" => 0,
101
+ "Used" => 1,
102
+ "XOffset" => "185",
103
+ "YOffset" => "592",
104
+ "idx" => "47"
105
+ })
106
+ end
107
+ end
@@ -1,7 +1,51 @@
1
1
  require "spec_helper"
2
2
 
3
3
  describe Domoticz do
4
- it "returns hellow world" do
5
- expect(Domoticz.hello).to eq "Hello Domoticz"
4
+ describe "configuration" do
5
+ it "stores the user name" do
6
+ Domoticz.configure do |config|
7
+ config.server = "http://127.0.1.1:8080/"
8
+ config.username = "username"
9
+ config.password = "password"
10
+ end
11
+ expect(Domoticz.configuration.server).to eq "http://127.0.1.1:8080/"
12
+ expect(Domoticz.configuration.username).to eq "username"
13
+ expect(Domoticz.configuration.password).to eq "password"
14
+ end
15
+
16
+ end
17
+
18
+ describe ".perform_api_request" do
19
+ it "performs an api request and parses the resulting JSON" do
20
+ expect(Net::HTTP).to receive(:start).and_return(double(body: %q{{"result": "ok"}}))
21
+
22
+ expect(Domoticz.perform_api_request("param")).to eq({ "result" => "ok" })
23
+ end
24
+
25
+ describe "basic auth" do
26
+ it "uses basic auth to authenticate" do
27
+ Domoticz.configure do |config|
28
+ config.server = "http://127.0.1.1:8080/"
29
+ config.username = "username"
30
+ config.password = "password"
31
+ end
32
+
33
+ expect(Net::HTTP).to receive(:start).and_return(double(body: %q{{"result": "ok"}}))
34
+ expect_any_instance_of(Net::HTTP::Get).to receive(:basic_auth).with("username", "password")
35
+
36
+ Domoticz.perform_api_request("param")
37
+ end
38
+ end
39
+
40
+ it "skips basic auth if no username and password are present" do
41
+ Domoticz.configure do |config|
42
+ config.server = "http://127.0.1.1:8080/"
43
+ end
44
+
45
+ expect(Net::HTTP).to receive(:start).and_return(double(body: %q{{"result": "ok"}}))
46
+ expect_any_instance_of(Net::HTTP::Get).not_to receive(:basic_auth)
47
+
48
+ Domoticz.perform_api_request("param")
49
+ end
6
50
  end
7
- end
51
+ end
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "OK",
3
+ "title": "SwitchLight"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "OK",
3
+ "title": "SwitchLight"
4
+ }
@@ -0,0 +1,4 @@
1
+ {
2
+ "status": "OK",
3
+ "title": "SwitchLight"
4
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "result": [
3
+ {
4
+ "IsDimmer": true,
5
+ "Name": "Switch 1",
6
+ "SubType": "X10",
7
+ "Type": "Lighting 1",
8
+ "idx": "1"
9
+ },
10
+ {
11
+ "IsDimmer": false,
12
+ "Name": "Switch 2",
13
+ "SubType": "X10",
14
+ "Type": "Lighting 1",
15
+ "idx": "2"
16
+ }
17
+ ],
18
+ "status": "OK",
19
+ "title": "GetLightSwitches"
20
+ }
@@ -0,0 +1,46 @@
1
+ {
2
+ "result": [
3
+ {
4
+ "AddjMulti": 1.0,
5
+ "AddjMulti2": 1.0,
6
+ "AddjValue": 0.0,
7
+ "AddjValue2": 0.0,
8
+ "BatteryLevel": 100,
9
+ "CustomImage": 0,
10
+ "Data": "20.6 C, 45 %",
11
+ "Description": "",
12
+ "DewPoint": "8.25",
13
+ "Favorite": 1,
14
+ "HardwareID": 3,
15
+ "HardwareName": "razberry",
16
+ "HardwareType": "OpenZWave USB",
17
+ "HardwareTypeVal": 21,
18
+ "HaveTimeout": false,
19
+ "Humidity": 45,
20
+ "HumidityStatus": "Comfortable",
21
+ "ID": "0601",
22
+ "LastUpdate": "2015-12-13 14:02:47",
23
+ "Name": "Woonkamer",
24
+ "Notifications": "false",
25
+ "PlanID": "5",
26
+ "PlanIDs": [
27
+ 5
28
+ ],
29
+ "Protected": false,
30
+ "ShowNotifications": true,
31
+ "SignalLevel": "-",
32
+ "SubType": "WTGR800",
33
+ "Temp": 20.60,
34
+ "Timers": "false",
35
+ "Type": "Temp + Humidity",
36
+ "TypeImg": "temperature",
37
+ "Unit": 0,
38
+ "Used": 1,
39
+ "XOffset": "185",
40
+ "YOffset": "592",
41
+ "idx": "47"
42
+ }
43
+ ],
44
+ "status": "OK",
45
+ "title": "GetLightSwitches"
46
+ }
@@ -1 +1,9 @@
1
- require "domoticz"
1
+ require "domoticz"
2
+ require "support/spec_helpers"
3
+ require "timecop"
4
+
5
+ RSpec.configure do |config|
6
+ config.before(:each) do
7
+ Domoticz.reset
8
+ end
9
+ end
@@ -0,0 +1,8 @@
1
+ def stub_server_with_fixture(params:, fixture:, required: false)
2
+ json = JSON.parse(File.read("spec/fixtures/" + fixture))
3
+ if required
4
+ expect(Domoticz).to receive(:perform_api_request).with(params).and_return(json)
5
+ else
6
+ allow(Domoticz).to receive(:perform_api_request).with(params).and_return(json)
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: domoticz
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jankees van Woezik
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-03-13 00:00:00.000000000 Z
11
+ date: 2015-12-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: timecop
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
55
69
  description:
56
70
  email:
57
71
  - jankeesvw@gmail.com
@@ -60,15 +74,25 @@ extensions: []
60
74
  extra_rdoc_files: []
61
75
  files:
62
76
  - ".gitignore"
77
+ - ".ruby-version"
63
78
  - Gemfile
64
79
  - LICENSE.txt
65
80
  - README.md
66
81
  - Rakefile
67
82
  - domoticz.gemspec
68
83
  - lib/domoticz.rb
84
+ - lib/domoticz/configuration.rb
85
+ - lib/domoticz/device.rb
69
86
  - lib/domoticz/version.rb
87
+ - spec/device_spec.rb
70
88
  - spec/domoticz_spec.rb
89
+ - spec/fixtures/switch_toggle.json
90
+ - spec/fixtures/switch_turn_off.json
91
+ - spec/fixtures/switch_turn_on.json
92
+ - spec/fixtures/switches.json
93
+ - spec/fixtures/temperature_device.json
71
94
  - spec/spec_helper.rb
95
+ - spec/support/spec_helpers.rb
72
96
  homepage: https://github.com/jankeesvw/domoticz
73
97
  licenses:
74
98
  - MIT
@@ -94,5 +118,12 @@ signing_key:
94
118
  specification_version: 4
95
119
  summary: Manage your Domoticz server from Ruby
96
120
  test_files:
121
+ - spec/device_spec.rb
97
122
  - spec/domoticz_spec.rb
123
+ - spec/fixtures/switch_toggle.json
124
+ - spec/fixtures/switch_turn_off.json
125
+ - spec/fixtures/switch_turn_on.json
126
+ - spec/fixtures/switches.json
127
+ - spec/fixtures/temperature_device.json
98
128
  - spec/spec_helper.rb
129
+ - spec/support/spec_helpers.rb