Module: MQTT::Homie::HomeAssistant::Property

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

Instance Method Summary collapse

Instance Method Details

#hass_binary_sensor(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


24
25
26
27
28
29
30
31
32
33
# File 'lib/mqtt/homie/home_assistant/property.rb', line 24

def hass_binary_sensor(**kwargs)
  raise ArgumentError, "Property must be a boolean" unless datatype == :boolean
  raise ArgumentError, "Property must not be settable" if settable?

  hass_property(kwargs)
  publish_hass_component(platform: :binary_sensor,
                         payload_off: "false",
                         payload_on: "true",
                         **kwargs)
end

#hass_button(**kwargs) ⇒ Object



35
36
37
38
# File 'lib/mqtt/homie/home_assistant/property.rb', line 35

def hass_button(**kwargs)
  hass_property(kwargs)
  publish_hass_component(platform: :button, **kwargs)
end

#hass_fan(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


40
41
42
43
44
45
46
47
48
49
# File 'lib/mqtt/homie/home_assistant/property.rb', line 40

def hass_fan(**kwargs)
  raise ArgumentError, "Property must be a boolean" unless datatype == :boolean
  raise ArgumentError, "Property must be settable" unless settable?

  hass_property(kwargs)
  publish_hass_component(platform: :fan,
                         payload_off: "false",
                         payload_on: "true",
                         **kwargs)
end

#hass_light(**kwargs) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/mqtt/homie/home_assistant/property.rb', line 51

def hass_light(**kwargs)
  case 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

  hass_property(kwargs)
  publish_hass_component(platform: :light, **kwargs)
end

#hass_number(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


66
67
68
69
70
71
72
73
74
# File 'lib/mqtt/homie/home_assistant/property.rb', line 66

def hass_number(**kwargs)
  raise ArgumentError, "Property must be an integer or a float" unless %i[integer float].include?(datatype)

  hass_property(kwargs)
  kwargs[:range] = range if range
  kwargs[:unit_of_measurement] = unit if unit

  publish_hass_component(platform: :number, **kwargs)
end

#hass_scene(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


76
77
78
79
80
81
82
83
84
85
86
# File 'lib/mqtt/homie/home_assistant/property.rb', line 76

def hass_scene(**kwargs)
  unless datatype == :enum && range.length == 1
    raise ArgumentError, "Property must be an enum with a single value"
  end
  raise ArgumentError, "Property must be settable" unless settable?

  publish_hass_component(platform: :scene,
                         command_topic: "#{topic}/set",
                         payload_on: range.first,
                         **kwargs)
end

#hass_select(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


88
89
90
91
92
93
94
# File 'lib/mqtt/homie/home_assistant/property.rb', line 88

def hass_select(**kwargs)
  raise ArgumentError, "Property must be an enum" unless datatype == :enum
  raise ArgumentError, "Property must be settable" unless settable?

  hass_property(kwargs)
  publish_hass_component(platform: :select, options: range, **kwargs)
end

#hass_sensor(**kwargs) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
# File 'lib/mqtt/homie/home_assistant/property.rb', line 96

def hass_sensor(**kwargs)
  if datatype == :enum
    kwargs[:device_class] = :enum
    kwargs[:options] = range
  end
  kwargs[:unit_of_measurement] = unit if unit

  publish_hass_component(platform: :sensor,
                         state_topic: topic,
                         **kwargs)
end

#hass_switch(**kwargs) ⇒ Object

Raises:

  • (ArgumentError)


108
109
110
111
112
113
114
115
116
# File 'lib/mqtt/homie/home_assistant/property.rb', line 108

def hass_switch(**kwargs)
  raise ArgumentError, "Property must be a boolean" unless datatype == :boolean

  hass_property(kwargs)
  publish_hass_component(platform: :switch,
                         payload_off: "false",
                         payload_on: "true",
                         **kwargs)
end

#initialize(*args, hass: nil, **kwargs) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/mqtt/homie/home_assistant/property.rb', line 7

def initialize(*args, hass: nil, **kwargs)
  super(*args, **kwargs)

  return unless hass

  case hass
  when Symbol
    public_send("hass_#{hass}")
  when Hash
    raise ArgumentError, "hass must only contain one item" unless hass.length == 1

    public_send("hass_#{hass.first.first}", **hass.first.last)
  else
    raise ArgumentError, "hass must be a Symbol or a Hash of HASS device type to additional HASS options"
  end
end

#publishObject



118
119
120
121
122
123
124
125
# File 'lib/mqtt/homie/home_assistant/property.rb', line 118

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