예제 #1
0
def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Netio platform."""

    host = config.get(CONF_HOST)
    username = config.get(CONF_USERNAME)
    password = config.get(CONF_PASSWORD)
    port = config.get(CONF_PORT)

    if not DEVICES:
        hass.http.register_view(NetioApiView)

    dev = Netio(host, port, username, password)

    DEVICES[host] = Device(dev, [])

    # Throttle the update for all Netio switches of one Netio
    dev.update = util.Throttle(MIN_TIME_BETWEEN_SCANS)(dev.update)

    for key in config[CONF_OUTLETS]:
        switch = NetioSwitch(DEVICES[host].netio, key, config[CONF_OUTLETS][key])
        DEVICES[host].entities.append(switch)

    add_entities(DEVICES[host].entities)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, dispose)
    return True
예제 #2
0
def setup_platform(hass, config, add_devices_callback, discovery_info=None):
    """Configure the netio platform."""
    from pynetio import Netio

    if validate_config({"conf": config}, {"conf": [CONF_OUTLETS, CONF_HOST]},
                       _LOGGER):
        if len(DEVICES) == 0:
            hass.wsgi.register_view(NetioApiView)

        dev = Netio(config[CONF_HOST], config.get(CONF_PORT, DEFAULT_PORT),
                    config.get(CONF_USERNAME, DEFAULT_USERNAME),
                    config.get(CONF_PASSWORD, DEFAULT_USERNAME))

        DEVICES[config[CONF_HOST]] = Device(dev, [])

        # Throttle the update for all NetioSwitches of one Netio
        dev.update = util.Throttle(MIN_TIME_BETWEEN_SCANS)(dev.update)

        for key in config[CONF_OUTLETS]:
            switch = NetioSwitch(DEVICES[config[CONF_HOST]].netio, key,
                                 config[CONF_OUTLETS][key])
            DEVICES[config[CONF_HOST]].entities.append(switch)

        add_devices_callback(DEVICES[config[CONF_HOST]].entities)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, dispose)
    return True
예제 #3
0
def setup_platform(
    hass: HomeAssistant,
    config: ConfigType,
    add_entities: AddEntitiesCallback,
    discovery_info: DiscoveryInfoType | None = None,
) -> None:
    """Set up the Netio platform."""

    host = config[CONF_HOST]
    username = config[CONF_USERNAME]
    password = config[CONF_PASSWORD]
    port = config[CONF_PORT]

    if not DEVICES:
        hass.http.register_view(NetioApiView)

    dev = Netio(host, port, username, password)

    DEVICES[host] = Device(dev, [])

    # Throttle the update for all Netio switches of one Netio
    dev.update = util.Throttle(MIN_TIME_BETWEEN_SCANS)(dev.update)

    for key in config[CONF_OUTLETS]:
        switch = NetioSwitch(DEVICES[host].netio, key,
                             config[CONF_OUTLETS][key])
        DEVICES[host].entities.append(switch)

    add_entities(DEVICES[host].entities)

    hass.bus.listen_once(EVENT_HOMEASSISTANT_STOP, dispose)
예제 #4
0
    def test_throttle_on_method(self):
        """ Test that throttle works when wrapping a method. """
        class Tester(object):
            def hello(self):
                return True

        tester = Tester()
        throttled = util.Throttle(timedelta(seconds=1))(tester.hello)

        self.assertTrue(throttled())
        self.assertIsNone(throttled())
예제 #5
0
def test_throttle_on_method():
    """Test that throttle works when wrapping a method."""
    class Tester:
        """A tester class for the throttle."""
        def hello(self):
            """Test the throttle."""
            return True

    tester = Tester()
    throttled = util.Throttle(timedelta(seconds=1))(tester.hello)

    assert throttled()
    assert throttled() is None