karlentwistle/ruby_home

View on GitHub
lib/ruby_home/accessory_info.rb

Summary

Maintainability
A
0 mins
Test Coverage
require_relative "persistable"

module RubyHome
  class AccessoryInfo
    include Persistable

    self.source = "accessory_info.yml"

    def self.instance
      @@_instance ||= persisted || create
    end

    def self.reload
      @@_instance = nil
    end

    USERNAME = -"Pair-Setup"

    def initialize(device_id: nil, paired_clients: [], password: nil, signature_key: nil)
      @device_id = device_id
      @paired_clients = paired_clients
      @password = password
      @signature_key = signature_key
    end

    def username
      USERNAME
    end

    def password
      @password ||= Password.generate
    end

    def password=(new_password)
      @password = new_password
      save
    end

    def device_id
      @device_id ||= DeviceID.generate
    end

    def paired_clients
      @paired_clients ||= []
    end

    def add_paired_client(identifier:, public_key:, admin: false)
      @paired_clients << {admin: admin, identifier: identifier, public_key: public_key}
      save
    end

    def remove_paired_client(identifier)
      paired_clients.delete_if { |h| h[:identifier] == identifier }
      save
    end

    def paired?
      paired_clients.any?
    end

    def signing_key
      @signing_key ||= RbNaCl::Signatures::Ed25519::SigningKey.new([signature_key].pack("H*"))
    end

    private

    def signature_key
      @signature_key ||= RbNaCl::Signatures::Ed25519::SigningKey.generate.to_bytes.unpack1("H*")
    end

    def persisted_attributes
      {
        device_id: device_id,
        paired_clients: paired_clients,
        password: password,
        signature_key: signature_key
      }
    end
  end
end