homie-mqtt 1.2.2 → 1.4.2

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
  SHA256:
3
- metadata.gz: 520ad8e14f8ce8df9fb824ef2cc3b65e1bef81e70d2b530bc5442732e627a913
4
- data.tar.gz: 801041e538a20ce166eb0528d33cd8c77bb5567e0a7ecea57dba59a63a85bef5
3
+ metadata.gz: df54d2ae01eb1d1972e4d0f5fb40d05286969feff5ad83547476be6d11db05e2
4
+ data.tar.gz: 719a2c270af518fd2ee7d7da4941a211da1f4af90d1e5f0daae2a0c50fd1616f
5
5
  SHA512:
6
- metadata.gz: efa3b6a779f9814ba7d9bade3bde3b3c65325630ec3e81cea5e1e023c338e105367d1f91b039bf9a43230a43bf6260352dfdf21833b6a3fa08c15e6a980e144b
7
- data.tar.gz: '09c32c899c26b9ec86ed5022daff775a48aafaa4aa65e95e0a30f306e381dd3d6262b0b398c923d9a5c9b0a778dd42211b1e680cb004be4196160bdbf57f927e'
6
+ metadata.gz: 19fdcadf299dbe73d3bafca37e32d65b27edb006569ae80f27ecae036e6da17370c9e1b0af405f0517fd6649f5cc0c5bcb75912f00ec2560971d17385502408f
7
+ data.tar.gz: b92d4b4cb05cc96b1a4b0d7e0254476f05db8be89f48d969277bbb09ae56cfcc7e597f5f9ccede42f5279d034bda7a85fe36ad1d652155cf3e38aac961c02667
@@ -40,27 +40,30 @@ module MQTT
40
40
  "#{root_topic}/#{id}"
41
41
  end
42
42
 
43
- def node(*args, **kwargs)
43
+ def node(id, *args, **kwargs)
44
+ raise ArgumentError, "Node '#{id}' already exists" if @nodes.key?(id)
45
+
44
46
  init do |prior_state|
45
- node = Node.new(self, *args, **kwargs)
46
- raise ArgumentError, "Node '#{node.id}' already exists" if @nodes.key?(node.id)
47
- @nodes[node.id] = node
47
+ node = Node.new(self, id, *args, **kwargs)
48
+
49
+ @nodes[id] = node
48
50
  yield node if block_given?
49
51
  if prior_state == :ready
50
52
  node.publish
51
53
  mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1)
52
54
  end
55
+ node
53
56
  end
54
- self
55
57
  end
56
58
 
57
59
  def remove_node(id)
58
- return unless (node = @nodes[id])
60
+ return false unless (node = @nodes[id])
59
61
  init do
60
62
  node.unpublish
61
63
  @nodes.delete(id)
62
64
  mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1) if @published
63
65
  end
66
+ true
64
67
  end
65
68
 
66
69
  def [](id)
@@ -89,7 +92,7 @@ module MQTT
89
92
  property = node[match[:property]] if node
90
93
 
91
94
  unless property&.settable?
92
- @block&.call(topic, packet.payload)
95
+ @block&.call(packet.topic, packet.payload)
93
96
  next
94
97
  end
95
98
 
@@ -120,28 +123,33 @@ module MQTT
120
123
 
121
124
  def init
122
125
  if state == :init
123
- yield(state)
124
- return
126
+ return yield state
125
127
  end
126
128
 
127
129
  prior_state = state
128
130
  mqtt.publish("#{topic}/$state", (state = :init).to_s, retain: true, qos: 1)
129
- yield(prior_state)
131
+ result = nil
132
+ mqtt.batch_publish do
133
+ result = yield prior_state
134
+ end
130
135
  mqtt.publish("#{topic}/$state", (state = :ready).to_s, retain: true, qos: 1)
131
- nil
136
+ result
132
137
  end
133
138
 
134
- private
135
-
136
139
  def clear_topics
140
+ raise ArgumentError, "cannot clear topics once published" if @published
141
+
137
142
  @mqtt.subscribe("#{topic}/#")
138
143
  @mqtt.unsubscribe("#{topic}/#", wait_for_ack: true)
139
144
  while !@mqtt.queue_empty?
140
145
  packet = @mqtt.get
141
146
  @mqtt.publish(packet.topic, retain: true, qos: 0)
142
147
  end
148
+ true
143
149
  end
144
150
 
151
+ private
152
+
145
153
  def topic_regex
146
154
  @topic_regex ||= Regexp.new("^#{Regexp.escape(topic)}/(?<node>#{REGEX})/(?<property>#{REGEX})/set$")
147
155
  end
@@ -23,17 +23,18 @@ module MQTT
23
23
  raise ArgumentError, "Property '#{property.id}' already exists on '#{id}'" if @properties.key?(property.id)
24
24
  @properties[property.id] = property
25
25
  property.publish if prior_state == :ready
26
+ property
26
27
  end
27
- self
28
28
  end
29
29
 
30
30
  def remove_property(id)
31
- return unless (property = @properties[id])
31
+ return false unless (property = @properties[id])
32
32
  init do
33
33
  property.unpublish
34
34
  @properties.delete(id)
35
35
  mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1) if @published
36
36
  end
37
+ true
37
38
  end
38
39
 
39
40
  def [](id)
@@ -6,7 +6,7 @@ module MQTT
6
6
  attr_reader :node, :datatype, :format, :unit, :value
7
7
 
8
8
  def initialize(node, id, name, datatype, value = nil, format: nil, retained: true, unit: nil, &block)
9
- raise ArgumentError, "Invalid Homie datatype" unless %s{string integer float boolean enum color}
9
+ raise ArgumentError, "Invalid Homie datatype" unless %i[string integer float boolean enum color datetime duration].include?(datatype)
10
10
  raise ArgumentError, "retained must be boolean" unless [true, false].include?(retained)
11
11
  format = format.join(",") if format.is_a?(Array) && datatype == :enum
12
12
  if %i{integer float}.include?(datatype) && format.is_a?(Range)
@@ -18,6 +18,7 @@ module MQTT
18
18
  raise ArgumentError, "format is required for enums" if datatype == :enum && format.nil?
19
19
  raise ArgumentError, "format is required for colors" if datatype == :color && format.nil?
20
20
  raise ArgumentError, "format must be either rgb or hsv for colors" if datatype == :color && !%w{rgb hsv}.include?(format.to_s)
21
+ raise ArgumentError, "an initial value cannot be provided for a non-retained property" if !value.nil? && !retained
21
22
 
22
23
  super(id, name)
23
24
 
@@ -49,8 +50,8 @@ module MQTT
49
50
 
50
51
  def value=(value)
51
52
  if @value != value
52
- @value = value
53
- mqtt.publish(topic, value.to_s, retain: retained?, qos: 1) if @published
53
+ @value = value if retained?
54
+ publish_value if @published
54
55
  end
55
56
  end
56
57
 
@@ -108,6 +109,18 @@ module MQTT
108
109
  elsif format == 'hsv'
109
110
  return if value.first > 360 || value[1..2].max > 100
110
111
  end
112
+ when :datetime
113
+ begin
114
+ value = Time.parse(value)
115
+ rescue ArgumentError
116
+ return
117
+ end
118
+ when :duration
119
+ begin
120
+ value = ActiveSupport::Duration.parse(value)
121
+ rescue ActiveSupport::Duration::ISO8601Parser::ParsingError
122
+ return
123
+ end
111
124
  end
112
125
 
113
126
  @block.arity == 2 ? @block.call(value, self) : @block.call(value)
@@ -127,7 +140,7 @@ module MQTT
127
140
  mqtt.publish("#{topic}/$settable", "true", retain: true, qos: 1) if settable?
128
141
  mqtt.publish("#{topic}/$retained", "false", retain: true, qos: 1) unless retained?
129
142
  mqtt.publish("#{topic}/$unit", unit, retain: true, qos: 1) if unit
130
- mqtt.publish(topic, value.to_s, retain: retained?, qos: 1) unless value.nil?
143
+ publish_value unless value.nil?
131
144
  subscribe
132
145
  end
133
146
 
@@ -151,6 +164,15 @@ module MQTT
151
164
  mqtt.unsubscribe("#{topic}/set") if settable?
152
165
  mqtt.publish(topic, retain: retained?, qos: 0) if !value.nil? && retained?
153
166
  end
167
+
168
+ private
169
+
170
+ def publish_value
171
+ serialized = value
172
+ serialized = serialized&.iso8601 if %i[datetime duration].include?(datatype)
173
+
174
+ mqtt.publish(topic, serialized.to_s, retain: retained?, qos: 1)
175
+ end
154
176
  end
155
177
  end
156
178
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MQTT
4
4
  module Homie
5
- VERSION = '1.2.2'
5
+ VERSION = '1.4.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homie-mqtt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.2
4
+ version: 1.4.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-02-28 00:00:00.000000000 Z
11
+ date: 2021-08-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mqtt-ccutrer
@@ -52,7 +52,7 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '13.0'
55
- description:
55
+ description:
56
56
  email: cody@cutrer.com'
57
57
  executables: []
58
58
  extensions: []
@@ -68,7 +68,7 @@ homepage: https://github.com/ccutrer/homie-mqtt
68
68
  licenses:
69
69
  - MIT
70
70
  metadata: {}
71
- post_install_message:
71
+ post_install_message:
72
72
  rdoc_options: []
73
73
  require_paths:
74
74
  - lib
@@ -83,8 +83,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
83
83
  - !ruby/object:Gem::Version
84
84
  version: '0'
85
85
  requirements: []
86
- rubygems_version: 3.1.4
87
- signing_key:
86
+ rubygems_version: 3.1.2
87
+ signing_key:
88
88
  specification_version: 4
89
89
  summary: Library for publishing devices that conform to the Homie spec.
90
90
  test_files: []