hubfeenix.fi/app/models/entity.rb
2023-01-15 14:52:40 +02:00

35 lines
865 B
Ruby

class Entity < ApplicationRecord
belongs_to :member
validates :name , presence: true
validates :ha_id , presence: true
validates :member_id , presence: true
def value
@data = Entity.response("/" + ha_id)
@data["state"]
end
def type
ha_id.split(".").first.capitalize
end
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 = "")
host = Rails.application.credentials.weather_dev
host = Rails.application.credentials.weather_pro if Rails.env.production?
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
end
end