Class: HomeAssistant::Ble::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/home_assistant/ble.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ Detector



15
16
17
18
# File 'lib/home_assistant/ble.rb', line 15

def initialize(config)
  @config = Mash.new(config)
  @known_devices = {}
end

Instance Attribute Details

#configObject (readonly)

Returns the value of attribute config.



13
14
15
# File 'lib/home_assistant/ble.rb', line 13

def config
  @config
end

#known_devicesObject (readonly)

Returns the value of attribute known_devices.



13
14
15
# File 'lib/home_assistant/ble.rb', line 13

def known_devices
  @known_devices
end

Instance Method Details

#clean_all_devicesObject



78
79
80
81
82
83
84
# File 'lib/home_assistant/ble.rb', line 78

def clean_all_devices
  known_devices.keys.each do |name|
    known_devices.delete(name).tap do
      home_assistant_devices[name] && update_home_assistant(home_assistant_devices[name], :not_home)
    end
  end
end

#discovery_timeObject



20
21
22
# File 'lib/home_assistant/ble.rb', line 20

def discovery_time
  config['discovery_time'] || 60
end

#grace_periodObject

time after which a device is considered as disappeared



30
31
32
# File 'lib/home_assistant/ble.rb', line 30

def grace_period
  config['grace_period'] || 60
end

#home_assistant_devicesObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/home_assistant/ble.rb', line 46

def home_assistant_devices
  devices = {}
  if  config['home_assistant_devices_file']
    YAML.load_file(config['home_assistant_devices_file']).each do |name, conf|
      next unless conf['mac'] =~ /^(ble_|bt_)/i
      next unless conf['track']
      mac = conf['mac'].gsub(/^(ble_|bt_)/i, '').upcase
      conf['dev_id'] = name
      devices[mac] = conf.select { |k, _v| %w(dev_id mac).include? k }
      debug "Adding #{mac} (#{conf['name']}) found in known_devices.yaml"
    end
  end
  if config['home_assistant_devices']
    config['home_assistant_devices'].each do |mac, _name|
      ble_mac = "BLE_#{mac.upcase}" unless mac =~ /^(ble_|bt_)/i
      devices[mac] = Mash.new(mac: ble_mac)
    end
  end

  devices
end

#home_assistant_passwordObject



38
39
40
# File 'lib/home_assistant/ble.rb', line 38

def home_assistant_password
  config['home_assistant_password']
end

#home_assistant_tokenObject



42
43
44
# File 'lib/home_assistant/ble.rb', line 42

def home_assistant_token
  config['home_assistant_token']
end

#home_assistant_urlObject



34
35
36
# File 'lib/home_assistant/ble.rb', line 34

def home_assistant_url
  config['home_assistant_url'] || 'http://localhost:8123'
end

#intervalObject

polling interval



25
26
27
# File 'lib/home_assistant/ble.rb', line 25

def interval
  config['interval'] || 30
end

#runObject



68
69
70
71
72
73
74
75
76
# File 'lib/home_assistant/ble.rb', line 68

def run
  loop do
    discover
    detect_new_devices
    clean_devices
    debug "Will sleep #{interval}s before relisting devices"
    sleep interval
  end
end