Module: MQTT::HomeAssistant::Client

Defined in:
lib/mqtt/home_assistant/client.rb

Instance Method Summary collapse

Instance Method Details

#publish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/mqtt/home_assistant/client.rb', line 85

def publish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil, **kwargs)
  raise ArgumentError, "Use `publish_hass_device` for device discovery" if platform == :device

  node_and_object_id = node_id ? "#{node_id}/#{object_id}" : object_id

  validate_hass_common(kwargs, platform, node_and_object_id)

  unless defined?(::HomeAssistant::MQTT::Discovery)
    unless KNOWN_ATTRIBUTES.key?(platform)
      raise ArgumentError, "Unknown platform #{platform} for #{node_and_object_id}"
    end

    required_attributes = attributes_for_schema(REQUIRED_ATTRIBUTES, platform, kwargs)
    required_attributes += [:unique_id] if @collected_hass_components && ENTITY_PLATFORMS.include?(platform)
    missing_attributes = required_attributes - kwargs.keys
    unless missing_attributes.empty?
      raise ArgumentError,
            "Missing attribute(s) #{missing_attributes.join(", ")} for #{platform}/#{node_and_object_id}"
    end

    known_attributes = attributes_for_schema(KNOWN_ATTRIBUTES, platform, kwargs)
    unknown_attributes = kwargs.keys - SPECIAL_ATTRIBUTES[:common] - known_attributes
    unless unknown_attributes.empty?
      raise ArgumentError,
            "Unknown attribute(s) #{unknown_attributes.join(", ")} for #{platform}/#{node_and_object_id}"
    end

    if @collected_hass_components &&
       !(extra_keys = DISALLOWED_COMPONENT_ATTRIBUTES_WHEN_DEVICE & kwargs.keys).empty?
      raise ArgumentError, "Unknown attribute(s) #{extra_keys} for #{platform}/#{node_and_object_id}"
    end

    INCLUSION_VALIDATIONS[:common].merge(INCLUSION_VALIDATIONS[platform] || {}).each do |attr, valid_values|
      if (value = kwargs[attr]) && !valid_values.include?(value)
        raise ArgumentError, "Unrecognized #{attr} #{value} for #{platform}/#{node_and_object_id}"
      end
    end
    SUBSET_VALIDATIONS[platform]&.each do |attr, valid_values|
      if (values = kwargs[attr]) && !(extra_values = values - valid_values).empty?
        raise ArgumentError, "Invalid #{attr} #{extra_values.join(", ")} for #{platform}/#{node_and_object_id}"
      end
    end

    VALIDATIONS[platform]&.call(**kwargs)
  end

  RANGE_ATTRIBUTES[platform]&.each do |attr, prefix_or_suffix|
    range_name = (prefix_or_suffix == :singleton) ? attr : :"#{attr}_range"
    next unless (range = kwargs.delete(range_name))

    case prefix_or_suffix
    when :prefix
      kwargs[:"min_#{attr}"] = range.begin
      kwargs[:"max_#{attr}"] = range.end
    when :suffix
      kwargs[:"#{attr}_min"] = range.begin
      kwargs[:"#{attr}_max"] = range.end
    when :singleton
      kwargs[:min] = range.begin
      kwargs[:max] = range.end
    end
  end

  hass_abbreviate(ABBREVIATIONS, kwargs)
  if @collected_hass_components
    kwargs[:p] = platform
    @collected_hass_components[object_id] = kwargs
  else
    hass_publish(discovery_prefix, platform, node_and_object_id, kwargs.to_json)
  end
end

#publish_hass_device(device_id, discovery_prefix: "homeassistant", migrate_discovery: false, **kwargs) ⇒ Object

Raises:

  • (ArgumentError)


44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/mqtt/home_assistant/client.rb', line 44

def publish_hass_device(device_id, discovery_prefix: "homeassistant", migrate_discovery: false, **kwargs)
  raise ArgumentError, "components cannot be passed as an argument" if kwargs[:components]

  validate_hass_common(kwargs, :device, device_id)

  @collected_hass_components = {}
  begin
    yield
  ensure
    kwargs[:components] = @collected_hass_components
    @collected_hass_components = nil
  end

  unless defined?(::HomeAssistant::MQTT::Discovery)
    missing_attributes = REQUIRED_ATTRIBUTES[:device] - kwargs.keys
    unless missing_attributes.empty?
      raise ArgumentError, "Missing attribute(s) #{missing_attributes.join(", ")} for device/#{device_id}"
    end
  end

  hass_abbreviate(ABBREVIATIONS, kwargs)

  if migrate_discovery
    kwargs[:cmps].each do |object_id, component|
      hass_publish(discovery_prefix,
                   component[:p],
                   "#{device_id}/#{object_id}",
                   MIGRATE_DISCOVERY_JSON,
                   validate: false)
    end
  end

  hass_publish(discovery_prefix, :device, device_id, kwargs.to_json)

  return unless migrate_discovery

  kwargs[:cmps].each do |object_id, component|
    hass_publish(discovery_prefix, component[:p], "#{device_id}/#{object_id}", "", validate: false)
  end
end

#unpublish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mqtt/home_assistant/client.rb', line 32

def unpublish_hass_component(object_id, platform:, discovery_prefix: "homeassistant", node_id: nil)
  node_and_object_id = node_id ? "#{node_id}/#{object_id}" : object_id
  unless KNOWN_ATTRIBUTES.key?(platform)
    raise ArgumentError, "Unknown platform #{platform} for #{node_and_object_id}"
  end

  publish("#{discovery_prefix || "homeassistant"}/#{platform}/#{node_and_object_id}/config",
          "",
          retain: true,
          qos: 1)
end