Class: ESPHome::Device
- Inherits:
-
Object
- Object
- ESPHome::Device
- Defined in:
- lib/esphome/device.rb
Instance Attribute Summary collapse
-
#address ⇒ Object
readonly
Returns the value of attribute address.
-
#compilation_time ⇒ Object
readonly
Returns the value of attribute compilation_time.
-
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
-
#connection_logger ⇒ Object
Returns the value of attribute connection_logger.
-
#device_logger ⇒ Object
Returns the value of attribute device_logger.
-
#esphome_version ⇒ Object
readonly
Returns the value of attribute esphome_version.
-
#friendly_name ⇒ Object
readonly
Returns the value of attribute friendly_name.
-
#mac_address ⇒ Object
readonly
Returns the value of attribute mac_address.
-
#manufacturer ⇒ Object
readonly
Returns the value of attribute manufacturer.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
-
#project_name ⇒ Object
readonly
Returns the value of attribute project_name.
-
#project_version ⇒ Object
readonly
Returns the value of attribute project_version.
-
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
-
#serial_proxies ⇒ Object
readonly
Returns the value of attribute serial_proxies.
-
#suggested_area ⇒ Object
readonly
Returns the value of attribute suggested_area.
Instance Method Summary collapse
- #connect ⇒ Object
- #connected? ⇒ Boolean
- #disconnect ⇒ Object
- #entities ⇒ Object
-
#initialize(address, encryption_key, port: 6053) ⇒ Device
constructor
A new instance of Device.
- #loop ⇒ Object
- #send(message) ⇒ Object
- #stream_actions ⇒ Object
- #stream_log(level = :very_verbose, dump_config: false) ⇒ Object
- #stream_states ⇒ Object
Constructor Details
#initialize(address, encryption_key, port: 6053) ⇒ Device
Returns a new instance of Device.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/esphome/device.rb', line 56 def initialize(address, encryption_key, port: 6053) @address = address @encryption_key = encryption_key.unpack1("m0") @port = port @socket = nil @connection_logger = nil @device_logger = nil @noise = nil @on_connect_callback = [] @on_disconnect_callback = [] @on_message_callback = [] @entities = nil @connect_timeout = 5 @read_timeout = 5 @messages_to_replay = [] @serial_proxies = {}.freeze end |
Instance Attribute Details
#address ⇒ Object (readonly)
Returns the value of attribute address.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def address @address end |
#compilation_time ⇒ Object (readonly)
Returns the value of attribute compilation_time.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def compilation_time @compilation_time end |
#connect_timeout ⇒ Object
Returns the value of attribute connect_timeout.
54 55 56 |
# File 'lib/esphome/device.rb', line 54 def connect_timeout @connect_timeout end |
#connection_logger ⇒ Object
Returns the value of attribute connection_logger.
54 55 56 |
# File 'lib/esphome/device.rb', line 54 def connection_logger @connection_logger end |
#device_logger ⇒ Object
Returns the value of attribute device_logger.
54 55 56 |
# File 'lib/esphome/device.rb', line 54 def device_logger @device_logger end |
#esphome_version ⇒ Object (readonly)
Returns the value of attribute esphome_version.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def esphome_version @esphome_version end |
#friendly_name ⇒ Object (readonly)
Returns the value of attribute friendly_name.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def friendly_name @friendly_name end |
#mac_address ⇒ Object (readonly)
Returns the value of attribute mac_address.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def mac_address @mac_address end |
#manufacturer ⇒ Object (readonly)
Returns the value of attribute manufacturer.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def manufacturer @manufacturer end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def model @model end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def name @name end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def port @port end |
#project_name ⇒ Object (readonly)
Returns the value of attribute project_name.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def project_name @project_name end |
#project_version ⇒ Object (readonly)
Returns the value of attribute project_version.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def project_version @project_version end |
#read_timeout ⇒ Object
Returns the value of attribute read_timeout.
54 55 56 |
# File 'lib/esphome/device.rb', line 54 def read_timeout @read_timeout end |
#serial_proxies ⇒ Object (readonly)
Returns the value of attribute serial_proxies.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def serial_proxies @serial_proxies end |
#suggested_area ⇒ Object (readonly)
Returns the value of attribute suggested_area.
41 42 43 |
# File 'lib/esphome/device.rb', line 41 def suggested_area @suggested_area end |
Instance Method Details
#connect ⇒ Object
74 75 76 77 78 79 80 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 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 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 |
# File 'lib/esphome/device.rb', line 74 def connect return if @socket addresses = resolve exception = nil addresses.each do |addrinfo| @socket = addrinfo.connect rescue => e exception = e end if exception raise DeviceConnectionError, "Unable to connect to #{Array(address).join(", ")}:#{port}: #{exception.}" end begin # Noise logs warnings about not being able to load algorithms we don't even care about. old_level = Noise.logger.level Noise.logger.level = Logger::ERROR noise = Noise::Connection::Initiator.new(NOISE_PROTOCOL) ensure Noise.logger.level = old_level end noise.psks = [@encryption_key] noise.prologue = NOISE_PROLOGUE noise.start_handshake write_frame("") write_frame("\0#{noise.}") _device_id = read_frame noise.(read_frame[1..]) @noise = noise send(Api::HelloRequest.new(client_info: "esphome-ruby", api_version_major: API_VERSION_MAJOR, api_version_minor: API_VERSION_MINOR)) do || if .is_a?(Api::AuthenticationResponse) raise InvalidPasswordError, "Invalid password" if .invalid_password next true end next false unless .is_a?(Api::HelloResponse) break end send(Api::DeviceInfoRequest.new) do || if .is_a?(Api::AuthenticationResponse) raise InvalidPasswordError, "Invalid password" if .invalid_password next true end next false unless .is_a?(Api::DeviceInfoResponse) @name = .name @mac_address = .mac_address @esphome_version = .esphome_version @compilation_time = .compilation_time @model = .model @project_name = .project_name @project_version = .project_version @manufacturer = .manufacturer @friendly_name = .friendly_name @suggested_area = .suggested_area old_proxies = @serial_proxies @serial_proxies = {} .serial_proxies.each_with_index do |proxy, instance| name = proxy.name.freeze # keep the same object post-reconnect proxy = old_proxies[name] if old_proxies[name]&.instance == instance proxy = SerialProxy.new(self, name, instance, normalize_serial_proxy_port_type(proxy.port_type)) @serial_proxies[name] = proxy @serial_proxies[proxy.instance] = proxy end @serial_proxies.freeze missing_proxies = old_proxies.values.uniq - @serial_proxies.values missing_proxies.each do |proxy| proxy.instance_variable_set(:@closed, true) proxy.instance_variable_set(:@device, nil) end @entities = nil @on_connect_callback.each(&:call) break end end |
#connected? ⇒ Boolean
183 184 185 |
# File 'lib/esphome/device.rb', line 183 def connected? !!(@socket && @noise) end |
#disconnect ⇒ Object
173 174 175 176 177 178 179 180 181 |
# File 'lib/esphome/device.rb', line 173 def disconnect return unless connected? send(Api::DisconnectRequest.new) rescue DeviceConnectionError, NotConnectedError, IOError, SocketError, SystemCallError nil ensure disconnected end |
#entities ⇒ Object
187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/esphome/device.rb', line 187 def entities return @entities if @entities send(Api::ListEntitiesRequest.new) @entities = {} do || break if .is_a?(Api::ListEntitiesDoneResponse) next false unless .class.respond_to?(:entity_class) entity_class = .class.entity_class @entities[.key] = entity_class.new(self, ) true end @entities.freeze end |
#loop ⇒ Object
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
# File 'lib/esphome/device.rb', line 219 def loop Kernel.loop do = nil begin = if @messages_to_replay.empty? else @messages_to_replay.shift end rescue Timeout::Error send(Api::PingRequest.new) if connected? next end if .is_a?(Api::PingRequest) send(Api::PingResponse.new) next end if .respond_to?(:key) && (entity = @entities[.key]) entity.update() @on_message_callback.each { |callback| callback.call(entity) } elsif .is_a?(Api::SubscribeLogsResponse) device_logger&.log(LOG_LEVEL_MAP[.level], .) elsif .is_a?(Api::HomeassistantActionRequest) action = Action.from_protobuf() @on_message_callback.each { |callback| callback.call(action) } elsif .is_a?(Api::DisconnectRequest) send(Api::DisconnectResponse.new) disconnected @on_disconnect_callback.each(&:call) elsif .is_a?(Api::DisconnectResponse) disconnected elsif .is_a?(Api::PingResponse) # Nothing to do else @on_message_callback.each { |callback| callback.call() } end end rescue Interrupt nil end |
#send(message) ⇒ Object
276 277 278 279 280 281 282 283 284 285 286 |
# File 'lib/esphome/device.rb', line 276 def send() raise NotConnectedError, "Encryption not yet set up" unless connected? connection_logger&.debug { "> #{.inspect}" } = .to_proto = [.class.descriptor.id, .length, ].pack("nnA*") = @noise.encrypt() write_frame() end |
#stream_actions ⇒ Object
215 216 217 |
# File 'lib/esphome/device.rb', line 215 def stream_actions send(Api::SubscribeHomeassistantServicesRequest.new) end |
#stream_log(level = :very_verbose, dump_config: false) ⇒ Object
209 210 211 212 213 |
# File 'lib/esphome/device.rb', line 209 def stream_log(level = :very_verbose, dump_config: false) request = Api::SubscribeLogsRequest.new(level: :"LOG_LEVEL_#{level.upcase}") request.dump_config = dump_config if dump_config send(request) end |
#stream_states ⇒ Object
205 206 207 |
# File 'lib/esphome/device.rb', line 205 def stream_states send(Api::SubscribeStatesRequest.new) end |