hubfeenix.fi/app/models/entity.rb

36 lines
900 B
Ruby
Raw Normal View History

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)
2023-01-16 21:13:31 +01:00
return "" if @data.is_a? Array
2023-01-13 20:47:31 +01:00
@data["state"]
end
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