def build_widget(self, config: Dict) -> str: mapping = {} html = '<div class="widget-panel panel panel-primary"><div class="panel-heading"><h3 class="panel-title">{' \ '} ({})</h3></div><div class="panel-body">'.format(self.name, self.driver) html += '<div class="device-status" id="status-{}"></div>'.format( self.name.replace(' ', '-')) buttons = config.get('buttons') if len(buttons) > 1: html += '<div class="btn-group" role="group" aria-label="...">' for button in buttons: if not button.get('method') and not button.get('action'): raise WidgetSetupError( 'Widget must have at least a method or action defined') _id = random_string(6) html += '<button class="widget btn {_class}" id="{id}">{text}</button>'.format( _class=button.get('class', 'btn-primary'), id=_id, text=button.get('text', _id)) mapping[_id] = ('method' if button.get('method') else 'action', method_from_name(self.dev, button.get('method')) if button.get('method') else button.get('action') + " " + self.name, button.get('config', {}), self) if len(buttons) > 1: html += '</div>' html += '</div></div>' self.widget = {'html': html, 'mapping': mapping}
def add_scheduled_job(job: Dict): if job.get('action'): scheduler.add_job(get_action(job.pop('action')).run, trigger=job.pop('trigger', 'cron'), kwargs=dict(jitter=True), **job) elif job.get('device'): device = get_device(job.pop('device')) method = method_from_name(device.dev, job.pop('method')) scheduler.add_job(method, trigger=job.pop('trigger', 'cron'), kwargs=job.pop('config', {}), **job)
def setup(self): for device in self.devices: device.widget = self.widget device.setup() if self.widget: self.widget = self.devices[0].widget for key in self.widget['mapping']: _map = self.widget['mapping'][key] self.widget['mapping'][key] = (_map[0], method_from_name( self, _map[1].__name__), _map[2], self) widgets.update(self.widget['mapping']) self.widget['html'] = self.widget['html'].replace( self.devices[0].name, self.name) self.widget['html'] = self.widget['html'].replace( 'status-' + self.name, 'status-' + self.name.replace(' ', '_'))
def prerun(self) -> (Iterator[Process], Iterator[int]): for action in self.actions: action.run() for callback, args, kwargs in self.subscriptions: callback(*args, **kwargs) for device, config in self.devices: method = method_from_name(device.dev, config['method']) kwargs = config.get('config', {}) print("Execute action", config['method']) if device.driver.noserialize or type(device) is MultiDevice: multiprocessing_run(target=method, delay=config.get('delay', 0), **kwargs) continue try: yield device, method, config.get('delay', 0), kwargs except Exception as e: print("Error", e)
def handle_task(post: dict, client: APIClient) -> bool: try: device = get_device(post.pop('device').strip()) method = method_from_name(device.dev, post.pop('method')) except StopIteration: return False from home.web.web import app if not client.has_permission(device.group): app.logger.warning( "({}) Insufficient API permissions to execute '{}' on '{}' with config {}" .format(client.name, method.__name__, device.name, post)) return False app.logger.info("({}) Execute '{}' on '{}' with config {}".format( client.name, method.__name__, device.name, post)) if device.driver.noserialize or type(device) is MultiDevice: method(**post) else: device.last_task = run(method, **post) return True
def method(*args, **kwargs): for device in self.devices: method = method_from_name(device.dev, name) method(*args, **kwargs)