mios 0.3.0 → 0.3.1

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.
Files changed (59) hide show
  1. data/.rubocop.yml +769 -0
  2. data/README.md +21 -3
  3. data/lib/mios/action.rb +25 -18
  4. data/lib/mios/category.rb +14 -0
  5. data/lib/mios/category_collection.rb +38 -0
  6. data/lib/mios/client.rb +35 -0
  7. data/lib/mios/device.rb +47 -55
  8. data/lib/mios/interface.rb +53 -37
  9. data/lib/mios/job.rb +33 -34
  10. data/lib/mios/room.rb +14 -0
  11. data/lib/mios/scene.rb +22 -0
  12. data/lib/mios/services/camera1.rb +4 -5
  13. data/lib/mios/services/dimmable_light1.rb +5 -6
  14. data/lib/mios/services/door_lock1.rb +12 -13
  15. data/lib/mios/services/energy_metering1.rb +5 -6
  16. data/lib/mios/services/generic_sensor1.rb +4 -4
  17. data/lib/mios/services/ha_device1.rb +7 -8
  18. data/lib/mios/services/humidity_sensor1.rb +4 -5
  19. data/lib/mios/services/hvac_fan_operating_mode1.rb +3 -4
  20. data/lib/mios/services/hvac_operating_state1.rb +3 -4
  21. data/lib/mios/services/hvac_user_operating_mode1.rb +3 -4
  22. data/lib/mios/services/light_sensor1.rb +4 -12
  23. data/lib/mios/services/scene_controller1.rb +4 -4
  24. data/lib/mios/services/security_sensor1.rb +11 -12
  25. data/lib/mios/services/switch_power1.rb +7 -8
  26. data/lib/mios/services/temperature_sensor1.rb +3 -4
  27. data/lib/mios/services/temperature_setpoint1_cool.rb +3 -4
  28. data/lib/mios/services/temperature_setpoint1_heat.rb +3 -4
  29. data/lib/mios/services/window_covering1.rb +5 -6
  30. data/lib/mios/services/zwave_device1.rb +3 -3
  31. data/lib/mios/services/zwave_network1.rb +3 -3
  32. data/lib/mios/type_conversion.rb +21 -0
  33. data/lib/mios/version.rb +1 -1
  34. data/lib/mios.rb +12 -6
  35. data/mios.gemspec +2 -3
  36. data/spec/integration/lights_spec.rb +17 -0
  37. data/spec/lib/mios/category_collection_spec.rb +37 -0
  38. data/spec/lib/mios/category_spec.rb +15 -0
  39. data/spec/lib/mios/client_spec.rb +28 -0
  40. data/spec/lib/mios/device_spec.rb +69 -6
  41. data/spec/lib/mios/interface_spec.rb +123 -7
  42. data/spec/lib/mios/job_spec.rb +54 -0
  43. data/spec/lib/mios/room_spec.rb +26 -0
  44. data/spec/lib/mios/scene_spec.rb +27 -0
  45. data/spec/lib/mios/services/energy_metering1_spec.rb +1 -1
  46. data/spec/lib/mios/services/hvac_fan_operating_mode1_spec.rb +1 -1
  47. data/spec/lib/mios/services/hvac_operating_state1_spec.rb +1 -1
  48. data/spec/lib/mios/services/hvac_user_operating_mode1_spec.rb +1 -1
  49. data/spec/lib/mios/services/temperature_setpoint1_cool_spec.rb +1 -1
  50. data/spec/lib/mios/services/temperature_setpoint1_heat_spec.rb +1 -1
  51. data/spec/spec_helper.rb +18 -0
  52. data/spec/support/device_data/category_filter.json +75 -0
  53. data/spec/support/device_data/data_request.json +7814 -0
  54. data/spec/support/device_data/device_attributes.json +21 -0
  55. data/spec/support/vcr_cassettes/data_request_failure.yml +28 -0
  56. data/spec/support/vcr_cassettes/run_scene.yml +28 -0
  57. data/spec/support/vcr_cassettes/turn_off_light.yml +2648 -0
  58. data/spec/support/vcr_cassettes/turn_on_light.yml +2647 -0
  59. metadata +57 -39
@@ -1,11 +1,127 @@
1
1
  require 'spec_helper'
2
2
 
3
- describe "interface" do
4
- it "should load all the devices" do
5
- VCR.use_cassette('data_request') do
6
- mios = MiOS::Interface.new('http://192.168.50.21:3480')
7
- expect(mios.devices.length).to eql 16
8
- end
3
+ module MiOS
4
+ describe Interface do
5
+ let(:mios) { MiOS::Interface.new('http://192.168.50.21:3480') }
6
+ let(:example_data_request) { JSON.parse(File.read('spec/support/device_data/data_request.json')) }
7
+ let(:example_device_names) { ['ZWave', '_Scene Controller', 'Thermostat', 'Caddx NX584 Security System',
8
+ 'Outdoor Switch', '_GE Advanced Remote', 'Serial_Portlist_2146893057',
9
+ 'ftdi_sio', 'Partition 1', 'Zone 1', 'Generic IP Camera', 'On/Off Outlet',
10
+ '_Home Energy Monitor', '_Home Energy Monitor Clamp 1',
11
+ '_Home Energy Monitor Clamp 2', 'Ergy'] }
12
+ let(:example_rooms) { "[#<MiOS::Room:0x00000104b49f38 @id=1, @name=\"Living Room\">, #<MiOS::Room:0x00000104b49ec0 @id=2, @name=\"Hallway\">]" }
13
+
14
+ describe :initialize do
15
+ context 'when vera unit is available' do
16
+ it 'should respond' do
17
+ VCR.use_cassette('data_request') do
18
+ expect(mios.attributes.length).to eql 29
19
+ end
20
+ end
21
+ end
22
+
23
+ context 'when vera unit is not available' do
24
+ it 'should throw an exception' do
25
+ VCR.use_cassette('data_request_failure') do
26
+ expect { mios.attributes }.to raise_exception 'Device not available'
27
+ end
28
+ end
29
+ end
30
+ end
31
+
32
+ describe :refresh! do
33
+ context 'when vera unit is available' do
34
+ it 'should refresh all the devices' do
35
+ VCR.use_cassette('data_request', allow_playback_repeats: true) do
36
+ # TODO: need to change something in the devices and verify that it
37
+ # returns back to original value
38
+ mios.refresh!
39
+ expect(mios.devices.length).to eql 16
40
+ end
41
+ end
42
+
43
+ it 'should refresh the vera attributes' do
44
+ VCR.use_cassette('data_request', allow_playback_repeats: true) do
45
+ # TODO: need to change something in the attributes and verify that it
46
+ # returns back to original value
47
+ mios.refresh!
48
+ expect(mios.attributes.length).to eql 29
49
+ end
50
+ end
51
+ end
52
+ end
53
+
54
+ describe :categories do
55
+ it 'should return a category collection' do
56
+ VCR.use_cassette('data_request') do
57
+ expect(mios.categories).to_not be_nil
58
+ end
59
+ end
60
+ end
61
+
62
+ describe :attributes do
63
+ it 'should return an array of attributes' do
64
+ VCR.use_cassette('data_request') do
65
+ expect(mios.attributes.length).to eql 29
66
+ expect(mios.attributes['model']).to eql 'MiCasaVerde VeraLite'
67
+ end
68
+ end
69
+ end
70
+
71
+ describe :devices do
72
+ it 'should return an array of devices' do
73
+ VCR.use_cassette('data_request') do
74
+ expect(mios.devices).to be_an(Array)
75
+ expect(mios.devices.length).to eql 16
76
+ end
77
+ end
78
+ end
79
+
80
+ describe :devices_for do
81
+ it 'should return an array of devices with a given category' do
82
+ VCR.use_cassette('data_request') do
83
+ expect(mios.devices_for_label('Lights')).to be_an(Array)
84
+ expect(mios.devices_for_label('Lights').length).to eql 2
85
+ end
86
+ end
87
+ end
88
+
89
+ describe :device_names do
90
+ it 'should return an array of device names' do
91
+ VCR.use_cassette('data_request') do
92
+ expect(mios.device_names).to eql example_device_names
93
+ end
94
+ end
95
+ end
96
+
97
+ describe :rooms do
98
+ it 'should return a list of rooms' do
99
+ VCR.use_cassette('data_request') do
100
+ expect(mios.rooms.length).to eql(2)
101
+ end
102
+ end
103
+ end
104
+
105
+ describe :scenes do
106
+ it 'should return a list of scenes' do
107
+ VCR.use_cassette('data_request') do
108
+ expect(mios.scenes.length).to eql(4)
109
+ end
110
+ end
111
+ end
112
+
113
+ describe :method_missing do
114
+ it 'should return values from the attributes when asked' do
115
+ VCR.use_cassette('data_request') do
116
+ expect(mios.model).to eql 'MiCasaVerde VeraLite'
117
+ end
118
+ end
119
+
120
+ it 'should throw method not found error if not found in attributes' do
121
+ VCR.use_cassette('data_request') do
122
+ expect { mios.foo }.to raise_exception
123
+ end
124
+ end
125
+ end
9
126
  end
10
127
  end
11
-
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+
3
+ describe MiOS::Job do
4
+ let(:job) do
5
+ interface = double('interface', job_status: 1)
6
+ MiOS::Job.new(interface, 1)
7
+ end
8
+
9
+ def stub_job_status(val)
10
+ job.stub(:status).and_return(val)
11
+ end
12
+
13
+ describe 'status predicate methods' do
14
+
15
+ it 'have been defined' do
16
+ job.class::STATUS.each do |status_num, status_name|
17
+ n = status_name.downcase.gsub(' ', '_') + '?'
18
+ job.methods.should include(n.to_sym), "#{n} not defined #{ job.class }"
19
+ end
20
+ end
21
+ end
22
+
23
+ describe :when_complete do
24
+
25
+ [[-1, MiOS::Error::JobTimeout],
26
+ [2, MiOS::Error::JobError],
27
+ [3, MiOS::Error::JobAborted],
28
+ [6, MiOS::Error::JobRequeue]
29
+ ].each do |status_num, err|
30
+ it "raises a #{err.to_s.split('::').last} when status #{ status_num }" do
31
+ stub_job_status(status_num)
32
+ expect { job.when_complete }.to raise_error(err)
33
+ end
34
+ end
35
+ end
36
+
37
+ describe :status do
38
+
39
+ it 'memoizes status instance variable' do
40
+ job.instance_variable_set('@status', 2)
41
+ job.stub(:reload_status!).and_return(3)
42
+ expect(job.status).to eq 2
43
+ end
44
+ end
45
+
46
+ describe :reload_status! do
47
+
48
+ it 'sets status instance variable' do
49
+ job.instance_variable_set('@status', nil)
50
+ job.reload_status!
51
+ expect(job.instance_variable_get('@status')).to eq 1
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+
3
+ module MiOS
4
+ describe Room do
5
+ before do
6
+ @room = Room.new(2, 'Living Room')
7
+ Room.new(1, 'Kitchen')
8
+ Room.new(3, 'Bathroom')
9
+ end
10
+
11
+ describe :initialize do
12
+ it 'should return a Room object' do
13
+ expect(@room).to be_a(Room)
14
+ expect(@room.name).to eql 'Living Room'
15
+ expect(@room.id).to eql 2
16
+ end
17
+ end
18
+
19
+ describe :to_s do
20
+ it 'should return the correct string' do
21
+ expect(@room.to_s).to eql 'Living Room'
22
+ end
23
+ end
24
+
25
+ end
26
+ end
@@ -0,0 +1,27 @@
1
+ require 'spec_helper'
2
+
3
+ module MiOS
4
+ describe Scene do
5
+
6
+ before do
7
+ VCR.use_cassette('data_request') do
8
+ @mios = MiOS::Interface.new('http://192.168.50.21:3480')
9
+ @scene = @mios.scenes[3]
10
+ end
11
+ end
12
+
13
+ describe :run do
14
+ it 'should run the scene' do
15
+ VCR.use_cassette('run_scene') do
16
+ expect(@scene.run).to eql('OK')
17
+ end
18
+ end
19
+ end
20
+
21
+ describe :to_s do
22
+ it 'should return the correct string' do
23
+ expect(@scene.to_s).to eql 'Entering Home'
24
+ end
25
+ end
26
+ end
27
+ end
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::EnergyMetering1 do
4
4
  before do
5
- @meter = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/power-meter.json')))
5
+ @meter = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/power-meter.json')))
6
6
  end
7
7
 
8
8
  describe :watts do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::HVACFanOperatingMode1 do
4
4
  before do
5
- @thermostat = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/thermostat.json')))
5
+ @thermostat = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/thermostat.json')))
6
6
  end
7
7
 
8
8
  describe :mode do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::HVACOperatingState1 do
4
4
  before do
5
- @thermostat = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/thermostat.json')))
5
+ @thermostat = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/thermostat.json')))
6
6
  end
7
7
 
8
8
  describe :operating_state do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::HVACUserOperatingMode1 do
4
4
  before do
5
- @thermostat = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/thermostat.json')))
5
+ @thermostat = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/thermostat.json')))
6
6
  end
7
7
 
8
8
  describe :mode do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::TemperatureSetpoint1Cool do
4
4
  before do
5
- @thermostat = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/thermostat.json')))
5
+ @thermostat = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/thermostat.json')))
6
6
  end
7
7
 
8
8
  describe :cool_target do
@@ -2,7 +2,7 @@ require 'spec_helper'
2
2
 
3
3
  describe MiOS::Services::TemperatureSetpoint1Heat do
4
4
  before do
5
- @thermostat = MiOS::Device.new(nil, '', MultiJson.load(File.read('spec/support/device_data/thermostat.json')))
5
+ @thermostat = MiOS::Device.new(nil, JSON.parse(File.read('spec/support/device_data/thermostat.json')))
6
6
  end
7
7
 
8
8
  describe :heat_target do
data/spec/spec_helper.rb CHANGED
@@ -8,6 +8,11 @@ SimpleCov.start
8
8
  $LOAD_PATH << "#{File.expand_path(File.dirname(__FILE__))}/../lib"
9
9
  require "mios"
10
10
 
11
+ RSpec.configure do |config|
12
+ config.color_enabled = true
13
+ config.before(:each) { silence_output }
14
+ config.after(:each) { enable_output }
15
+ end
11
16
 
12
17
  VCR.configure do |c|
13
18
  c.cassette_library_dir = 'spec/support/vcr_cassettes'
@@ -15,6 +20,19 @@ VCR.configure do |c|
15
20
  c.allow_http_connections_when_no_cassette = true
16
21
  end
17
22
 
23
+ # Redirects stderr and stdout to /dev/null.
24
+ def silence_output
25
+ @orig_stderr = $stderr
26
+ @orig_stdout = $stdout
27
+ $stderr = File.new('/dev/null', 'w')
28
+ $stdout = File.new('/dev/null', 'w')
29
+ end
30
+
31
+ # Replace stdout and stderr so anything else is output correctly.
32
+ def enable_output
33
+ $stderr = @orig_stderr
34
+ $stdout = @orig_stdout
35
+ end
18
36
 
19
37
  def capture_stderr(&block)
20
38
  original_stderr = $stderr
@@ -0,0 +1,75 @@
1
+ [
2
+ {
3
+ "id": 1,
4
+ "categories": [],
5
+ "Label": {
6
+ "lang_tag": "all",
7
+ "text": "All"
8
+ }
9
+ },
10
+ {
11
+ "id": 2,
12
+ "categories": [
13
+ "15"
14
+ ],
15
+ "Label": {
16
+ "lang_tag": "av_devices",
17
+ "text": "Audio/Video"
18
+ }
19
+ },
20
+ {
21
+ "id": 3,
22
+ "categories": [
23
+ "2",
24
+ "3"
25
+ ],
26
+ "Label": {
27
+ "lang_tag": "lights",
28
+ "text": "Lights"
29
+ }
30
+ },
31
+ {
32
+ "id": 4,
33
+ "categories": [
34
+ "6"
35
+ ],
36
+ "Label": {
37
+ "lang_tag": "cameras",
38
+ "text": "Cameras"
39
+ }
40
+ },
41
+ {
42
+ "id": 5,
43
+ "categories": [
44
+ "7"
45
+ ],
46
+ "Label": {
47
+ "lang_tag": "door_locks",
48
+ "text": "Door locks"
49
+ }
50
+ },
51
+ {
52
+ "id": 6,
53
+ "categories": [
54
+ "4",
55
+ "12",
56
+ "16",
57
+ "17",
58
+ "18"
59
+ ],
60
+ "Label": {
61
+ "lang_tag": "sensors",
62
+ "text": "Sensors"
63
+ }
64
+ },
65
+ {
66
+ "id": 7,
67
+ "categories": [
68
+ "5"
69
+ ],
70
+ "Label": {
71
+ "lang_tag": "thermostats",
72
+ "text": "Thermostats"
73
+ }
74
+ }
75
+ ]