External enable/disable intent
Allow intents to be enabled or disabled from outside of the skill via
the messagebus.
Adds mycroft.skill.enable_intent and mycroft.skill.disable_intent
@@ -264,6 +264,10 @@ class MycroftSkill(object):
self.emitter = emitter
self.enclosure = EnclosureAPI(emitter, self.name)
self.__register_stop()
+ self.add_event('mycroft.skill.enable_intent',
+ self.handle_enable_intent)
+ self.add_event('mycroft.skill.disable_intent',
+ self.handle_disable_intent)
def __register_stop(self):
self.stop_time = time.time()
@@ -734,24 +738,67 @@ class MycroftSkill(object):
"name": name
}))
+ def handle_enable_intent(self, message):
+ """
+ Listener to enable a registered intent if it belongs to this skill
+ """
+ intent_name = message.data["intent_name"]
+ for (name, intent) in self.registered_intents:
+ if name == intent_name:
+ return self.enable_intent(intent_name)
+
+ def handle_disable_intent(self, message):
+ """
+ Listener to disable a registered intent if it belongs to this skill
+ """
+ intent_name = message.data["intent_name"]
+ for (name, intent) in self.registered_intents:
+ if name == intent_name:
+ return self.disable_intent(intent_name)
+
def disable_intent(self, intent_name):
- """Disable a registered intent"""
+ """
+ Disable a registered intent if it belongs to this skill
+
+ Args:
+ intent_name: name of the intent to be disabled
+
+ Returns:
+ bool: True if disabled, False if it wasn't registered
+ """
+ names = [intent_tuple[0] for intent_tuple in self.registered_intents]
+ if intent_name in names:
LOG.debug('Disabling intent ' + intent_name)
name = str(self.skill_id) + ':' + intent_name
- self.emitter.emit(Message("detach_intent", {"intent_name": name}))
+ self.emitter.emit(
+ Message("detach_intent", {"intent_name": name}))
+ return True
+ LOG.error('Could not disable ' + intent_name +
+ ', it hasn\'t been registered.')
+ return False
def enable_intent(self, intent_name):
- """Reenable a registered intent"""
- for (name, intent) in self.registered_intents:
- if name == intent_name:
- self.registered_intents.remove((name, intent))
- intent.name = name
+ """
+ (Re)Enable a registered intentif it belongs to this skill
+
+ Args:
+ intent_name: name of the intent to be enabled
+
+ Returns:
+ bool: True if enabled, False if it wasn't registered
+ """
+ names = [intent[0] for intent in self.registered_intents]
+ intents = [intent[1] for intent in self.registered_intents]
+ if intent_name in names:
+ intent = intents[names.index(intent_name)]
+ self.registered_intents.remove((intent_name, intent))
+ intent.name = intent_name
self.register_intent(intent, None)
LOG.debug('Enabling intent ' + intent_name)
- break
- else:
- LOG.error('Could not enable ' + intent_name +
- ', it hasn\'t been registered.')
+ return True
+ LOG.error('Could not enable ' + intent_name + ', it hasn\'t been '
+ 'registered.')
+ return False
def set_context(self, context, word=''):
"""
