mqtt-homeassistant 0.1.5 → 1.0.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: 96765202ae955e74f283eb348c22fed1c8bd4cd792aa9792ef071cc5f4bf68c3
4
- data.tar.gz: bfd420db58ba339dd034c177b5352b9dee84063cd2800f7db21fbe5a125ee8ea
3
+ metadata.gz: dd38ce90b005b89ff546d1f7b790b769ab2976e6ad042e808d1f44f4637fa64d
4
+ data.tar.gz: ff5ebc4f121e91e868c9b006709af8cd34754de3bd35bd580dc8492551d5ac2f
5
5
  SHA512:
6
- metadata.gz: bb2f2bd2d70e208ad42a9420723088d16c33cb4607104f7b3edcaaf26a0c7a04211bd2ba04d9c31e26df84cdbd71b726ad1b995b2d34afb580648b6ee540d0fd
7
- data.tar.gz: c1ee571c04ddeea59975ea544dddd1690607ae58fd4bd7c7e825879b8357bcaf405389cdb9c7154e6ce91a4f2a1562742676d5aba5d36acd21bcbbd74f7f5f17
6
+ metadata.gz: 37cc6a6f5b194756a122c7bc82cfbe560a0e7dd41b1c8322810ed6354ea8acdb0f142b6e6547b87578288024a798168a026f8de50f0e6eec76c806b9f8573eb2
7
+ data.tar.gz: ba760f546fcaf9f8e63d49ef0420004581caa6f2e783f9b6258bdb71396dba2fa9ed7ba3680ff005dcfabd26ffba19a9df89961d0e1b2a380d9ba1d2638dcecb
@@ -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.5"
5
+ VERSION = "1.0.0"
6
6
  end
7
7
  end