mqtt-homeassistant 0.0.2 → 0.1.0

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: e2701bd3fb3bde99e200213253949dbe73787ada219260463309ea1f27dcd9af
4
- data.tar.gz: 3c58be7186fb3aa443e6d1ff33b5282549988422536daf34c430415effece799
3
+ metadata.gz: 66adc7f6c74477f3b343bdfe1decae41a5a1de06fffd276c3c9cf7bb0bb137c6
4
+ data.tar.gz: c422275a7683ea94d7f34dd2b880c79901a9f70b15a30c1f928ed548799e3991
5
5
  SHA512:
6
- metadata.gz: 2257944fc2298287e7c9cd6ffa8b2afd4edd7c22c7a47165817cd8b7ee32084e4e11b7fd30fc794805472cb06c9f608d4d4c587b7685ebdb2e4f1df20ca4777f
7
- data.tar.gz: 60a670f138fe6378810cc5b86291a99d92084dfaf509769c67f515132f51ad336fb421cae45f8192113e5e1a01a48cc98fad110218a73071b4c78a3f59f553f8
6
+ metadata.gz: c41a65d50cb2287f87c7ca9a8c7135596a192c3a81ec5d6d467e511d58de7219028c6af335b8b93dc38f0c4a1c358d070e9695fc1f431c33a73f148cb2c86516
7
+ data.tar.gz: 801b997f76c306396bec2720465f264de2ce79a7ebef1a97d5f27d99e4d35150944e4e6ecf05c65ed524c0bbea70e6ff5ca7ce693304118f472a158c9711bb8c
@@ -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.2"
5
+ VERSION = "0.1.0"
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
@@ -211,13 +216,13 @@ module MQTT
211
216
  entity_category: entity_category,
212
217
  icon: icon,
213
218
  templates: {})
214
- add_property(config, oscillation_property, :oscillation_property, templates)
215
- 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)
216
221
  if percentage_property&.range
217
222
  config[:speed_range_min] = percentage_property.range.begin
218
223
  config[:speed_range_max] = percentage_property.range.end
219
224
  end
220
- add_property(config, preset_mode_property, :preset, templates)
225
+ add_property(config, preset_mode_property, :preset, templates: templates)
221
226
  add_enum(config, preset_mode_property, :preset)
222
227
 
223
228
  publish(node.mqtt, "fan", config, discovery_prefix: discovery_prefix)
@@ -301,6 +306,7 @@ module MQTT
301
306
  device: device,
302
307
  entity_category: entity_category,
303
308
  icon: icon)
309
+ config[:unique_id] = "#{property.device.id}_#{property.node.id}_#{property.id}"
304
310
  add_property(config, property)
305
311
  case property.datatype
306
312
  when :boolean
@@ -311,21 +317,21 @@ module MQTT
311
317
  when :float
312
318
  config[:payload_off] = "0.0"
313
319
  end
314
- add_property(config, brightness_property, :brightness, templates)
320
+ add_property(config, brightness_property, :brightness, templates: templates)
315
321
  config[:brightness_scale] = brightness_property.range.end if brightness_property&.range
316
- add_property(config, color_mode_property, :color_mode, templates)
317
- 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)
318
324
  if color_temp_property&.range && color_temp_property.unit == "mired"
319
325
  config[:min_mireds] = color_temp_property.range.begin
320
326
  config[:max_mireds] = color_temp_property.range.end
321
327
  end
322
- add_property(config, effect_property, :effect, templates)
328
+ add_property(config, effect_property, :effect, templates: templates)
323
329
  config[:effect_list] = effect_property.range if effect_property&.datatype == :enum
324
- add_property(config, hs_property, :hs, templates)
325
- add_property(config, rgb_property, :rgb, templates)
326
- 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)
327
333
  config[:white_scale] = white_property.range.end if white_property&.range
328
- add_property(config, xy_property, :xy, templates)
334
+ add_property(config, xy_property, :xy, templates: templates)
329
335
  config[:on_command_type] = on_command_type if on_command_type
330
336
 
331
337
  publish(property.mqtt, "light", config, discovery_prefix: discovery_prefix)
@@ -473,7 +479,7 @@ module MQTT
473
479
 
474
480
  private
475
481
 
476
- def add_property(config, property, prefix = nil, templates = {}, read_only: false)
482
+ def add_property(config, property, prefix = nil, templates: {}, read_only: false)
477
483
  return unless property
478
484
 
479
485
  prefix = "#{prefix}_" if prefix
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.2
4
+ version: 0.1.0
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-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: homie-mqtt
@@ -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: