Module plugins.gpio.BinaryAction

Expand source code
from enum import Enum
from modules.base.Configuration import *
from modules.base.Instances import *
from plugins.gpio.Platform import Platform

@configuration
class BinaryActionConfiguration(ActionConfiguration):
    '''Configuration settings for a GPIO output'''

    pin: str
    '''GPIO PIN name. e.g. GPIO22'''

    on_high: list[AutomationConfiguration] = []
    '''List of Automations to execute when the GPIO is triggered to high, see `modules.base.Configuration.AutomationConfiguration`'''

    on_low: list[AutomationConfiguration] = []
    '''List of Automations to execute when the GPIO is triggered to low, see `modules.base.Configuration.AutomationConfiguration`'''

    active_high: bool = True
    '''"True=normal, False=inverted'''

    initial_value: bool = False
    '''expected initial value'''

    @validator('platform')
    def check_platform(cls, v):
        platform_name = "gpio"
        if v != platform_name:
            raise ValueError("wrong script platform: " + platform_name + ", is: " + v)
        return v    

class BinaryActionState(BaseState):
    '''Represents the state of the GPIO pin'''

    is_high = False
    '''Returns if the LED is HIGH(True) or LOW(False)'''


class BinaryActionCommands(Enum):
    '''Available commands to control the GPIO Pin'''

    on = "on"
    '''Turn the LED on'''

    off = "off"
    '''Turn the LED off'''

    toggle = "toggle"
    '''Toggle on/off'''


class BinaryAction(BaseAction, Debuggable, Logging):
    '''Control the state of a GPIO pin'''

    def __init__(self, parent: Platform, config: BinaryActionConfiguration) -> None:
        super().__init__(parent, config)

        self.gpio = parent.gpio
        self.configuration = config
        self.state = BinaryActionState()

        self.output_pin = self.gpio.DigitalOutputDevice(
            pin = self.configuration.pin,
            active_high = self.configuration.active_high,
            initial_value = self.configuration.initial_value
        )

        self.on_high = Automation.create_automations(self, config.on_high)
        self.on_low = Automation.create_automations(self, config.on_low)

    def toggle(self):
        call_stack = CallStack().with_key("topic", BinaryActionCommands.toggle)
        self.invoke(call_stack)

    def invoke(self, call_stack: CallStack):
        topic = str(call_stack.get("{{topic}}"))
        payload = str(call_stack.get("{{payload}}"))
        payload_lower = payload.lower()

        call_stack = call_stack.with_element(self)

        if BinaryActionCommands.on.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.on()
            self.state.is_high = True
            for on_high in self.on_high:
                on_high.invoke(call_stack)

        elif BinaryActionCommands.off.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.off()
            self.state.is_high = False
            for on_low in self.on_low:
                on_low.invoke(call_stack)

        elif BinaryActionCommands.toggle.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.toggle()
            if self.output_pin.is_active:
                self.state.is_high = True
                for on_high in self.on_high:
                    on_high.invoke(call_stack)
            else:
                self.state.is_high = False
                for on_low in self.on_low:
                    on_low.invoke(call_stack)

        elif payload_lower == 'true' or payload_lower == 'on':
            self.log_debug("Payload: " + payload)
            self.state.is_high = True
            for on_high in self.on_high:
                on_high.invoke(call_stack)

        elif payload_lower == 'false' or payload_lower == 'off':
            self.log_debug("Payload: " + payload)
            self.state.is_high = False
            for on_low in self.on_low:
                on_low.invoke(call_stack)

        else:
            self.log_error("Unknown command: {{" + topic + "}}")

        super().invoke(call_stack)

Classes

class BinaryAction (parent: Platform, config: BinaryActionConfiguration)

Control the state of a GPIO pin

Expand source code
class BinaryAction(BaseAction, Debuggable, Logging):
    '''Control the state of a GPIO pin'''

    def __init__(self, parent: Platform, config: BinaryActionConfiguration) -> None:
        super().__init__(parent, config)

        self.gpio = parent.gpio
        self.configuration = config
        self.state = BinaryActionState()

        self.output_pin = self.gpio.DigitalOutputDevice(
            pin = self.configuration.pin,
            active_high = self.configuration.active_high,
            initial_value = self.configuration.initial_value
        )

        self.on_high = Automation.create_automations(self, config.on_high)
        self.on_low = Automation.create_automations(self, config.on_low)

    def toggle(self):
        call_stack = CallStack().with_key("topic", BinaryActionCommands.toggle)
        self.invoke(call_stack)

    def invoke(self, call_stack: CallStack):
        topic = str(call_stack.get("{{topic}}"))
        payload = str(call_stack.get("{{payload}}"))
        payload_lower = payload.lower()

        call_stack = call_stack.with_element(self)

        if BinaryActionCommands.on.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.on()
            self.state.is_high = True
            for on_high in self.on_high:
                on_high.invoke(call_stack)

        elif BinaryActionCommands.off.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.off()
            self.state.is_high = False
            for on_low in self.on_low:
                on_low.invoke(call_stack)

        elif BinaryActionCommands.toggle.name == topic:
            self.log_debug("Command: " + topic)
            self.output_pin.toggle()
            if self.output_pin.is_active:
                self.state.is_high = True
                for on_high in self.on_high:
                    on_high.invoke(call_stack)
            else:
                self.state.is_high = False
                for on_low in self.on_low:
                    on_low.invoke(call_stack)

        elif payload_lower == 'true' or payload_lower == 'on':
            self.log_debug("Payload: " + payload)
            self.state.is_high = True
            for on_high in self.on_high:
                on_high.invoke(call_stack)

        elif payload_lower == 'false' or payload_lower == 'off':
            self.log_debug("Payload: " + payload)
            self.state.is_high = False
            for on_low in self.on_low:
                on_low.invoke(call_stack)

        else:
            self.log_error("Unknown command: {{" + topic + "}}")

        super().invoke(call_stack)

Ancestors

Methods

def toggle(self)
Expand source code
def toggle(self):
    call_stack = CallStack().with_key("topic", BinaryActionCommands.toggle)
    self.invoke(call_stack)

Inherited members

class BinaryActionCommands (value, names=None, *, module=None, qualname=None, type=None, start=1)

Available commands to control the GPIO Pin

Expand source code
class BinaryActionCommands(Enum):
    '''Available commands to control the GPIO Pin'''

    on = "on"
    '''Turn the LED on'''

    off = "off"
    '''Turn the LED off'''

    toggle = "toggle"
    '''Toggle on/off'''

Ancestors

  • enum.Enum

Class variables

var off

Turn the LED off

var on

Turn the LED on

var toggle

Toggle on/off

class BinaryActionConfiguration (**data: Any)

Configuration settings for a GPIO output

YAML configuration

Expand source code
@configuration
class BinaryActionConfiguration(ActionConfiguration):
    '''Configuration settings for a GPIO output'''

    pin: str
    '''GPIO PIN name. e.g. GPIO22'''

    on_high: list[AutomationConfiguration] = []
    '''List of Automations to execute when the GPIO is triggered to high, see `modules.base.Configuration.AutomationConfiguration`'''

    on_low: list[AutomationConfiguration] = []
    '''List of Automations to execute when the GPIO is triggered to low, see `modules.base.Configuration.AutomationConfiguration`'''

    active_high: bool = True
    '''"True=normal, False=inverted'''

    initial_value: bool = False
    '''expected initial value'''

    @validator('platform')
    def check_platform(cls, v):
        platform_name = "gpio"
        if v != platform_name:
            raise ValueError("wrong script platform: " + platform_name + ", is: " + v)
        return v    

Ancestors

Class variables

var active_high : bool

"True=normal, False=inverted

var initial_value : bool

expected initial value

var on_high : list

List of Automations to execute when the GPIO is triggered to high, see AutomationConfiguration

var on_low : list

List of Automations to execute when the GPIO is triggered to low, see AutomationConfiguration

var pin : str

GPIO PIN name. e.g. GPIO22

Static methods

def check_platform(v)
Expand source code
@validator('platform')
def check_platform(cls, v):
    platform_name = "gpio"
    if v != platform_name:
        raise ValueError("wrong script platform: " + platform_name + ", is: " + v)
    return v    

Inherited members

class BinaryActionState

Represents the state of the GPIO pin

Expand source code
class BinaryActionState(BaseState):
    '''Represents the state of the GPIO pin'''

    is_high = False
    '''Returns if the LED is HIGH(True) or LOW(False)'''

Ancestors

Class variables

var is_high

Returns if the LED is HIGH(True) or LOW(False)