Skip to content

Garage Door Opener #5

@fubano

Description

@fubano

Thanks for sharing this project, I find python a little easier to using NodeJS. I can get the TempSensor, LightBulb to work on my Pi Zero W, but my attempt to mash code from multiple existing accessories to create a Garage Door Opener fails (it pairs and then unpairs right away). Can someone can point to the issues with this code? Not sure getstate is needed at all, my suspicion was that the required characteristic state is not getting set correctly.

class GarageDoorOpener(Accessory):
category = Category.GARAGE_DOOR_OPENER

@classmethod
def _gpio_setup(_cls, pulse_pin, sense_pin):
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(pulse_pin, GPIO.OUT)
    GPIO.setup(sense_pin, GPIO.IN)
    GPIO.add_event_detect(sense_pin, GPIO.BOTH, callback=self._detected)

def __init__(self, *args, pulse_pin=26, sense_pin=22, **kwargs):
    super(GarageDoorOpener, self).__init__(*args, **kwargs)

    self.CurrentDoorState = self.get_service("GarageDoorOpener").get_characteristic("CurrentDoorState")
    self.ObstructionDetected = self.get_service("GarageDoorOpener").get_characteristic("ObstructionDetected")

    self.pulse_pin = pulse_pin
    self.sense_pin = sense_pin
    self._gpio_setup(pulse_pin,sense_pin)

def __getstate__(self):
    state = super(GarageDoorOpener, self).__getstate__()
    return state

def __setstate__(self, state):
    self.__dict__.update(state)
    self._gpio_setup(self.pulse_pin, self.sense_pin)

def _pulse_door(self, value):
    GPIO.output(self.pulse_pin, GPIO.HIGH)
    time.sleep(2)
    GPIO.output(self.pulse_pin, GPIO.LOW)
    time.sleep(2)

def _set_services(self):
    super(GarageDoorOpener, self)._set_services()

    garage_door_opener_service = loader.get_serv_loader().get("GarageDoorOpener")
    self.add_service(garage_door_opener_service)
    garage_door_opener_service.get_characteristic("TargetDoorState").setter_callback = self._pulse_door

def _detected(self, _pin):
    if(GPIO.input(self.sense_pin)):
        self.CurrentDoorState.set_value(1)
    else:
        self.CurrentDoorState.set_value(0)

def stop(self):
    super(GarageDoorOpener, self).stop()
    GPIO.cleanup()

~

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions