Module: MQTT::Homie::HomeAssistant::Node

Defined in:
lib/mqtt/homie/home_assistant/node.rb

Instance Method Summary collapse

Instance Method Details

#hass_climate(action_property: nil, current_humidity_property: nil, current_temperature_property: nil, fan_mode_property: nil, mode_property: nil, power_property: nil, preset_mode_property: nil, swing_mode_property: nil, target_humidity_property: nil, temperature_property: nil, temperature_high_property: nil, temperature_low_property: nil, **kwargs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/mqtt/homie/home_assistant/node.rb', line 7

def hass_climate(action_property: nil,
                 current_humidity_property: nil,
                 current_temperature_property: nil,
                 fan_mode_property: nil,
                 mode_property: nil,
                 power_property: nil,
                 preset_mode_property: nil,
                 swing_mode_property: nil,
                 target_humidity_property: nil,
                 temperature_property: nil,
                 temperature_high_property: nil,
                 temperature_low_property: nil,
                 **kwargs)
  if power_property && power_property.datatype != :boolean
    raise ArgumentError, "Power property must be a boolean"
  end

  temperature_property = resolve_property(temperature_property)
  temperature_high_property = resolve_property(temperature_high_property)
  temperature_low_property = resolve_property(temperature_low_property)
  temp_properties = [
    temperature_property,
    temperature_high_property,
    temperature_low_property
  ].compact
  unless (temp_ranges = temp_properties.map(&:range).compact).empty?
    min = temp_ranges.map(&:begin).min
    max = temp_ranges.map(&:end).max
    kwargs[:temp_range] = min..max
  end
  kwargs[:temperature_unit] = temp_properties.map(&:unit).compact.first&.yield_self { |unit| unit[-1] }
  if power_property
    kwargs[:payload_off] = "false"
    kwargs[:payload_on] = "true"
  end

  hass_property(kwargs, action_property, :action, read_only: true)
  hass_property(kwargs, current_humidity_property, :current_humidity, read_only: true)
  hass_property(kwargs, current_temperature_property, :current_temperature, read_only: true)
  hass_enum(kwargs, fan_mode_property, :fan_mode)
  hass_enum(kwargs, mode_property, :mode, MQTT::HomeAssistant::DEFAULTS[:climate][:modes])
  hass_property(kwargs, power_property, :power)
  hass_enum(kwargs, preset_mode_property, :preset_mode)
  hass_enum(kwargs, swing_mode_property, :swing_mode)
  hass_property(kwargs, target_humidity_property, :target_humidity)
  hass_property(kwargs, temperature_property, :temperature)
  hass_property(kwargs, temperature_high_property, :temperature_high)
  hass_property(kwargs, temperature_low_property, :temperature_low)
  publish_hass_component(platform: :climate, **kwargs)
end

#hass_fan(property, oscillation_property: nil, preset_mode_property: nil, **kwargs) ⇒ Object



58
59
60
61
62
63
64
65
66
# File 'lib/mqtt/homie/home_assistant/node.rb', line 58

def hass_fan(property,
             oscillation_property: nil,
             preset_mode_property: nil,
             **kwargs)
  hass_property(kwargs, property)
  hass_property(kwargs, oscillation_property, :oscillation)
  hass_enum(kwargs, preset_mode_property, :preset_mode)
  publish_hass_component(platform: :fan, **kwargs)
end

#hass_humidifier(property, target_humidity_property: nil, mode_property: nil, **kwargs) ⇒ Object



68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/mqtt/homie/home_assistant/node.rb', line 68

def hass_humidifier(property,
                    target_humidity_property: nil,
                    mode_property: nil,
                    **kwargs)
  hass_property(kwargs, property)
  hass_property(kwargs, target_humidity_property, :target_humidity)
  hass_property(kwargs, mode_property, :mode)
  publish_hass_component(platform: :humidifier,
                         payload_off: "false",
                         payload_on: "true",
                         **kwargs)
end

#hass_light(property = nil, brightness_property: nil, color_mode_property: nil, color_temp_property: nil, effect_property: nil, hs_property: nil, rgb_property: nil, white_property: nil, xy_property: nil, **kwargs) ⇒ Object



81
82
83
84
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
# File 'lib/mqtt/homie/home_assistant/node.rb', line 81

def hass_light(property = nil,
               brightness_property: nil,
               color_mode_property: nil,
               color_temp_property: nil,
               effect_property: nil,
               hs_property: nil,
               rgb_property: nil,
               white_property: nil,
               xy_property: nil,
               **kwargs)
  # automatically infer a brightness-only light and adjust config
  if brightness_property && property.nil?
    property = brightness_property
    kwargs[:on_command_type] = :brightness
  end
  case property.datatype
  when :boolean
    kwargs[:payload_off] = "false"
    kwargs[:payload_on] = "true"
  when :integer
    kwargs[:payload_off] = "0"
  when :float
    kwargs[:payload_off] = "0.0"
  end
  kwargs[:brightness_scale] = brightness_property.range.end if brightness_property&.range
  kwargs[:effect_list] = effect_property.range if effect_property&.datatype == :enum
  kwargs[:mireds_range] = color_temp_property.range if color_temp_property.unit == "mired"
  kwargs[:white_scale] = white_property.range.end if white_property&.range

  hass_property(kwargs, property)
  hass_property(kwargs, brightness_property, :brightness)
  hass_property(kwargs, color_mode_property, :color_mode)
  hass_property(kwargs, color_temp_property, :color_temp)
  hass_property(kwargs, effect_property, :effect)
  hass_property(kwargs, hs_property, :hs)
  hass_property(kwargs, rgb_property, :rgb)
  hass_property(kwargs, white_property, :white)
  hass_property(kwargs, xy_property, :xy)
  publish_hass_component(platform: :light, **kwargs)
end

#hass_water_heater(current_temperature_property: nil, mode_property: nil, power_property: nil, temperature_property: nil, **kwargs) ⇒ Object



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
# File 'lib/mqtt/homie/home_assistant/node.rb', line 122

def hass_water_heater(
  current_temperature_property: nil,
  mode_property: nil,
  power_property: nil,
  temperature_property: nil,
  **kwargs
)
  temperature_property = resolve_property(temperature_property)
  current_temperature_property = resolve_property(current_temperature_property)
  temp_properties = [
    temperature_property,
    current_temperature_property
  ].compact
  kwargs[:range] = temperature_property&.range
  kwargs[:temperature_unit] = temp_properties.map(&:unit).compact.first&.yield_self { |unit| unit[-1] }
  if power_property
    kwargs[:payload_off] = "false"
    kwargs[:payload_on] = "true"
  end
  hass_property(kwargs, current_temperature_property, :current_temperature, read_only: true)
  hass_enum(kwargs, mode_property, :mode, MQTT::HomeAssistant::DEFAULTS[:water_heater][:modes])
  hass_property(kwargs, power_property, :power)
  hass_property(kwargs, temperature_property, :temperature)
  publish_hass_component(platform: :water_heater, **kwargs)
end

#publishObject



148
149
150
151
152
153
154
155
# File 'lib/mqtt/homie/home_assistant/node.rb', line 148

def publish
  super.tap do
    @pending_hass_registrations&.each do |(object_id, kwargs)|
      device.mqtt.publish_hass_component(object_id, **kwargs)
    end
    @pending_hass_registrations = nil
  end
end