huginn_renault_ze_agent 0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '01902892554eb864a1604eca1b1a67a7862b303e554fa6a1cf14d9a6aecde79b'
4
+ data.tar.gz: 81941e9c27ce5150f5ecb106009eba991338adcaf2ade30ec82b0eb53fde2f84
5
+ SHA512:
6
+ metadata.gz: 76e40fd3e92b95940f1c0cf1f32bc8a356e6d44636e229be69fc143403ab3b221e44b375279ee357913480fcd3a6aa65ac458445a2cbb535fbbd8fd3015913ee
7
+ data.tar.gz: ded77044f591baee5679d4b3a6d4501b8889a8afbd364a9cb14487ae67fb43639b408a5bb70e5066a33a1e421806cc9bfb8a85523dae6bebf9a3d395abcd909b
data/LICENSE.txt ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2019 Adrian Schoenig
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,6 @@
1
+ require 'huginn_agent'
2
+ require 'renault_ze/renault_ze_connect'
3
+
4
+ # HuginnAgent.load 'huginn_renault_ze_agent/concerns/my_agent_concern'
5
+ # HuginnAgent.register 'huginn_renault_ze_agent/renault_ze_agent'
6
+ HuginnAgent.register 'huginn_renault_ze_agent/renault_ze_battery_agent'
@@ -0,0 +1,29 @@
1
+ module Agents
2
+ class RenaultZeAgent < Agent
3
+ default_schedule '12h'
4
+
5
+ description <<-MD
6
+ Add a Agent description here
7
+ MD
8
+
9
+ def default_options
10
+ {
11
+ }
12
+ end
13
+
14
+ def validate_options
15
+ end
16
+
17
+ def working?
18
+ # Implement me! Maybe one of these next two lines would be a good fit?
19
+ # checked_without_error?
20
+ # received_event_without_error?
21
+ end
22
+
23
+ # def check
24
+ # end
25
+
26
+ # def receive(incoming_events)
27
+ # end
28
+ end
29
+ end
@@ -0,0 +1,45 @@
1
+ module Agents
2
+ class RenaultZeBatteryAgent < Agent
3
+ default_schedule 'every_5h'
4
+
5
+ cannot_receive_events!
6
+
7
+ description <<-MD
8
+ The Renault ZE Battery Agent checks the battery status of your Renault.
9
+
10
+ To authenticate you need to either set the `username` and `password`, or
11
+ provide credentials named `renault_ze_username` and `renault_ze_password`.
12
+ MD
13
+
14
+ def default_options
15
+ {
16
+ "username" => "",
17
+ "password" => ""
18
+ }
19
+ end
20
+
21
+ form_configurable :username
22
+ form_configurable :password
23
+
24
+ def validate_options
25
+ errors.add(:base, "you need to specify your Renault ZE username or provide a credential names renault_ze_username") unless options["username"].present? || credential("renault_ze_username").present?
26
+ errors.add(:base, "you need to specify your Renault ZE password or provide a credential names renault_ze_password") unless options["password"].present? || credential("renault_ze_password").present?
27
+ end
28
+
29
+ def working?
30
+ checked_without_error?
31
+ end
32
+
33
+ def check
34
+ username = interpolated["username"].present? ? interpolated["username"] : credential("renault_ze_username")
35
+ password = interpolated["password"].present? ? interpolated["password"] : credential("renault_ze_password")
36
+ service = RenaultZE::Client.new(username, password)
37
+ content = service.get_battery(service.login())
38
+
39
+ return if memory["last_update"] == content["last_update"]
40
+ memory["last_update"] = content["last_update"]
41
+ create_event payload: content
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,31 @@
1
+ module RenaultZE
2
+ class Client
3
+ def initialize(username, password)
4
+ @username = username
5
+ @password = password
6
+ end
7
+
8
+ def login
9
+ response = HTTParty.post(
10
+ "https://www.services.renault-ze.com/api/user/login",
11
+ body: {username: @username, password: @password}.to_json
12
+ )
13
+ content = JSON.parse response
14
+ return {
15
+ token: content["token"],
16
+ vin: content["user"]["vehicle_details"]["VIN"]
17
+ }
18
+ end
19
+
20
+ def get_battery(creds)
21
+ response = HTTParty.get(
22
+ "https://www.services.renault-ze.com/api/vehicle/#{creds[:vin]}/battery",
23
+ headers: {"Authorization": "Bearer #{creds["token"]}"}
24
+ )
25
+
26
+ result = JSON.parse response
27
+ result["last_update"] = result["last_update"] / 1000
28
+ return result
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,13 @@
1
+ # require 'rails_helper'
2
+ # require 'huginn_agent/spec_helper'
3
+
4
+ # describe Agents::RenaultZeAgent do
5
+ # before(:each) do
6
+ # @valid_options = Agents::RenaultZeAgent.new.default_options
7
+ # @checker = Agents::RenaultZeAgent.new(:name => "RenaultZeAgent", :options => @valid_options)
8
+ # @checker.user = users(:bob)
9
+ # @checker.save!
10
+ # end
11
+
12
+ # pending "add specs here"
13
+ # end
@@ -0,0 +1,67 @@
1
+ require 'rails_helper'
2
+ require 'huginn_agent/spec_helper'
3
+
4
+ describe Agents::RenaultZeBatteryAgent do
5
+ before(:each) do
6
+ @valid_options = {
7
+ "username" => "some_username",
8
+ "password" => "some_password"
9
+ }
10
+ @checker = Agents::RenaultZeBatteryAgent.new(:name => "RenaultZeBatteryAgent", :options => @valid_options)
11
+ @checker.user = users(:bob)
12
+ @checker.memory = {}
13
+ @checker.save!
14
+ end
15
+
16
+ describe "#validate_options" do
17
+ before do
18
+ expect(@checker).to be_valid
19
+ end
20
+
21
+ it "should reject an empty username" do
22
+ @checker.options["username"] = nil
23
+ expect(@checker).not_to be_valid
24
+ end
25
+
26
+ it "should reject an empty password" do
27
+ @checker.options["password"] = nil
28
+ expect(@checker).not_to be_valid
29
+ end
30
+
31
+ it "should allow credentials (instead of providing as options)" do
32
+ @checker.user.user_credentials.create :credential_name => "renault_ze_username", :credential_value => "some_username"
33
+ @checker.user.user_credentials.create :credential_name => "renault_ze_password", :credential_value => "some_password"
34
+ @checker.options["username"] = nil
35
+ @checker.options["password"] = nil
36
+ expect(@checker).to be_valid
37
+ end
38
+ end
39
+
40
+ describe "#check" do
41
+ before do
42
+ stub_login = {"token": "some_token", "user": {"vehicle_details": {"VIN": "some_vin"}}}
43
+ stub_battery = {"charge_level": 40, "charging": false, "last_update": 1554572276000, "remaining_range": 115.0}
44
+
45
+ stub_request(:post, "https://www.services.renault-ze.com/api/user/login")
46
+ .to_return(:status => 200, :body => JSON.dump(stub_login), :headers => {})
47
+
48
+ stub_request(:get, "https://www.services.renault-ze.com/api/vehicle/some_vin/battery")
49
+ .to_return(:status => 200, :body => JSON.dump(stub_battery), :headers => {})
50
+ end
51
+
52
+ it "should execute the query and emit a new event with battery status" do
53
+ expect { @checker.check }.to change {Event.count}.by(1)
54
+
55
+ event = Event.last
56
+ expect(event.payload['charge_level']).to eq(40)
57
+ expect(event.payload['charging']).to eq(false)
58
+ expect(event.payload['last_update']).to eq(1554572276) # sic
59
+ expect(event.payload['remaining_range']).to eq(115)
60
+ end
61
+
62
+ it "shouldn't emit the same status again if the timestamp didn't change" do
63
+ expect { @checker.check }.to change {Event.count}.by(1)
64
+ expect { @checker.check }.to change {Event.count}.by(0)
65
+ end
66
+ end
67
+ end
metadata ADDED
@@ -0,0 +1,94 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: huginn_renault_ze_agent
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Schoenig
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-04-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 1.17.3
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 1.17.3
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: huginn_agent
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Write a longer description or delete this line.
56
+ email:
57
+ - adrian.schoenig@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - lib/huginn_renault_ze_agent.rb
64
+ - lib/huginn_renault_ze_agent/renault_ze_agent.rb
65
+ - lib/huginn_renault_ze_agent/renault_ze_battery_agent.rb
66
+ - lib/renault_ze/renault_ze_connect.rb
67
+ - spec/renault_ze_agent_spec.rb
68
+ - spec/renault_ze_battery_agent_spec.rb
69
+ homepage: https://github.com/nighthawk/huginn_renault_ze_agent
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubygems_version: 3.0.2
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: Write a short summary, because Rubygems requires one.
92
+ test_files:
93
+ - spec/renault_ze_battery_agent_spec.rb
94
+ - spec/renault_ze_agent_spec.rb