fritzbox-smarthome 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 294b43e027f43ddf8c0b3e162c2c4ce5584a11ae6d05c280e5cfadc20e02a165
4
+ data.tar.gz: c2cb16ea1b9c819e3db06f283d256a24410ac3c8609f0c4a5a9eb68a13be90d9
5
+ SHA512:
6
+ metadata.gz: b3bcc1d1b6e925823fe7c307f5bc1a040caba8ad00b416e24d228573b38d49237f69c3f7201c7fa72c6c3b1bbd3c8f3b111f8ed7458f825369e04315ca6c1e48
7
+ data.tar.gz: e1c4af48265f7f3969199040dfc7c3a7f10e98861238f932a94a2511f33b677ee983ea896199da04247794811e4bedc35fec51e1156b50371217e138febef67e
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+ .byebug_history
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1 @@
1
+ fritzbox-smarthome
@@ -0,0 +1 @@
1
+ 2.5.1
@@ -0,0 +1,5 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5.1
5
+ before_install: gem install bundler -v 1.16.1
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in fritzbox-smarthome.gemspec
6
+ gemspec
@@ -0,0 +1,75 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ fritzbox-smarthome (0.1.0)
5
+ activemodel (~> 5.1)
6
+ activesupport (~> 5.1)
7
+ httparty (~> 0.16)
8
+ nokogiri (~> 1.8)
9
+ nori (~> 2.6)
10
+
11
+ GEM
12
+ remote: https://rubygems.org/
13
+ specs:
14
+ activemodel (5.2.0)
15
+ activesupport (= 5.2.0)
16
+ activesupport (5.2.0)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ addressable (2.5.2)
22
+ public_suffix (>= 2.0.2, < 4.0)
23
+ byebug (10.0.2)
24
+ concurrent-ruby (1.0.5)
25
+ crack (0.4.3)
26
+ safe_yaml (~> 1.0.0)
27
+ diff-lcs (1.3)
28
+ hashdiff (0.3.7)
29
+ httparty (0.16.2)
30
+ multi_xml (>= 0.5.2)
31
+ i18n (1.0.0)
32
+ concurrent-ruby (~> 1.0)
33
+ mini_portile2 (2.3.0)
34
+ minitest (5.11.3)
35
+ multi_xml (0.6.0)
36
+ nokogiri (1.8.2)
37
+ mini_portile2 (~> 2.3.0)
38
+ nori (2.6.0)
39
+ public_suffix (3.0.2)
40
+ rake (10.5.0)
41
+ rspec (3.7.0)
42
+ rspec-core (~> 3.7.0)
43
+ rspec-expectations (~> 3.7.0)
44
+ rspec-mocks (~> 3.7.0)
45
+ rspec-core (3.7.1)
46
+ rspec-support (~> 3.7.0)
47
+ rspec-expectations (3.7.0)
48
+ diff-lcs (>= 1.2.0, < 2.0)
49
+ rspec-support (~> 3.7.0)
50
+ rspec-mocks (3.7.0)
51
+ diff-lcs (>= 1.2.0, < 2.0)
52
+ rspec-support (~> 3.7.0)
53
+ rspec-support (3.7.1)
54
+ safe_yaml (1.0.4)
55
+ thread_safe (0.3.6)
56
+ tzinfo (1.2.5)
57
+ thread_safe (~> 0.1)
58
+ webmock (3.3.0)
59
+ addressable (>= 2.3.6)
60
+ crack (>= 0.3.2)
61
+ hashdiff
62
+
63
+ PLATFORMS
64
+ ruby
65
+
66
+ DEPENDENCIES
67
+ bundler (~> 1.16)
68
+ byebug (~> 10.0)
69
+ fritzbox-smarthome!
70
+ rake (~> 10.0)
71
+ rspec (~> 3.0)
72
+ webmock (~> 3.3)
73
+
74
+ BUNDLED WITH
75
+ 1.16.1
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Klaus Meyer
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,49 @@
1
+ # Fritzbox::Smarthome
2
+
3
+ Ruby client library to interface with Smarthome features of your FritzBox.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'fritzbox-smarthome'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install fritzbox-smarthome
20
+
21
+ ## Usage
22
+
23
+ ```ruby
24
+ Fritzbox::Smarthome.configure do |config|
25
+ config.endpoint = 'https://fritz.box'
26
+ config.username = 'smarthome'
27
+ config.password = 'verysmart'
28
+ config.verify_ssl = false
29
+ end
30
+
31
+ actors = Fritzbox::Smarthome::Actor.all
32
+
33
+ actor = actors.last
34
+ actor.update_hkr_temp_set(BigDecimal.new('21.5'))
35
+ ```
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ Bug reports and pull requests are welcome on GitHub at https://github.com/klausmeyer/fritzbox-smarthome.
46
+
47
+ ## License
48
+
49
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "fritzbox/smarthome"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,34 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'fritzbox/smarthome/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'fritzbox-smarthome'
7
+ spec.version = Fritzbox::Smarthome::VERSION
8
+ spec.authors = ['Klaus Meyer']
9
+ spec.email = ['spam@klaus-meyer.net']
10
+
11
+ spec.summary = 'Client library to interface with Smarthome features of your FritzBox'
12
+ spec.description = spec.description
13
+ spec.homepage = 'https://github.com/klausmeyer/fritzbox-smarthome'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'activesupport', '~> 5.1'
24
+ spec.add_dependency 'activemodel', '~> 5.1'
25
+ spec.add_dependency 'httparty', '~> 0.16'
26
+ spec.add_dependency 'nori', '~> 2.6'
27
+ spec.add_dependency 'nokogiri', '~> 1.8'
28
+
29
+ spec.add_development_dependency 'bundler', '~> 1.16'
30
+ spec.add_development_dependency 'rake', '~> 10.0'
31
+ spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'webmock', '~> 3.3'
33
+ spec.add_development_dependency 'byebug', '~> 10.0'
34
+ end
@@ -0,0 +1,39 @@
1
+ require 'active_model'
2
+ require 'active_support/all'
3
+ require 'httparty'
4
+ require 'nori'
5
+
6
+ require 'fritzbox/smarthome/version'
7
+ require "fritzbox/smarthome/resource"
8
+ require 'fritzbox/smarthome/actor'
9
+
10
+ module Fritzbox
11
+ module Smarthome
12
+ class Configuration
13
+ include ActiveModel::Model
14
+
15
+ attr_accessor \
16
+ :endpoint, \
17
+ :username, \
18
+ :password,
19
+ :verify_ssl,
20
+ :logger
21
+ end
22
+
23
+ @config = Configuration.new.tap do |defaults|
24
+ defaults.endpoint = 'https://fritz.box'
25
+ defaults.username = 'smarthome'
26
+ defaults.password = 'verysmart'
27
+ defaults.verify_ssl = true
28
+ defaults.logger = nil
29
+ end
30
+
31
+ class << self
32
+ def configure(&block)
33
+ block.yield(@config)
34
+ end
35
+
36
+ attr_reader :config
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,55 @@
1
+ module Fritzbox
2
+ module Smarthome
3
+ class Actor < Resource
4
+ include ActiveModel::Model
5
+
6
+ attr_accessor \
7
+ :id,
8
+ :type,
9
+ :ain,
10
+ :present,
11
+ :name,
12
+ :hkr_temp_is,
13
+ :hkr_temp_set,
14
+ :hkr_next_change_period,
15
+ :hkr_next_change_temp
16
+
17
+ class << self
18
+ def all(types: ['group', 'device'])
19
+ response = get(command: 'getdevicelistinfos')
20
+ xml = nori.parse(response.body)
21
+
22
+ Array.wrap(types.map { |type| xml.dig('devicelist', type) }.flatten).map do |data|
23
+ new_from_api(data)
24
+ end
25
+ end
26
+
27
+ def only_heaters
28
+ all.select { |record| record.hkr_temp_is.present? }
29
+ end
30
+
31
+ def new_from_api(data)
32
+ new(
33
+ id: data.dig('@id').to_s,
34
+ type: data.dig('groupinfo').present? ? :group : :device,
35
+ ain: data.dig('@identifier').to_s,
36
+ present: data.dig('present') == '1',
37
+ name: data.dig('name').to_s,
38
+ hkr_temp_is: data.dig('hkr', 'tist').to_i * 0.5,
39
+ hkr_temp_set: data.dig('hkr', 'tsoll').to_i * 0.5,
40
+ hkr_next_change_period: Time.at(data.dig('hkr', 'nextchange', 'endperiod').to_i),
41
+ hkr_next_change_temp: data.dig('hkr', 'nextchange', 'tchange').to_i * 0.5,
42
+ )
43
+ end
44
+ end
45
+
46
+ def update_hkr_temp_set(value)
47
+ raise ArgumentError unless value.is_a? BigDecimal
48
+ value = (value / 0.5).to_i
49
+ response = self.class.get(command: 'sethkrtsoll', ain: ain, param: value)
50
+ raise 'Could not set temperature' unless response.body == "#{value}\n"
51
+ true
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,50 @@
1
+ module Fritzbox
2
+ module Smarthome
3
+ class Resource
4
+ class << self
5
+ def get(command:, ain: nil, param: nil)
6
+ url = "#{config.endpoint}/webservices/homeautoswitch.lua?switchcmd=#{command}&sid=#{authenticate}"
7
+ url = "#{url}&ain=#{ain}" if ain.present?
8
+ url = "#{url}&param=#{param}" if param.present?
9
+
10
+ HTTParty.get(url, **httparty_options)
11
+ end
12
+
13
+ private
14
+
15
+ delegate :config, to: Smarthome
16
+
17
+ def authenticate
18
+ response = HTTParty.get(login_endpoint, **httparty_options)
19
+ xml = nori.parse(response.body)
20
+ challenge = xml.dig('SessionInfo', 'Challenge')
21
+
22
+ md5 = Digest::MD5.hexdigest("#{challenge}-#{config.password}".encode('UTF-16LE'))
23
+
24
+ url = "#{login_endpoint}?response=#{challenge}-#{md5}"
25
+ url = "#{url}&username=#{config.username}" if config.username.present?
26
+
27
+ response = HTTParty.get(url, **httparty_options)
28
+
29
+ xml = nori.parse(response.body)
30
+ xml.dig('SessionInfo', 'SID')
31
+ end
32
+
33
+ def login_endpoint
34
+ "#{config.endpoint}/login_sid.lua"
35
+ end
36
+
37
+ def httparty_options
38
+ {
39
+ verify: config.verify_ssl,
40
+ logger: config.logger
41
+ }.compact
42
+ end
43
+
44
+ def nori
45
+ @nori ||= Nori.new
46
+ end
47
+ end
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,5 @@
1
+ module Fritzbox
2
+ module Smarthome
3
+ VERSION = '0.1.0'.freeze
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,201 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: fritzbox-smarthome
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Klaus Meyer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-04-10 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: activesupport
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activemodel
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '5.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '5.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: httparty
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.16'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.16'
55
+ - !ruby/object:Gem::Dependency
56
+ name: nori
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '2.6'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '2.6'
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.8'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.8'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.16'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.16'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '10.0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '10.0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '3.0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '3.0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: webmock
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - "~>"
130
+ - !ruby/object:Gem::Version
131
+ version: '3.3'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - "~>"
137
+ - !ruby/object:Gem::Version
138
+ version: '3.3'
139
+ - !ruby/object:Gem::Dependency
140
+ name: byebug
141
+ requirement: !ruby/object:Gem::Requirement
142
+ requirements:
143
+ - - "~>"
144
+ - !ruby/object:Gem::Version
145
+ version: '10.0'
146
+ type: :development
147
+ prerelease: false
148
+ version_requirements: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - "~>"
151
+ - !ruby/object:Gem::Version
152
+ version: '10.0'
153
+ description: ''
154
+ email:
155
+ - spam@klaus-meyer.net
156
+ executables: []
157
+ extensions: []
158
+ extra_rdoc_files: []
159
+ files:
160
+ - ".gitignore"
161
+ - ".rspec"
162
+ - ".ruby-gemset"
163
+ - ".ruby-version"
164
+ - ".travis.yml"
165
+ - Gemfile
166
+ - Gemfile.lock
167
+ - LICENSE.txt
168
+ - README.md
169
+ - Rakefile
170
+ - bin/console
171
+ - bin/setup
172
+ - fritzbox-smarthome.gemspec
173
+ - lib/fritzbox/smarthome.rb
174
+ - lib/fritzbox/smarthome/actor.rb
175
+ - lib/fritzbox/smarthome/resource.rb
176
+ - lib/fritzbox/smarthome/version.rb
177
+ homepage: https://github.com/klausmeyer/fritzbox-smarthome
178
+ licenses:
179
+ - MIT
180
+ metadata: {}
181
+ post_install_message:
182
+ rdoc_options: []
183
+ require_paths:
184
+ - lib
185
+ required_ruby_version: !ruby/object:Gem::Requirement
186
+ requirements:
187
+ - - ">="
188
+ - !ruby/object:Gem::Version
189
+ version: '0'
190
+ required_rubygems_version: !ruby/object:Gem::Requirement
191
+ requirements:
192
+ - - ">="
193
+ - !ruby/object:Gem::Version
194
+ version: '0'
195
+ requirements: []
196
+ rubyforge_project:
197
+ rubygems_version: 2.7.6
198
+ signing_key:
199
+ specification_version: 4
200
+ summary: Client library to interface with Smarthome features of your FritzBox
201
+ test_files: []