mqtt-homeassistant 0.1.6 → 1.0.1

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: c9ce071c9aff4a9278fd43c312610aef5a9ef11e99452823067dbf4cf6980029
4
- data.tar.gz: 47b50acb51c8280f29f80f0c4f918b744ca291b5484af0119d2a4cbed80cf86f
3
+ metadata.gz: b52a4f79380d1f0a0aa808fac2328d00a972dbf9c349372a1cd623d836574d9d
4
+ data.tar.gz: 81747c398e5737eb4e4803a343eeae0b5f293c50a197873dcb436f2b92329eba
5
5
  SHA512:
6
- metadata.gz: a7521c34bad575547c953530311c9111d4d143579139738a158311595b3996b300d22a5ded21d815712bb7bc50916c6b356ad66c3246fcd82d05fdc02ef26ca1
7
- data.tar.gz: c50827bcc8264b573d2f0543f5fb3ef6a94d1b3d04146379251a53681816756e8a731377f98b3f2d8df2f2d42e93cc89ddfa713fd91ca9521711a4c95af69664
6
+ metadata.gz: 1a35ab09e32a71e6fa9253b6413262bd2cebd922facf6c86e732843f3eb35f6cfa420b82d86d363a4a3b29916d9de52bb0e2a1429eed6ee79d5b52a2288800c9
7
+ data.tar.gz: e72ca801a8489241d079380059ad7b6b1868e413652cac2eb21f5c8eeccd6dd94068b098701acafeba8e327130a1c40715bf57578d7cfa3f9206c178d2cae3c9
@@ -0,0 +1,140 @@
1
+ # frozen_string_literal: true
2
+
3
+ module MQTT
4
+ module HomeAssistant
5
+ module Client
6
+ KNOWN_ATTRIBUTES.each_key do |platform|
7
+ class_eval <<~RUBY, __FILE__, __LINE__ + 1
8
+ def publish_hass_#{platform}(object_id, platform: #{platform.inspect}, **kwargs)
9
+ raise ArgumentError, "platform must be #{platform.inspect}" unless platform == #{platform.inspect}
10
+
11
+ publish_hass_component(object_id, platform: platform, **kwargs)
12
+ end
13
+
14
+ def unpublish_hass_#{platform}(object_id, platform: #{platform.inspect}, **kwargs)
15
+ raise ArgumentError, "platform must be #{platform.inspect}" unless platform == #{platform.inspect}
16
+
17
+ unpublish_hass_component(object_id, platform: platform, **kwargs)
18
+ end
19
+ RUBY
20
+ end
21
+
22
+ def unpublish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil)
23
+ node_and_object_id = node_id ? "#{node_id}/#{object_id}" : object_id
24
+ unless KNOWN_ATTRIBUTES.key?(platform)
25
+ raise ArgumentError, "Unknown platform #{platform} for #{node_and_object_id}"
26
+ end
27
+
28
+ publish("#{discovery_prefix || "homeassistant"}/#{platform}/#{node_and_object_id}/config",
29
+ "",
30
+ retain: true,
31
+ qos: 1)
32
+ end
33
+
34
+ def publish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil, **kwargs)
35
+ node_and_object_id = node_id ? "#{node_id}/#{object_id}" : object_id
36
+ unless KNOWN_ATTRIBUTES.key?(platform)
37
+ raise ArgumentError, "Unknown platform #{platform} for #{node_and_object_id}"
38
+ end
39
+
40
+ required_attributes = attributes_for_schema(REQUIRED_ATTRIBUTES, platform, kwargs)
41
+ if required_attributes
42
+ missing_attributes = required_attributes - kwargs.keys
43
+ unless missing_attributes&.empty?
44
+ raise ArgumentError,
45
+ "Missing attribute(s) #{missing_attributes.join(", ")} for #{platform}/#{node_and_object_id}"
46
+ end
47
+ end
48
+
49
+ known_attributes = attributes_for_schema(KNOWN_ATTRIBUTES, platform, kwargs)
50
+ unknown_attributes = kwargs.keys - SPECIAL_ATTRIBUTES[:common] - known_attributes
51
+ unless unknown_attributes.empty?
52
+ raise ArgumentError,
53
+ "Unknown attribute(s) #{unknown_attributes.join(", ")} for #{platform}/#{node_and_object_id}"
54
+ end
55
+
56
+ if (availability_list = kwargs[:availability])
57
+ unless (kwargs.keys & %i[availability_mode
58
+ availability_template
59
+ availability_topic
60
+ payload_available
61
+ payload_not_available]).empty?
62
+ raise ArgumentError,
63
+ "availability cannot be used together with availability topic for #{platform}/#{node_and_object_id}"
64
+ end
65
+
66
+ availability_list = [availability_list] if availability_list.is_a?(Hash)
67
+ unless availability_list.is_a?(Array)
68
+ raise ArgumentError, "availability must be an array for #{platform}/#{node_and_object_id}"
69
+ end
70
+
71
+ availability_list.each do |availability|
72
+ unless availability.key?(:topic)
73
+ raise ArgumentError, "availability must have a topic for #{platform}/#{node_and_object_id}"
74
+ end
75
+
76
+ unless (extra_keys = availability.keys - SPECIAL_ATTRIBUTES[:availability]).empty?
77
+ raise ArgumentError,
78
+ "Unknown attribute(s) #{extra_keys} for #{platform}/#{node_and_object_id}'s availability"
79
+ end
80
+ end
81
+ end
82
+
83
+ if (device = kwargs[:device])
84
+ raise ArgumentError, "device must be a hash for #{platform}/#{node_and_object_id}" unless device.is_a?(Hash)
85
+ unless (extra_keys = device.keys - SPECIAL_ATTRIBUTES[:device]).empty?
86
+ raise ArgumentError, "Unknown attribute(s) #{extra_keys} for #{platform}/#{node_and_object_id}'s device"
87
+ end
88
+ end
89
+
90
+ INCLUSION_VALIDATIONS[:common].merge(INCLUSION_VALIDATIONS[platform] || {}).each do |attr, valid_values|
91
+ if (value = kwargs[attr]) && !valid_values.include?(value)
92
+ raise ArgumentError, "Unrecognized #{attr} #{value} for #{platform}/#{node_and_object_id}"
93
+ end
94
+ end
95
+ SUBSET_VALIDATIONS[platform]&.each do |attr, valid_values|
96
+ if (values = kwargs[attr]) && !(extra_values = values - valid_values).empty?
97
+ raise ArgumentError, "Invalid #{attr} #{extra_values.join(", ")} for #{platform}/#{node_and_object_id}"
98
+ end
99
+ end
100
+
101
+ VALIDATIONS[platform]&.call(**kwargs)
102
+
103
+ RANGE_ATTRIBUTES[platform]&.each do |attr, prefix_or_suffix|
104
+ if (range = kwargs.delete(:"#{attr}_range"))
105
+ case prefix_or_suffix
106
+ when :prefix
107
+ kwargs[:"min_#{attr}"] = range.begin
108
+ kwargs[:"max_#{attr}"] = range.end
109
+ when :suffix
110
+ kwargs[:"#{attr}_min"] = range.begin
111
+ kwargs[:"#{attr}_max"] = range.end
112
+ when :singleton
113
+ kwargs[:min] = range.begin
114
+ kwargs[:max] = range.end
115
+ end
116
+ end
117
+ end
118
+
119
+ publish("#{discovery_prefix || "homeassistant"}/#{platform}/#{node_and_object_id}/config",
120
+ kwargs.to_json,
121
+ retain: true,
122
+ qos: 1)
123
+ end
124
+
125
+ private
126
+
127
+ def attributes_for_schema(hash, platform, kwargs)
128
+ attributes = hash[platform]
129
+ if attributes.is_a?(Hash)
130
+ schema = kwargs[:schema] || :default
131
+ attributes = attributes[schema]
132
+ raise ArgumentError, "Invalid schema #{schema} for platform #{platfomr}" unless attributes
133
+ end
134
+
135
+ attributes
136
+ end
137
+ end
138
+ end
139
+ Client.include(HomeAssistant::Client)
140
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module MQTT
4
4
  module HomeAssistant
5
- VERSION = "0.1.6"
5
+ VERSION = "1.0.1"
6
6
  end
7
7
  end