homie-mqtt 1.6.3 → 1.8.0

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: eb087a1fa491f77b56c7231be4578549e946f243546544d26eb02b729ff0f1e0
4
- data.tar.gz: 21aeb256936a4425c10262bfbb695712708c607358391ff8c7cc886aa9d5cbbf
3
+ metadata.gz: 580fba09d7d9a5fa0de4f8cf86a6a63b6d9cb1d62871b869b67df1fffcd16bf8
4
+ data.tar.gz: 1c286151ba1eed536ca0ff415523d9e5c22c6d650e25cf1f4c832cb3423e95f5
5
5
  SHA512:
6
- metadata.gz: ad9843d1d1d2210050dd0e1149bd77ac83213bab6916e168514f632c809f1609192d3d4091f5bac6f1801441d6ba7c95e129b33f6a9f6e32df310ce8e671d1e8
7
- data.tar.gz: 933767440a03109805b509d9486a3b299c94b713652c4910310d02275a97309a764f98372dbbcd9c6d8c4be0ecd77f26a74161897e9861b2eb2dde145e0cc8d2
6
+ metadata.gz: f69b585e7f81866e37e061a9d217f3e0dfa491d8413ea96e4dde3d542548fe642b75cbab9764d4fb16c88080cd3a5487e41cb89459e5b053e129dd2195560fa0
7
+ data.tar.gz: 8ac03bfdfcff14982f27725b20dbe19597883aa81831ebcfd53a93d358ce5d353437399fd3939bb8a834a47ebcc879d8473d70368a2e0d88d0ddb9e604d1f19e
@@ -3,7 +3,7 @@
3
3
  module MQTT
4
4
  module Homie
5
5
  class Base
6
- REGEX = "[a-z0-9][a-z0-9\-]*"
6
+ REGEX = "[a-z0-9][a-z0-9-]*"
7
7
 
8
8
  attr_reader :id, :name
9
9
 
@@ -12,13 +12,14 @@ module MQTT
12
12
  attr_reader :root_topic, :state, :mqtt
13
13
  attr_accessor :logger, :out_of_band_topic_proc
14
14
 
15
- def initialize(id, name, root_topic: nil, mqtt: nil, clear_topics: true, &block)
15
+ def initialize(id, name, root_topic: nil, mqtt: nil, clear_topics: true, metadata: true, &block)
16
16
  super(id, name)
17
17
  @root_topic = root_topic || "homie"
18
18
  @state = :init
19
19
  @nodes = {}
20
20
  @published = false
21
21
  @out_of_band_topic_proc = block
22
+ @metadata = metadata
22
23
  # retry forever
23
24
  mqtt = MQTT::Client.new(mqtt, reconnect_limit: nil) if mqtt.is_a?(String)
24
25
  mqtt = MQTT::Client.new(reconnect_limit: nil, **mqtt) if mqtt.is_a?(Hash)
@@ -88,16 +89,26 @@ module MQTT
88
89
  @nodes.count
89
90
  end
90
91
 
92
+ def empty?
93
+ @nodes.empty?
94
+ end
95
+
91
96
  def published?
92
97
  @published
93
98
  end
94
99
 
100
+ def metadata?
101
+ @metadata
102
+ end
103
+
95
104
  def publish
96
105
  return if published?
97
106
 
98
107
  mqtt.batch_publish do
99
- mqtt.publish("#{topic}/$homie", VERSION, retain: true, qos: 1)
100
- mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
108
+ if metadata?
109
+ mqtt.publish("#{topic}/$homie", VERSION, retain: true, qos: 1)
110
+ mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
111
+ end
101
112
  mqtt.publish("#{topic}/$state", @state.to_s, retain: true, qos: 1)
102
113
 
103
114
  @subscription_thread = Thread.new do
@@ -119,7 +130,7 @@ module MQTT
119
130
  end
120
131
  end
121
132
 
122
- mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1)
133
+ mqtt.publish("#{topic}/$nodes", @nodes.keys.join(","), retain: true, qos: 1) if metadata?
123
134
  @nodes.each_value(&:publish)
124
135
 
125
136
  yield if block_given?
@@ -57,6 +57,14 @@ module MQTT
57
57
  @properties.each_value(&block)
58
58
  end
59
59
 
60
+ def count
61
+ @properties.count
62
+ end
63
+
64
+ def empty?
65
+ @properties.empty?
66
+ end
67
+
60
68
  def mqtt
61
69
  device.mqtt
62
70
  end
@@ -76,18 +84,21 @@ module MQTT
76
84
 
77
85
  def publish
78
86
  mqtt.batch_publish do
79
- unless published?
80
- mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
81
- mqtt.publish("#{topic}/$type", @type.to_s, retain: true, qos: 1)
82
- @published = true
87
+ if device.metadata?
88
+ unless published?
89
+ mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
90
+ mqtt.publish("#{topic}/$type", @type.to_s, retain: true, qos: 1)
91
+ @published = true
92
+ end
93
+
94
+ mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1)
83
95
  end
84
-
85
- mqtt.publish("#{topic}/$properties", @properties.keys.join(","), retain: true, qos: 1)
86
96
  @properties.each_value(&:publish)
87
97
  end
88
98
  end
89
99
 
90
100
  def unpublish
101
+ return unless device.metadata?
91
102
  return unless published?
92
103
 
93
104
  @published = false
@@ -16,6 +16,7 @@ module MQTT
16
16
  retained: true,
17
17
  unit: nil,
18
18
  non_standard_value_check: nil,
19
+ optimistic: false,
19
20
  &block)
20
21
  raise ArgumentError, "Invalid Homie datatype" unless %i[string
21
22
  integer
@@ -41,6 +42,7 @@ module MQTT
41
42
  @value = value
42
43
  @published = false
43
44
  @non_standard_value_check = non_standard_value_check
45
+ @optimistic = optimistic
44
46
  @block = block
45
47
  end
46
48
 
@@ -54,6 +56,7 @@ module MQTT
54
56
  else
55
57
  ", retained=false"
56
58
  end
59
+ result << ", optimistic=true" if optimistic?
57
60
  result << ">"
58
61
  result.freeze
59
62
  end
@@ -78,6 +81,10 @@ module MQTT
78
81
  !!@block
79
82
  end
80
83
 
84
+ def optimistic?
85
+ @optimistic
86
+ end
87
+
81
88
  def value=(value)
82
89
  return if @value == value
83
90
 
@@ -172,7 +179,8 @@ module MQTT
172
179
  casted_value = @non_standard_value_check&.call(value) if casted_value.nil?
173
180
  return if casted_value.nil?
174
181
 
175
- @block.arity == 2 ? @block.call(casted_value, self) : @block.call(casted_value)
182
+ (@block.arity == 2) ? @block.call(casted_value, self) : @block.call(casted_value)
183
+ self.value = casted_value if optimistic?
176
184
  end
177
185
 
178
186
  def mqtt
@@ -187,12 +195,14 @@ module MQTT
187
195
  return if published?
188
196
 
189
197
  mqtt.batch_publish do
190
- mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
191
- mqtt.publish("#{topic}/$datatype", datatype.to_s, retain: true, qos: 1)
192
- mqtt.publish("#{topic}/$format", format, retain: true, qos: 1) if format
193
- mqtt.publish("#{topic}/$settable", "true", retain: true, qos: 1) if settable?
194
- mqtt.publish("#{topic}/$retained", "false", retain: true, qos: 1) unless retained?
195
- mqtt.publish("#{topic}/$unit", unit, retain: true, qos: 1) if unit
198
+ if device.metadata?
199
+ mqtt.publish("#{topic}/$name", name, retain: true, qos: 1)
200
+ mqtt.publish("#{topic}/$datatype", datatype.to_s, retain: true, qos: 1)
201
+ mqtt.publish("#{topic}/$format", format, retain: true, qos: 1) if format
202
+ mqtt.publish("#{topic}/$settable", "true", retain: true, qos: 1) if settable?
203
+ mqtt.publish("#{topic}/$retained", "false", retain: true, qos: 1) unless retained?
204
+ mqtt.publish("#{topic}/$unit", unit, retain: true, qos: 1) if unit
205
+ end
196
206
  publish_value(value) unless value.nil?
197
207
  subscribe
198
208
  end
@@ -209,12 +219,14 @@ module MQTT
209
219
 
210
220
  @published = false
211
221
 
212
- mqtt.publish("#{topic}/$name", retain: true, qos: 0)
213
- mqtt.publish("#{topic}/$datatype", retain: true, qos: 0)
214
- mqtt.publish("#{topic}/$format", retain: true, qos: 0) if format
215
- mqtt.publish("#{topic}/$settable", retain: true, qos: 0) if settable?
216
- mqtt.publish("#{topic}/$retained", retain: true, qos: 0) unless retained?
217
- mqtt.publish("#{topic}/$unit", retain: true, qos: 0) if unit
222
+ if device.metadata?
223
+ mqtt.publish("#{topic}/$name", retain: true, qos: 0)
224
+ mqtt.publish("#{topic}/$datatype", retain: true, qos: 0)
225
+ mqtt.publish("#{topic}/$format", retain: true, qos: 0) if format
226
+ mqtt.publish("#{topic}/$settable", retain: true, qos: 0) if settable?
227
+ mqtt.publish("#{topic}/$retained", retain: true, qos: 0) unless retained?
228
+ mqtt.publish("#{topic}/$unit", retain: true, qos: 0) if unit
229
+ end
218
230
  mqtt.unsubscribe("#{topic}/set") if settable?
219
231
  mqtt.publish(topic, retain: retained?, qos: 0) if !value.nil? && retained?
220
232
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MQTT
4
4
  module Homie
5
- VERSION = "1.6.3"
5
+ VERSION = "1.8.0"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: homie-mqtt
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.6.3
4
+ version: 1.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2022-08-26 00:00:00.000000000 Z
10
+ date: 2025-05-13 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: mqtt-ccutrer
@@ -44,20 +43,6 @@ dependencies:
44
43
  - - "~>"
45
44
  - !ruby/object:Gem::Version
46
45
  version: 0.0.5
47
- - !ruby/object:Gem::Dependency
48
- name: byebug
49
- requirement: !ruby/object:Gem::Requirement
50
- requirements:
51
- - - "~>"
52
- - !ruby/object:Gem::Version
53
- version: '9.0'
54
- type: :development
55
- prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- requirements:
58
- - - "~>"
59
- - !ruby/object:Gem::Version
60
- version: '9.0'
61
46
  - !ruby/object:Gem::Dependency
62
47
  name: rake
63
48
  requirement: !ruby/object:Gem::Requirement
@@ -72,49 +57,6 @@ dependencies:
72
57
  - - "~>"
73
58
  - !ruby/object:Gem::Version
74
59
  version: '13.0'
75
- - !ruby/object:Gem::Dependency
76
- name: rubocop
77
- requirement: !ruby/object:Gem::Requirement
78
- requirements:
79
- - - "~>"
80
- - !ruby/object:Gem::Version
81
- version: '1.23'
82
- type: :development
83
- prerelease: false
84
- version_requirements: !ruby/object:Gem::Requirement
85
- requirements:
86
- - - "~>"
87
- - !ruby/object:Gem::Version
88
- version: '1.23'
89
- - !ruby/object:Gem::Dependency
90
- name: rubocop-performance
91
- requirement: !ruby/object:Gem::Requirement
92
- requirements:
93
- - - "~>"
94
- - !ruby/object:Gem::Version
95
- version: '1.12'
96
- type: :development
97
- prerelease: false
98
- version_requirements: !ruby/object:Gem::Requirement
99
- requirements:
100
- - - "~>"
101
- - !ruby/object:Gem::Version
102
- version: '1.12'
103
- - !ruby/object:Gem::Dependency
104
- name: rubocop-rake
105
- requirement: !ruby/object:Gem::Requirement
106
- requirements:
107
- - - "~>"
108
- - !ruby/object:Gem::Version
109
- version: '0.6'
110
- type: :development
111
- prerelease: false
112
- version_requirements: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - "~>"
115
- - !ruby/object:Gem::Version
116
- version: '0.6'
117
- description:
118
60
  email: cody@cutrer.com'
119
61
  executables: []
120
62
  extensions: []
@@ -132,7 +74,6 @@ licenses:
132
74
  - MIT
133
75
  metadata:
134
76
  rubygems_mfa_required: 'true'
135
- post_install_message:
136
77
  rdoc_options: []
137
78
  require_paths:
138
79
  - lib
@@ -147,8 +88,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
147
88
  - !ruby/object:Gem::Version
148
89
  version: '0'
149
90
  requirements: []
150
- rubygems_version: 3.3.5
151
- signing_key:
91
+ rubygems_version: 3.6.3
152
92
  specification_version: 4
153
93
  summary: Library for publishing devices that conform to the Homie spec.
154
94
  test_files: []