Beispiel #1
0
async def _shutdown():
    global requested
    "Request execution of all functions"

    async_context.set('Shutdown')

    log = logging.getLogger('HABApp.Shutdown')
    log.debug('Requested shutdown')

    requested = True

    for obj in itertools.chain(filter(lambda x: not x.last, _FUNCS),
                               filter(lambda x: x.last, _FUNCS)):
        try:
            log.debug(f'{obj.msg}')
            if iscoroutinefunction(obj.func):
                await obj.func()
            else:
                obj.func()
            log.debug('-> done!')
            await sleep(0.02)
        except Exception as ex:
            log.error(ex)
            tb = traceback.format_exc().splitlines()
            for line in tb:
                log.error(line)

    log.debug('Shutdown complete')
    return None
Beispiel #2
0
async def test_item_has_name(func, args):
    async_context.set('Test')

    if func not in (post_update, send_command):
        with pytest.raises(AsyncContextError) as e:
            func(*args)
        assert e.value.func == func
    else:
        # call the function to make sure it doesn't raise an exception
        func(*args)
Beispiel #3
0
async def test_error_msg():
    def my_sync_func():
        if async_context.get(None) is not None:
            raise AsyncContextError(my_sync_func)

    async_context.set('Test')
    with pytest.raises(AsyncContextError) as e:
        my_sync_func()
    assert str(
        e.value
    ) == 'Function "my_sync_func" may not be called from an async context!'
    def tear_down(self):
        async_context.set('Tear down test')

        self.vars.pop('__UNITTEST__')
        self.vars.pop('__HABAPP__RUNTIME__')
        self.vars.pop('__HABAPP__RULE_FILE__')
        loaded_rules = self.vars.pop('__HABAPP__RULES')
        for rule in loaded_rules:
            rule._unload()
        loaded_rules.clear()

        # restore patched
        self.restore()
    async def async_run(self, *args, **kwargs):

        token = async_context.set('WrappedFunction')

        try:
            await self._func(*args, **kwargs)
        except Exception as e:
            self.__format_traceback(e, *args, **kwargs)

        async_context.reset(token)
        return None
Beispiel #6
0
    async def start(self, config_folder: Path):
        try:
            token = async_context.set('HABApp startup')

            # setup exception handler for the scheduler
            eascheduler.set_exception_handler(lambda x: process_exception('HABApp.scheduler', x))

            # Start Folder watcher!
            HABApp.core.files.watcher.start()

            self.config_loader = HABApp.config.HABAppConfigLoader(config_folder)

            await HABApp.core.files.setup()

            # generic HTTP
            await HABApp.rule.interfaces._http.create_client()

            # openhab
            openhab_connection.setup()

            # Parameter Files
            await HABApp.parameters.parameter_files.setup_param_files()

            # Rule engine
            self.rule_manager = HABApp.rule_manager.RuleManager(self)
            await self.rule_manager.setup()

            # MQTT
            HABApp.mqtt.mqtt_connection.setup()
            HABApp.mqtt.mqtt_connection.connect()

            await openhab_connection.start()

            shutdown.register_func(HABApp.core.const.loop.stop, msg='Stopping asyncio loop')

            async_context.reset(token)
        except asyncio.CancelledError:
            pass
        except Exception as e:
            process_exception('Runtime.start', e)
            await asyncio.sleep(1)  # Sleep so we can do a graceful shutdown
            shutdown.request_shutdown()
Beispiel #7
0
 async def _run_next(self):
     async_context.set('HABAppScheduler')
     return await super()._run_next()