2023-01-13 19:50:41 +01:00
|
|
|
class Entity < ApplicationRecord
|
2023-01-13 20:47:31 +01:00
|
|
|
belongs_to :member
|
2023-01-13 19:50:41 +01:00
|
|
|
|
2023-01-13 20:47:31 +01:00
|
|
|
validates :name , presence: true
|
|
|
|
validates :ha_id , presence: true
|
|
|
|
validates :member_id , presence: true
|
|
|
|
|
|
|
|
def value
|
|
|
|
@data = Entity.response("/" + ha_id)
|
|
|
|
@data["state"]
|
|
|
|
end
|
|
|
|
|
2023-01-13 21:23:23 +01:00
|
|
|
def type
|
|
|
|
ha_id.split(".").first.capitalize
|
|
|
|
end
|
2023-01-14 17:38:27 +01:00
|
|
|
|
2023-01-13 20:47:31 +01:00
|
|
|
def self.ha_entities
|
|
|
|
res = response()
|
|
|
|
res.collect{|i| i["entity_id"]}.select{|i| i.include?("4a") || i.include?("2d")}
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.response(path = "")
|
2023-01-15 13:52:40 +01:00
|
|
|
host = Rails.application.credentials.weather_dev
|
|
|
|
host = Rails.application.credentials.weather_pro if Rails.env.production?
|
2023-01-14 17:38:27 +01:00
|
|
|
token = Rails.application.credentials.weather_token
|
|
|
|
begin
|
|
|
|
all = RestClient.get( "#{host}:8123/api/states#{path}" ,
|
|
|
|
{"Authorization" => "Bearer #{token}" })
|
|
|
|
res = JSON.parse(all.body)
|
|
|
|
rescue
|
|
|
|
[]
|
|
|
|
end
|
2023-01-13 20:47:31 +01:00
|
|
|
end
|
2023-01-13 19:50:41 +01:00
|
|
|
end
|