Skip to content

RPi.GPIO does not work within pyscript #498

@Jelle7

Description

@Jelle7

I am trying to do a complex automation in HA, running on a raspberry pi 4 as HA OS. The automation involves running a python script which imports the RPI.GPIO package to control a RF module on the PI which has HA installed.

For some reason, importing the package and running it is no problem but it does not seem to actually send stuff to my RPI GPIO pins. I know there is a GPIO package for 455Mhz on HACS but it sends a number while I need to send something in binary. See my script below:

import time
import sys
import RPi.GPIO as GPIO
command = {
    'up': '1101101010010011111',
    'down': '1101101010010011000',
    'stop': '1101101010010011101'
}
short_delay = 0.00035
long_delay = 0.00070
extended_delay = 0.015212

NUM_ATTEMPTS = 15
TRANSMIT_PIN = 23

@state_trigger("input_select.shutter_state")
def shutter_action(trigger_type=None, var_name=None, value=None):
    move_shutter(value)

@service
def move_shutter(action):
    '''Transmit a chosen code string using the GPIO transmitter'''
    code = command.get(action)
    log.info(f"Action: {action}, Code: {code}")
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(TRANSMIT_PIN, GPIO.OUT)
    for t in range(NUM_ATTEMPTS):
        for i in code:
            if i == '1':
                GPIO.output(TRANSMIT_PIN, 0)
                time.sleep(long_delay)
                GPIO.output(TRANSMIT_PIN, 1)
                time.sleep(short_delay)
            elif i == '0':
                GPIO.output(TRANSMIT_PIN, 0)
                time.sleep(short_delay)
                GPIO.output(TRANSMIT_PIN, 1)
                time.sleep(long_delay)
            else:
                continue
        GPIO.output(TRANSMIT_PIN, 0)
        time.sleep(extended_delay)
    GPIO.cleanup()

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions