Module: Fritzbox::Smarthome::Properties::SimpleOnOff

Extended by:
ActiveSupport::Concern
Included in:
Lightbulb, Switch
Defined in:
lib/fritzbox/smarthome/properties/simple_on_off.rb

Overview

Defines a common interface/behaviour for actors with the “simpleonoff” state. The including class is expected to have an ‘ain` attribute defined.

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


24
25
26
# File 'lib/fritzbox/smarthome/properties/simple_on_off.rb', line 24

def active?
  simpleonoff_state == 1
end

#toggle!false, Integer

Makes a request to the Fritzbox and set the current instance’s active state.

The instance state is kept in memory and not checked with the Fritzbox state. It is possible that the device is switched on/off through other means.

Examples:

lightbulb.active?
# => true
lightbulb.toggle!
# => 0
lightbulb.active?
# => false

Returns:

  • (false, Integer)

    Returns the new on/off state or false when the request was unsuccessful

Raises:

  • (ArgumentError)

    if the including class does not respond to ‘#ain`



43
44
45
46
47
48
49
# File 'lib/fritzbox/smarthome/properties/simple_on_off.rb', line 43

def toggle!
  raise ArgumentError, "Attribute `ain` is missing on #{inspect}" unless respond_to?(:ain)
  value = active? ? 0 : 1
  response = Fritzbox::Smarthome::Resource.get(command: 'setsimpleonoff', ain: ain, onoff: value)

  response.ok? && self.simpleonoff_state = value
end