def __init__(self, remote_api, local_api=None, loop=None): """Initalize the forward instance.""" if not remote_api.validate_api(): raise HomeAssistantError( "Remote API at {}:{} not valid: {}".format( remote_api.host, remote_api.port, remote_api.status)) self.remote_api = remote_api self.loop = loop or asyncio.get_event_loop() self.executor = ThreadPoolExecutor(max_workers=5) self.loop.set_default_executor(self.executor) self.loop.set_exception_handler(self._async_exception_handler) self.pool = ha.create_worker_pool() self.bus = EventBus(remote_api, self) self.services = ha.ServiceRegistry(self.bus, self.add_job, self.loop) self.states = StateMachine(self.bus, self.loop, self.remote_api) self.config = ha.Config() # This is a dictionary that any component can store any data on. self.data = {} self.state = ha.CoreState.not_running self.exit_code = None self._websession = None self.config.api = local_api
def setUp(self): # pylint: disable=invalid-name """ things to be run when tests are started. """ self.pool = ha.create_worker_pool(0) self.bus = ha.EventBus(self.pool) self.states = ha.StateMachine(self.bus) self.states.set("light.Bowl", "on") self.states.set("switch.AC", "off")
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.pool = ha.create_worker_pool(0) self.bus = ha.EventBus(self.pool) def add_job(*args, **kwargs): """Forward calls to add_job on Home Assistant.""" # self works because we also have self.pool defined. return ha.HomeAssistant.add_job(self, *args, **kwargs) self.services = ha.ServiceRegistry(self.bus, add_job) self.services.register("Test_Domain", "TEST_SERVICE", lambda x: None)
def setUp(self): # pylint: disable=invalid-name """Setup things to be run when tests are started.""" self.pool = ha.create_worker_pool(0) self.bus = ha.EventBus(self.pool) def add_job(*args, **kwargs): """Forward calls to add_job on Home Assistant.""" # self works because we also have self.pool defined. return ha.HomeAssistant.add_job(self, *args, **kwargs) self.services = ha.ServiceRegistry(self.bus, add_job) self.services.register("test_domain", "test_service", lambda x: None)
def test_exception_during_job(self): pool = ha.create_worker_pool(1) def malicious_job(_): raise Exception("Test breaking worker pool") calls = [] def register_call(_): calls.append(1) pool.add_job(ha.JobPriority.EVENT_DEFAULT, (malicious_job, None)) pool.add_job(ha.JobPriority.EVENT_DEFAULT, (register_call, None)) pool.block_till_done() self.assertEqual(1, len(calls))
def __init__(self, remote_api, local_api=None): if not remote_api.validate_api(): raise ha.HomeAssistantError( "Remote API at {}:{} not valid: {}".format( remote_api.host, remote_api.port, remote_api.status)) self.remote_api = remote_api self.pool = pool = ha.create_worker_pool() self.bus = EventBus(remote_api, pool) self.services = ha.ServiceRegistry(self.bus, pool) self.states = StateMachine(self.bus, self.remote_api) self.config = ha.Config() self.config.api = local_api
def __init__(self, remote_api, local_api=None): if not remote_api.validate_api(): raise HomeAssistantError( "Remote API at {}:{} not valid: {}".format( remote_api.host, remote_api.port, remote_api.status)) self.remote_api = remote_api self.pool = pool = ha.create_worker_pool() self.bus = EventBus(remote_api, pool) self.services = ha.ServiceRegistry(self.bus, pool) self.states = StateMachine(self.bus, self.remote_api) self.config = ha.Config() self.config.api = local_api
def test_exception_during_job(self): """Test exception during a job.""" pool = ha.create_worker_pool(1) def malicious_job(_): raise Exception("Test breaking worker pool") calls = [] def register_call(_): calls.append(1) pool.add_job((malicious_job, None)) pool.block_till_done() pool.add_job((register_call, None)) pool.block_till_done() self.assertEqual(1, len(calls))
def __init__(self, remote_api, local_api=None, loop=None): """Initalize the forward instance.""" if not remote_api.validate_api(): raise HomeAssistantError( "Remote API at {}:{} not valid: {}".format(remote_api.host, remote_api.port, remote_api.status) ) self.remote_api = remote_api self.loop = loop or asyncio.get_event_loop() self.pool = ha.create_worker_pool() self.bus = EventBus(remote_api, self) self.services = ha.ServiceRegistry(self.bus, self.add_job, self.loop) self.states = StateMachine(self.bus, self.loop, self.remote_api) self.config = ha.Config() self.state = ha.CoreState.not_running self.config.api = local_api
def __init__(self, remote_api, local_api=None, loop=None): """Initalize the forward instance.""" if not remote_api.validate_api(): raise HomeAssistantError( "Remote API at {}:{} not valid: {}".format( remote_api.host, remote_api.port, remote_api.status)) self.remote_api = remote_api self.loop = loop or asyncio.get_event_loop() self.pool = ha.create_worker_pool() self.bus = EventBus(remote_api, self) self.services = ha.ServiceRegistry(self.bus, self.add_job, self.loop) self.states = StateMachine(self.bus, self.loop, self.remote_api) self.config = ha.Config() self.state = ha.CoreState.not_running self.config.api = local_api
def setUp(self): # pylint: disable=invalid-name """ things to be run when tests are started. """ self.pool = ha.create_worker_pool(0) self.bus = ha.EventBus(self.pool) self.services = ha.ServiceRegistry(self.bus, self.pool) self.services.register("test_domain", "test_service", lambda x: None)
def setUp(self): # pylint: disable=invalid-name """ things to be run when tests are started. """ self.bus = ha.EventBus(ha.create_worker_pool(0)) self.bus.listen('test_event', lambda x: len)