mqtt-homeassistant 0.0.1 → 0.1.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: 63f377ed2f680df05fcdd443d7827be88ad49630b6a2e046a1291f3ccbe1fe41
4
- data.tar.gz: 9f48fd3a21b57073ae31bc488c5f8fda3369a2e3d2ef37d41426e1865d1d307a
3
+ metadata.gz: e4abb16dba6ea2ddab3b0619ded2e0712e2b997d710cc69d367a5234ee00371c
4
+ data.tar.gz: b22f5a9ddc2fc216c8a57564f6691661fb0cbaeedfa00d3de76548cf23de299f
5
5
  SHA512:
6
- metadata.gz: e378a1edd2a12093be108cb9b1685393422274fcc65b578dee2e73c0a429a06dc35f17889f113998ac03a3f4a936fb01028a07f201061dc5420b3557b204b0f2
7
- data.tar.gz: 82f86cf62752b56a357844149c2b51952a9fa448783a29e2396053a77648d73acae97921db149a7745a423b97edbd606bd53185f8431b865026383d7cb7d2227
6
+ metadata.gz: 9704e5112d570e0a7cdf255a365af0440a7a24c09376513703eb25726d46f180334e6c78f7ea6ad71ec2faa38adbdc511e4f453430f08840555d44276a1598d0
7
+ data.tar.gz: ad8ce6569cbd7fccc1fa968061f4f9a966f97710d1d51d723128f67780191d32edeb5f6738f3eb5339d3a8305d9b2e3745b5e52cc92d2b816305e5010faff0a4
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ MQTT::Homie::Device.attr_accessor :home_assistant_device, :home_assistant_discovery_prefix
@@ -0,0 +1,74 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MQTT
4
+ module HomeAssistant
5
+ module Homie
6
+ module Node
7
+ {
8
+ climate: %i[action
9
+ aux
10
+ away_mode
11
+ current_temperature
12
+ fan_mode
13
+ mode
14
+ hold
15
+ power
16
+ swing_mode
17
+ temperature
18
+ temperature_high
19
+ temperature_low],
20
+ fan: [nil, :oscillation],
21
+ humidifier: [nil, :target, :mode],
22
+ light: [nil, :brightness, :color_mode, :color_temp, :effect, :hs, :rgb, :white, :xy]
23
+ }.each do |(integration, properties)|
24
+ has_nil = properties.include?(nil)
25
+ method_arguments = []
26
+ method_arguments << "property" if has_nil
27
+ method_arguments.concat(properties.compact.map { |p| "#{p}_property: nil" })
28
+ transforms = []
29
+ transforms << "property = self[property] if property.is_a?(String)"
30
+ transforms.concat(
31
+ properties.compact.map do |p|
32
+ "kwargs[:#{p}_property] = #{p}_property.is_a?(String) ? self[#{p}_property] : #{p}_property"
33
+ end
34
+ )
35
+ args_code = has_nil ? "args = [property]" : "args = []"
36
+
37
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
38
+ def hass_#{integration}(#{method_arguments.join(", ")}, device: nil, discovery_prefix: nil, **kwargs)
39
+ #{transforms.join("\n")}
40
+ discovery_prefix ||= self.device.home_assistant_discovery_prefix
41
+ device = self.device.home_assistant_device.merge(device || {}) if self.device.home_assistant_device
42
+
43
+ kwargs[:device] = device
44
+ kwargs[:discovery_prefix] = discovery_prefix
45
+ #{args_code}
46
+ if published?
47
+ HomeAssistant.publish_#{integration}(*args, **kwargs)
48
+ else
49
+ pending_hass_registrations << [:publish_#{integration}, args, kwargs]
50
+ end
51
+ end
52
+ RUBY
53
+ end
54
+
55
+ def publish
56
+ super.tap do
57
+ @pending_hass_registrations&.each do |(method, args, kwargs)|
58
+ HomeAssistant.public_send(method, *args, **kwargs)
59
+ end
60
+ @pending_hass_registrations = nil
61
+ end
62
+ end
63
+
64
+ private
65
+
66
+ def pending_hass_registrations
67
+ @pending_hass_registrations ||= []
68
+ end
69
+ end
70
+ end
71
+ end
72
+ end
73
+
74
+ MQTT::Homie::Node.prepend(MQTT::HomeAssistant::Homie::Node)
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MQTT
4
+ module HomeAssistant
5
+ module Homie
6
+ module Property
7
+ def initialize(*args, hass: nil, **kwargs)
8
+ super(*args, **kwargs)
9
+
10
+ return unless hass
11
+
12
+ case hass
13
+ when Symbol
14
+ public_send("hass_#{hass}")
15
+ when Hash
16
+ raise ArgumentError, "hass must only contain one item" unless hass.length == 1
17
+
18
+ public_send("hass_#{hass.first.first}", **hass.first.last)
19
+ else
20
+ raise ArgumentError, "hass must be a Symbol or a Hash of HASS device type to additional HASS options"
21
+ end
22
+ end
23
+
24
+ %i[binary_sensor fan humidifier light number scene select sensor switch].each do |integration|
25
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
26
+ def hass_#{integration}(device: nil, discovery_prefix: nil, **kwargs)
27
+ discovery_prefix ||= self.device.home_assistant_discovery_prefix
28
+ device = self.device.home_assistant_device.merge(device || {}) if self.device.home_assistant_device
29
+
30
+ kwargs[:device] = device
31
+ kwargs[:discovery_prefix] = discovery_prefix
32
+ if published?
33
+ HomeAssistant.publish_#{integration}(self, **kwargs)
34
+ else
35
+ kwargs[:method] = :publish_#{integration}
36
+ pending_hass_registrations << kwargs
37
+ end
38
+ end
39
+ RUBY
40
+ end
41
+
42
+ def publish
43
+ super.tap do
44
+ @pending_hass_registrations&.each do |entity|
45
+ method = entity.delete(:method)
46
+ HomeAssistant.public_send(method, self, **entity)
47
+ end
48
+ @pending_hass_registrations = nil
49
+ end
50
+ end
51
+
52
+ private
53
+
54
+ def pending_hass_registrations
55
+ @pending_hass_registrations ||= []
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
61
+
62
+ MQTT::Homie::Property.prepend(MQTT::HomeAssistant::Homie::Property)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MQTT
4
4
  module HomeAssistant
5
- VERSION = "0.0.1"
5
+ VERSION = "0.1.2"
6
6
  end
7
7
  end
@@ -2,6 +2,11 @@
2
2
 
3
3
  require "json"
4
4
 
5
+ require "mqtt/homie"
6
+ require "mqtt/home_assistant/homie/device"
7
+ require "mqtt/home_assistant/homie/node"
8
+ require "mqtt/home_assistant/homie/property"
9
+
5
10
  module MQTT
6
11
  module HomeAssistant
7
12
  class << self
@@ -159,8 +164,9 @@ module MQTT
159
164
  entity_category: entity_category,
160
165
  icon: icon)
161
166
  config[:unique_id] = "#{node.device.id}_#{id || node.id}"
167
+ read_only_props = %i[action current_temperature]
162
168
  properties.each do |prefix, property|
163
- add_property(config, property, prefix, templates)
169
+ add_property(config, property, prefix, templates: templates, read_only: read_only_props.include?(prefix))
164
170
  end
165
171
  temp_properties = [
166
172
  temperature_property,
@@ -210,13 +216,13 @@ module MQTT
210
216
  entity_category: entity_category,
211
217
  icon: icon,
212
218
  templates: {})
213
- add_property(config, oscillation_property, :oscillation_property, templates)
214
- add_property(config, percentage_property, :percentage, templates)
219
+ add_property(config, oscillation_property, :oscillation_property, templates: templates)
220
+ add_property(config, percentage_property, :percentage, templates: templates)
215
221
  if percentage_property&.range
216
222
  config[:speed_range_min] = percentage_property.range.begin
217
223
  config[:speed_range_max] = percentage_property.range.end
218
224
  end
219
- add_property(config, preset_mode_property, :preset, templates)
225
+ add_property(config, preset_mode_property, :preset, templates: templates)
220
226
  add_enum(config, preset_mode_property, :preset)
221
227
 
222
228
  publish(node.mqtt, "fan", config, discovery_prefix: discovery_prefix)
@@ -300,6 +306,7 @@ module MQTT
300
306
  device: device,
301
307
  entity_category: entity_category,
302
308
  icon: icon)
309
+ config[:unique_id] = "#{property.device.id}_#{property.node.id}_#{property.id}"
303
310
  add_property(config, property)
304
311
  case property.datatype
305
312
  when :boolean
@@ -310,21 +317,21 @@ module MQTT
310
317
  when :float
311
318
  config[:payload_off] = "0.0"
312
319
  end
313
- add_property(config, brightness_property, :brightness, templates)
320
+ add_property(config, brightness_property, :brightness, templates: templates)
314
321
  config[:brightness_scale] = brightness_property.range.end if brightness_property&.range
315
- add_property(config, color_mode_property, :color_mode, templates)
316
- add_property(config, color_temp_property, :color_temp, templates)
322
+ add_property(config, color_mode_property, :color_mode, templates: templates)
323
+ add_property(config, color_temp_property, :color_temp, templates: templates)
317
324
  if color_temp_property&.range && color_temp_property.unit == "mired"
318
325
  config[:min_mireds] = color_temp_property.range.begin
319
326
  config[:max_mireds] = color_temp_property.range.end
320
327
  end
321
- add_property(config, effect_property, :effect, templates)
328
+ add_property(config, effect_property, :effect, templates: templates)
322
329
  config[:effect_list] = effect_property.range if effect_property&.datatype == :enum
323
- add_property(config, hs_property, :hs, templates)
324
- add_property(config, rgb_property, :rgb, templates)
325
- add_property(config, white_property, :white, templates)
330
+ add_property(config, hs_property, :hs, templates: templates)
331
+ add_property(config, rgb_property, :rgb, templates: templates)
332
+ add_property(config, white_property, :white, templates: templates)
326
333
  config[:white_scale] = white_property.range.end if white_property&.range
327
- add_property(config, xy_property, :xy, templates)
334
+ add_property(config, xy_property, :xy, templates: templates)
328
335
  config[:on_command_type] = on_command_type if on_command_type
329
336
 
330
337
  publish(property.mqtt, "light", config, discovery_prefix: discovery_prefix)
@@ -472,16 +479,17 @@ module MQTT
472
479
 
473
480
  private
474
481
 
475
- def add_property(config, property, prefix = nil, templates = {})
482
+ def add_property(config, property, prefix = nil, templates: {}, read_only: false)
476
483
  return unless property
477
484
 
478
485
  prefix = "#{prefix}_" if prefix
479
- config[:"#{prefix}state_topic"] = property.topic if property.retained?
480
- if property.settable?
486
+ state_prefix = "state_" unless read_only
487
+ config[:"#{prefix}#{state_prefix}topic"] = property.topic if property.retained?
488
+ if !read_only && property.settable?
481
489
  config[:"#{prefix}command_topic"] = "#{property.topic}/set"
482
490
  config[:"#{prefix}command_template"] = "{{ value | round(0) }}" if property.datatype == :integer
483
491
  end
484
- config.merge!(templates.slice(:"#{prefix}_template", :"#{prefix}_command_template"))
492
+ config.merge!(templates.slice(:"#{prefix}template", :"#{prefix}command_template"))
485
493
  end
486
494
 
487
495
  def add_enum(config, property, prefix = nil, valid_set = nil)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mqtt-homeassistant
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.1.2
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-12-08 00:00:00.000000000 Z
11
+ date: 2021-12-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: homie-mqtt
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: '1.5'
19
+ version: '1.6'
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
- version: '1.5'
26
+ version: '1.6'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: json
29
29
  requirement: !ruby/object:Gem::Requirement
@@ -116,6 +116,9 @@ extra_rdoc_files: []
116
116
  files:
117
117
  - lib/mqtt-homeassistant.rb
118
118
  - lib/mqtt/home_assistant.rb
119
+ - lib/mqtt/home_assistant/homie/device.rb
120
+ - lib/mqtt/home_assistant/homie/node.rb
121
+ - lib/mqtt/home_assistant/homie/property.rb
119
122
  - lib/mqtt/home_assistant/version.rb
120
123
  homepage: https://github.com/ccutrer/ruby-mqtt-homeassistant
121
124
  licenses: