homie-mqtt 1.3.0 → 1.4.4

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: 64cfaf8cbbff82a3059c8fcfa639efc39f006241cd79aaadb6bce200eb82a9cc
4
- data.tar.gz: d9045a9c729515c2912aeb11b20c5ecad9885b7e0f4df95263e688d74d72f077
3
+ metadata.gz: fe33f7f3d3d6588a385857dfad083584df0de5af3e66e540732dc3307b7d8a0f
4
+ data.tar.gz: ba032b7b5760ac2a71915ebbbc4e3671cceff13bcf3b75bba3181b86a0b3c015
5
5
  SHA512:
6
- metadata.gz: 04ea1126fe4310cc758ddf23fe859f380a0250baf0998a1fab3d695c6f18e6087028dd35de8f619414ec0512d46ac24fb6fb91cae2821729c18b2a91b209455d
7
- data.tar.gz: 6200b420bed16a9267cdde8f7192c52d8cd72568dcb72e35247620bd4b71031069a12986b41290329c199bfbf0fc7abdbf25a81d8fdbe1bcce7906b09a883673
6
+ metadata.gz: f7dabb99327ba052edef139511680ab1a9c5c90e21ce0c3229585e238846f5a2f6abd42365bb2f73cf43767b55e41ad091d45d1c5003fd86d91599d52e73347f
7
+ data.tar.gz: a302d00a71bcde1495604da147e2d0e200a0dc686a074cd285e95d1861bc334007cd94a45d79d206c19c80dc5bd93dcec504c07488ae362cc94d592986663a8f
@@ -6,6 +6,8 @@ module MQTT
6
6
  module Homie
7
7
  class Device < Base
8
8
  attr_reader :root_topic, :state, :mqtt
9
+ attr_accessor :logger
10
+ attr_accessor :out_of_band_topic_proc
9
11
 
10
12
  def initialize(id, name, root_topic: nil, mqtt: nil, clear_topics: true, &block)
11
13
  super(id, name)
@@ -13,7 +15,7 @@ module MQTT
13
15
  @state = :init
14
16
  @nodes = {}
15
17
  @published = false
16
- @block = block
18
+ @out_of_band_topic_proc = block
17
19
  mqtt = MQTT::Client.new(mqtt) if mqtt.is_a?(String)
18
20
  @mqtt = mqtt || MQTT::Client.new
19
21
  @mqtt.set_will("#{topic}/$state", "lost", retain: true, qos: 1)
@@ -87,12 +89,13 @@ module MQTT
87
89
  Thread.current.report_on_exception = false
88
90
 
89
91
  mqtt.get do |packet|
92
+ logger&.debug("received packet at #{packet.topic} with payload #{packet.payload.inspect}")
90
93
  match = packet.topic.match(topic_regex)
91
94
  node = @nodes[match[:node]] if match
92
95
  property = node[match[:property]] if node
93
96
 
94
97
  unless property&.settable?
95
- @block&.call(topic, packet.payload)
98
+ @out_of_band_topic_proc&.call(packet.topic, packet.payload)
96
99
  next
97
100
  end
98
101
 
@@ -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)
@@ -51,7 +51,7 @@ module MQTT
51
51
  def value=(value)
52
52
  if @value != value
53
53
  @value = value if retained?
54
- mqtt.publish(topic, value.to_s, retain: retained?, qos: 1) if @published
54
+ publish_value if @published
55
55
  end
56
56
  end
57
57
 
@@ -109,6 +109,18 @@ module MQTT
109
109
  elsif format == 'hsv'
110
110
  return if value.first > 360 || value[1..2].max > 100
111
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
112
124
  end
113
125
 
114
126
  @block.arity == 2 ? @block.call(value, self) : @block.call(value)
@@ -128,7 +140,7 @@ module MQTT
128
140
  mqtt.publish("#{topic}/$settable", "true", retain: true, qos: 1) if settable?
129
141
  mqtt.publish("#{topic}/$retained", "false", retain: true, qos: 1) unless retained?
130
142
  mqtt.publish("#{topic}/$unit", unit, retain: true, qos: 1) if unit
131
- mqtt.publish(topic, value.to_s, retain: retained?, qos: 1) unless value.nil?
143
+ publish_value unless value.nil?
132
144
  subscribe
133
145
  end
134
146
 
@@ -152,6 +164,17 @@ module MQTT
152
164
  mqtt.unsubscribe("#{topic}/set") if settable?
153
165
  mqtt.publish(topic, retain: retained?, qos: 0) if !value.nil? && retained?
154
166
  end
167
+
168
+ private
169
+
170
+ def publish_value
171
+ serialized = value
172
+ serialized = serialized&.iso8601 if %i[datetime duration].include?(datatype)
173
+ serialized = serialized.to_s
174
+
175
+ node.device.logger&.debug("publishing #{serialized.inspect} to #{topic}")
176
+ mqtt.publish(topic, serialized, retain: retained?, qos: 1)
177
+ end
155
178
  end
156
179
  end
157
180
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MQTT
4
4
  module Homie
5
- VERSION = '1.3.0'
5
+ VERSION = '1.4.4'
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.3.0
4
+ version: 1.4.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cody Cutrer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-16 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