tradfri 0.0.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.
- checksums.yaml +7 -0
- data/Gemfile +3 -0
- data/LICENSE.txt +21 -0
- data/README.md +81 -0
- data/lib/tradfri.rb +5 -0
- data/lib/tradfri/client.rb +45 -0
- data/lib/tradfri/device.rb +77 -0
- data/lib/tradfri/gateway.rb +37 -0
- data/lib/tradfri/service.rb +43 -0
- data/lib/tradfri/version.rb +3 -0
- data/tradfri.gemspec +19 -0
- metadata +73 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 75618514cb5e629e1a16cd4689cc95383019522b
|
4
|
+
data.tar.gz: 45cb06ad1531e7104e198b723b42e562957104b8
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: db9caa6476804c7a71b8cb7257e48f79ee31d34adff6fc012cb70de544f33ff27491ea1e3ab4e6d2af70102bbde0e03268136e1fce31b03934d91bf9e4cc3c2e
|
7
|
+
data.tar.gz: 78957f3c6a895ad96d33d1a01438df0daf3d8c167d17bc88096a55a766c7d1440ee1520626107690b0c5d1b7c1484b3ea8b9dd896eb24f27e1e9d30558d65647
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2017 Tom Stuart
|
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 all
|
13
|
+
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 THE
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# `tradfri`, a Ruby interface to IKEA’s smart lighting system
|
2
|
+
|
3
|
+
`tradfri` is a Ruby library for talking to the [gateway][] which controls
|
4
|
+
IKEA’s [TRÅDFRI][] smart lighting system. If you own a TRÅDFRI gateway and some
|
5
|
+
bulbs, this library lets you turn them on and off from a Ruby program.
|
6
|
+
|
7
|
+
The communication with the gateway uses the HTTP-like [CoAP][] protocol over
|
8
|
+
UDP and is secured with [DTLS][]. I don’t know how the hell any of that works
|
9
|
+
so this library sends CoAP requests by shelling out to the `coap-client`
|
10
|
+
program that comes with [`libcoap`][libcoap]. You’ll have to build this
|
11
|
+
yourself and tell `tradfri` where to find it.
|
12
|
+
|
13
|
+
Because the gateway advertises itself with [DNS-SD][], `tradfri` can discover
|
14
|
+
it automatically without you having to specify an IP address. The CoAP messages
|
15
|
+
are encrypted, though, so you’ll need the “serial number” (MAC address) and
|
16
|
+
“security code” (pre-shared key) from the sticker on the underside of the
|
17
|
+
gateway.
|
18
|
+
|
19
|
+
[gateway]: http://www.ikea.com/us/en/catalog/products/00337813/
|
20
|
+
[TRÅDFRI]: http://www.ikea.com/us/en/catalog/categories/departments/lighting/36812/
|
21
|
+
[CoAP]: http://coap.technology/
|
22
|
+
[DTLS]: https://en.wikipedia.org/wiki/Datagram_Transport_Layer_Security
|
23
|
+
[libcoap]: https://libcoap.net/
|
24
|
+
[DNS-SD]: https://en.wikipedia.org/wiki/Zero-configuration_networking#DNS-SD
|
25
|
+
|
26
|
+
## Building `libcoap`
|
27
|
+
|
28
|
+
You should build the `dtls` branch of `libcoap` according to [these
|
29
|
+
instructions][]. You may need to install other tools like `libtool` and
|
30
|
+
`automake` for this to work.
|
31
|
+
|
32
|
+
Here’s what worked for me:
|
33
|
+
|
34
|
+
```bash
|
35
|
+
$ git clone --recursive --branch dtls https://github.com/obgm/libcoap.git
|
36
|
+
$ cd libcoap
|
37
|
+
$ ./autogen.sh
|
38
|
+
$ ./configure --disable-documentation --disable-shared
|
39
|
+
$ make
|
40
|
+
```
|
41
|
+
|
42
|
+
[these instructions]: https://libcoap.net/doc/install.html
|
43
|
+
|
44
|
+
## Example
|
45
|
+
|
46
|
+
Once you’ve built `libcoap`’s `coap-client` binary, you can use `tradfri` like
|
47
|
+
this:
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
require 'tradfri'
|
51
|
+
|
52
|
+
CLIENT_PATH = 'libcoap/examples/coap-client' # wherever you compiled it
|
53
|
+
|
54
|
+
SERIAL_NUMBER = 'a0-b1-c2-d3-e4-f5' # from the sticker on your gateway
|
55
|
+
SECURITY_CODE = 'aBcDeFgHiJkLmNoP' # from the sticker on your gateway
|
56
|
+
SECRETS = { SERIAL_NUMBER => SECURITY_CODE }
|
57
|
+
|
58
|
+
client = Tradfri::Client.new(CLIENT_PATH)
|
59
|
+
gateways = client.discover_gateways(SECRETS)
|
60
|
+
gateway = gateways.first
|
61
|
+
bulbs = gateway.bulbs
|
62
|
+
|
63
|
+
puts "Found: #{bulbs.map(&:name).join(', ')}"
|
64
|
+
|
65
|
+
bulbs.each do |bulb|
|
66
|
+
bulb.on # bulb.off works too
|
67
|
+
end
|
68
|
+
|
69
|
+
loop do
|
70
|
+
bulbs.each do |bulb|
|
71
|
+
bulb.dim(rand) # 0 is full darkness, 1 is full brightness
|
72
|
+
bulb.send(%i{cold normal warm}.sample)
|
73
|
+
end
|
74
|
+
|
75
|
+
sleep 5
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## Limitations
|
80
|
+
|
81
|
+
This is incredibly hacky and doesn’t work very well, but it’s a start.
|
data/lib/tradfri.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'open3'
|
2
|
+
require 'tempfile'
|
3
|
+
require 'tradfri/gateway'
|
4
|
+
require 'tradfri/service'
|
5
|
+
|
6
|
+
module Tradfri
|
7
|
+
class Client < Struct.new(:coap_client_path)
|
8
|
+
METHOD_GET = 'get'
|
9
|
+
METHOD_PUT = 'put'
|
10
|
+
|
11
|
+
def discover_gateways(keys)
|
12
|
+
Service.discover.map do |service|
|
13
|
+
connect_to service.host, service.port, keys[service.mac_address]
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def connect_to(host, port, key)
|
18
|
+
Gateway.new(self, host, port, key)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(key, uri)
|
22
|
+
Tempfile.open do |file|
|
23
|
+
args =
|
24
|
+
'-k', key,
|
25
|
+
'-m', METHOD_GET,
|
26
|
+
'-o', file.path,
|
27
|
+
uri.to_s
|
28
|
+
|
29
|
+
Open3.capture3(coap_client_path, *args)
|
30
|
+
|
31
|
+
file.read
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def put(key, uri, payload)
|
36
|
+
args =
|
37
|
+
'-k', key,
|
38
|
+
'-m', METHOD_PUT,
|
39
|
+
'-e', payload,
|
40
|
+
uri.to_s
|
41
|
+
|
42
|
+
Open3.capture3(coap_client_path, *args)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,77 @@
|
|
1
|
+
require 'json'
|
2
|
+
|
3
|
+
module Tradfri
|
4
|
+
class Device < Struct.new(:gateway, :uri)
|
5
|
+
# https://github.com/IPSO-Alliance/pub/blob/master/reg/xml/3311.xml
|
6
|
+
LIGHT_CONTROL = 3311
|
7
|
+
ON_OFF = 5850
|
8
|
+
OFF = 0
|
9
|
+
ON = 1
|
10
|
+
DIMMER = 5851
|
11
|
+
DIMMER_MIN = 0
|
12
|
+
DIMMER_MAX = 255
|
13
|
+
COLOUR = 5706
|
14
|
+
COLOUR_COLD = 'f5faf6'
|
15
|
+
COLOUR_NORMAL = 'f1e0b5'
|
16
|
+
COLOUR_WARM = 'efd275'
|
17
|
+
|
18
|
+
# I don’t know if/where these are officially documented
|
19
|
+
NAME = 9001
|
20
|
+
|
21
|
+
BULBS = 15001 # TODO discover this
|
22
|
+
|
23
|
+
def bulb?
|
24
|
+
info.has_key? LIGHT_CONTROL.to_s
|
25
|
+
end
|
26
|
+
|
27
|
+
def name
|
28
|
+
info[NAME.to_s]
|
29
|
+
end
|
30
|
+
|
31
|
+
def info
|
32
|
+
data = gateway.get(uri)
|
33
|
+
|
34
|
+
result =
|
35
|
+
begin
|
36
|
+
JSON.parse(data)
|
37
|
+
rescue JSON::ParserError
|
38
|
+
{}
|
39
|
+
end
|
40
|
+
|
41
|
+
case result
|
42
|
+
when Hash
|
43
|
+
result
|
44
|
+
else
|
45
|
+
{}
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def on
|
50
|
+
change ON_OFF => ON
|
51
|
+
end
|
52
|
+
|
53
|
+
def off
|
54
|
+
change ON_OFF => OFF
|
55
|
+
end
|
56
|
+
|
57
|
+
def dim(brightness)
|
58
|
+
change DIMMER => DIMMER_MIN + (brightness * (DIMMER_MAX - DIMMER_MIN)).round
|
59
|
+
end
|
60
|
+
|
61
|
+
def cold
|
62
|
+
change COLOUR => COLOUR_COLD
|
63
|
+
end
|
64
|
+
|
65
|
+
def normal
|
66
|
+
change COLOUR => COLOUR_NORMAL
|
67
|
+
end
|
68
|
+
|
69
|
+
def warm
|
70
|
+
change COLOUR => COLOUR_WARM
|
71
|
+
end
|
72
|
+
|
73
|
+
private def change(state)
|
74
|
+
gateway.put uri, JSON.generate(LIGHT_CONTROL => [state])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'tradfri/device'
|
2
|
+
require 'uri'
|
3
|
+
|
4
|
+
module Tradfri
|
5
|
+
class Gateway < Struct.new(:client, :host, :port, :key)
|
6
|
+
SCHEME = 'coaps'
|
7
|
+
DISCOVERY_PATH = '/.well-known/core'
|
8
|
+
|
9
|
+
def devices
|
10
|
+
client.get(key, discovery_uri).split(',').
|
11
|
+
map { |link| %r{\A</(?<uri>[^>]+)>}.match(link) }.
|
12
|
+
compact.
|
13
|
+
map { |match| discovery_uri.merge(match[:uri]) }.
|
14
|
+
map { |uri| Device.new(self, uri) }
|
15
|
+
end
|
16
|
+
|
17
|
+
def bulbs
|
18
|
+
devices.select(&:bulb?)
|
19
|
+
end
|
20
|
+
|
21
|
+
def get(uri)
|
22
|
+
client.get key, uri
|
23
|
+
end
|
24
|
+
|
25
|
+
def put(uri, payload)
|
26
|
+
client.put key, uri, payload
|
27
|
+
end
|
28
|
+
|
29
|
+
private def discovery_uri
|
30
|
+
URI::Generic.build \
|
31
|
+
scheme: SCHEME,
|
32
|
+
host: host,
|
33
|
+
port: port,
|
34
|
+
path: DISCOVERY_PATH
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,43 @@
|
|
1
|
+
require 'dnssd'
|
2
|
+
|
3
|
+
module Tradfri
|
4
|
+
class Service < Struct.new(:name, :host, :port)
|
5
|
+
SERVICE_TYPE = '_coap'
|
6
|
+
TRANSPORT_PROTOCOL = '_udp'
|
7
|
+
INTERNET_PROTOCOL = DNSSD::Service::IPv4
|
8
|
+
REGISTRATION_TYPE = [SERVICE_TYPE, TRANSPORT_PROTOCOL].join('.')
|
9
|
+
|
10
|
+
def mac_address
|
11
|
+
name.slice %r{(?:\h{2}-){5}\h{2}} ||
|
12
|
+
raise("couldn’t find MAC address in “#{name}”")
|
13
|
+
end
|
14
|
+
|
15
|
+
def self.discover
|
16
|
+
browse(REGISTRATION_TYPE).map do |reply|
|
17
|
+
host, port = resolve(reply, INTERNET_PROTOCOL)
|
18
|
+
Service.new(reply.name, host, port)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
private_class_method def self.browse(registration_type)
|
23
|
+
[].tap do |replies|
|
24
|
+
DNSSD.browse! registration_type do |reply|
|
25
|
+
replies << reply if reply.flags.add?
|
26
|
+
break unless reply.flags.more_coming?
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private_class_method def self.resolve(browse_reply, protocol)
|
32
|
+
DNSSD.resolve! browse_reply do |resolve_reply|
|
33
|
+
DNSSD::Service.getaddrinfo(resolve_reply.target, protocol).each do |address_reply|
|
34
|
+
return [address_reply.address, resolve_reply.port]
|
35
|
+
end
|
36
|
+
|
37
|
+
break unless resolve_reply.flags.more_coming?
|
38
|
+
end
|
39
|
+
|
40
|
+
raise "couldn’t resolve #{browse_reply.name}"
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
data/tradfri.gemspec
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
lib = File.expand_path('../lib', __FILE__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require 'tradfri/version'
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = 'tradfri'
|
7
|
+
spec.version = Tradfri::VERSION
|
8
|
+
spec.author = 'Tom Stuart'
|
9
|
+
spec.email = 'tom@codon.com'
|
10
|
+
spec.summary = 'A Ruby interface to IKEA’s smart lighting system'
|
11
|
+
spec.homepage = 'https://github.com/tomstuart/tradfri'
|
12
|
+
spec.license = 'MIT'
|
13
|
+
|
14
|
+
spec.files = `git ls-files -z`.split("\x0")
|
15
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
16
|
+
spec.require_paths = ['lib']
|
17
|
+
|
18
|
+
spec.add_dependency 'dnssd', '~> 3.0', '>= 3.0.0'
|
19
|
+
end
|
metadata
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: tradfri
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Tom Stuart
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2017-04-30 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: dnssd
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '3.0'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 3.0.0
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 3.0.0
|
33
|
+
description:
|
34
|
+
email: tom@codon.com
|
35
|
+
executables: []
|
36
|
+
extensions: []
|
37
|
+
extra_rdoc_files: []
|
38
|
+
files:
|
39
|
+
- Gemfile
|
40
|
+
- LICENSE.txt
|
41
|
+
- README.md
|
42
|
+
- lib/tradfri.rb
|
43
|
+
- lib/tradfri/client.rb
|
44
|
+
- lib/tradfri/device.rb
|
45
|
+
- lib/tradfri/gateway.rb
|
46
|
+
- lib/tradfri/service.rb
|
47
|
+
- lib/tradfri/version.rb
|
48
|
+
- tradfri.gemspec
|
49
|
+
homepage: https://github.com/tomstuart/tradfri
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata: {}
|
53
|
+
post_install_message:
|
54
|
+
rdoc_options: []
|
55
|
+
require_paths:
|
56
|
+
- lib
|
57
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
requirements: []
|
68
|
+
rubyforge_project:
|
69
|
+
rubygems_version: 2.6.11
|
70
|
+
signing_key:
|
71
|
+
specification_version: 4
|
72
|
+
summary: A Ruby interface to IKEA’s smart lighting system
|
73
|
+
test_files: []
|