Exemple #1
0
    def __init__(self, *args, **kwargs):
        print("Initializing switch")
        super().__init__(*args, **kwargs)

        self.switch_state = None
        self.pin = machine.Pin(self.settings["pin"], machine.Pin.IN)
        self.switch_state = int(not self.pin.value())
        self.switch = Switch(self.pin)
        
        if self.settings["invert_switch"] == False:
            self.switch_off_func = self.switch_off
            self.switch_on_func = self.switch_on
        else:
            self.switch_off_func = self.switch_on
            self.switch_on_func = self.switch_off
        
        self.switch.close_func(self.switch_on_func)
        self.switch.open_func(self.switch_off_func)
Exemple #2
0
    def __init__(self, id, rpin, swpin, name="Light Switch", type="Shelly"):
        super().__init__(id=id, name=name, type=type)
        self.relay = Pin(rpin, Pin.OUT, value=0)
        self.switch = Switch(Pin(swpin, Pin.IN))

        self.relay_property = HomieNodeProperty(
            id="power",
            name=id,
            settable=True,
            retained=True,
            datatype="boolean",
            default=FALSE,
            restore=True,
        )
        self.add_property(self.relay_property)

        self.switch.open_func(self.toggle, ())
        self.switch.close_func(self.toggle, ())
Exemple #3
0
class ShellyRelay(HomieNode):
    def __init__(self, id, rpin, swpin, name="Light Switch", type="Shelly"):
        super().__init__(id=id, name=name, type=type)
        self.relay = Pin(rpin, Pin.OUT, value=0)
        self.switch = Switch(Pin(swpin, Pin.IN))

        self.relay_property = HomieNodeProperty(
            id="power",
            name=id,
            settable=True,
            retained=True,
            datatype="boolean",
            default=FALSE,
            restore=True,
        )
        self.add_property(self.relay_property)

        self.switch.open_func(self.toggle, ())
        self.switch.close_func(self.toggle, ())

    def off(self):
        self.relay(0)
        self.relay_property.data = FALSE

    def on(self):
        self.relay(1)
        self.relay_property.data = TRUE

    def callback(self, topic, payload, retained):
        if b"power" in topic:
            if payload == FALSE:
                self.off()
            elif payload == TRUE:
                self.on()

    def toggle(self):
        if self.relay_property.data == TRUE:
            self.off()
        else:
            self.on()
Exemple #4
0
def sw_start():

    pin = PCFPin(pcf, 0)
    relay = PCFPin(pcf, 4)

    sw = Switch(pin)
    sw.close_func(toggle, (relay, ))
    sw.open_func(toggle, (relay, ))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
Exemple #5
0
def test_swcb():
    print('Test of switch executing callbacks.')
    print(helptext)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(toggle, (red, ))
    sw.open_func(toggle, (green, ))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
Exemple #6
0
def test_sw():
    print('Test of switch scheduling coroutines.')
    print(helptext)
    pin = Pin('X1', Pin.IN, Pin.PULL_UP)
    red = LED(1)
    green = LED(2)
    sw = Switch(pin)
    # Register a coro to launch on contact close
    sw.close_func(pulse, (green, 1000))
    sw.open_func(pulse, (red, 1000))
    loop = asyncio.get_event_loop()
    loop.run_until_complete(killer())
Exemple #7
0
 def init(self, plugin, device, queue, scriptqueue):
     self._log.debug("Plugin: switch init")
     # generic section
     self._utils.plugin_initdata(self, plugin, device, queue, scriptqueue)
     self.content = plugin.get('content', content)
     self.pincnt = pincnt
     self.valuecnt = valuecnt
     self.stype = stype
     plugin['dtype'] = dtype
     plugin['stype'] = stype
     plugin['template'] = template
     datastore = self._plugins.readstore(self.devicename)
     # plugin specific section
     self.switch_status = bootstate
     if self.inputtype == 'normal':
         self._log.debug("Plugin: switch init normal, pin: " + self.dxpin)
         # Setup switch
         self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                    core.PIN_PULL_UP)
         self.switch = Switch(self.swpin)
         # Register coros to launch on contact close and open
         self.switch.close_func(self.asyncswitchopen)
         self.switch.open_func(self.asyncswitchclosed)
     elif self.inputtype == 'low':
         self._log.debug("Plugin: switch init low, pin: " + self.dxpin)
         # Setup button active low
         self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                    core.PIN_PULL_UP)
         self.switch = Pushbutton(self.swpin)
         self.switch.press_func(self.asyncbuttonpress)
         self.switch.release_func(self.asyncbuttonrelease)
         self.switch.double_func(self.asyncbuttondouble)
         self.switch.long_func(self.asyncbuttonlong)
     else:
         self._log.debug("Plugin: switch init high, pin: " + self.dxpin)
         # Setup button active high
         self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                    core.PIN_PULL_DOWN)
         self.switch = Pushbutton(self.swpin)
         self.switch.press_func(self.asyncbuttonpress)
         self.switch.release_func(self.asyncbuttonrelease)
         self.switch.double_func(self.asyncbuttondouble)
         self.switch.long_func(self.asyncbuttonlong)
     return True
Exemple #8
0
    def saveform(self, plugindata):
        self._log.debug("Plugin: switch saveform")
        # generic section
        self._utils.plugin_saveform(self, plugindata)
        # plugin specific section
        self.inputtype = plugindata['inputtype']
        self.dxpin = plugindata['dxpin0']

        # store values
        data = {}
        data["inputtype"] = self.inputtype
        data["dxpin"] = self.dxpin
        data["valueN1"] = self.valuenames["valueN1"]
        data["valueF1"] = self.valuenames["valueF1"]
        data["valueD1"] = self.valuenames["valueD1"]
        self._plugins.writestore(self.devicename, data)

        if self.inputtype == 'normal':
            # Setup switch
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Switch(self.swpin)
            # Register coros to launch on contact close and open
            self.switch.close_func(self.asyncswitchopen)
            self.switch.open_func(self.asyncswitchclosed)
        elif self.inputtype == 'low':
            # Setup button active low
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_UP)
            self.switch = Pushbutton(self.swpin)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)
        else:
            # Setup button active high
            self.swpin = self._hal.pin(self.dxpin, core.PIN_IN,
                                       core.PIN_PULL_DOWN)
            self.switch.press_func(self.asyncbuttonpress)
            self.switch.release_func(self.asyncbuttonrelease)
            self.switch.double_func(self.asyncbuttondouble)
            self.switch.long_func(self.asyncbuttonlong)