Class: ESPHome::Dashboard

Inherits:
Object
  • Object
show all
Defined in:
lib/esphome/dashboard.rb

Constant Summary collapse

DEFAULT_URI =
"http://localhost:6052/"

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Dashboard

Returns a new instance of Dashboard.



24
25
26
27
# File 'lib/esphome/dashboard.rb', line 24

def initialize(uri)
  @http = HTTPX.plugin(:persistent).with(origin: uri)
  @websocket = nil
end

Class Method Details

.address_and_encryption_key(uri, device_name) ⇒ Object

Raises:



11
12
13
14
15
16
17
18
19
20
21
# File 'lib/esphome/dashboard.rb', line 11

def address_and_encryption_key(uri, device_name)
  dashboard = uri.is_a?(Dashboard) ? uri : new(uri)
  device = dashboard.devices.find { |d| d["name"] == device_name }
  raise NoSuchDeviceError, "Device not found: #{device_name}" unless device

  address = device["address"]
  encryption_key = dashboard.encryption_key(device["configuration"])
  raise MissingEncryptionKeyError, "No encryption key found for device #{device_name}" unless encryption_key

  [address, encryption_key]
end

Instance Method Details

#compile(configuration) ⇒ Object



43
44
45
# File 'lib/esphome/dashboard.rb', line 43

def compile(configuration, &)
  websocket_command("/compile", { type: "spawn", configuration: }, &)
end

#config(configuration) ⇒ Object



33
34
35
36
37
# File 'lib/esphome/dashboard.rb', line 33

def config(configuration)
  @http.get("/json-config", params: { configuration: })
       .tap(&:raise_for_status)
       .json(allow_nan: true)
end

#devicesObject



29
30
31
# File 'lib/esphome/dashboard.rb', line 29

def devices
  @http.get("/devices").tap(&:raise_for_status).json["configured"]
end

#encryption_key(configuration) ⇒ Object



39
40
41
# File 'lib/esphome/dashboard.rb', line 39

def encryption_key(configuration)
  config(configuration).dig("api", "encryption", "key")
end

#update(configuration, port: :ota) ⇒ Object



52
53
54
# File 'lib/esphome/dashboard.rb', line 52

def update(configuration, port: :ota, &)
  compile(configuration, &) && upload(configuration, port:, &)
end

#upload(configuration, port: :ota) ⇒ Object



47
48
49
50
# File 'lib/esphome/dashboard.rb', line 47

def upload(configuration, port: :ota, &)
  port = "OTA" if port == :ota
  websocket_command("/upload", { type: "spawn", configuration:, port: }, &)
end