diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index 23cfa51d2b..884ae9867a 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -3,7 +3,8 @@ import esphome.config_validation as cv from esphome import automation from esphome.automation import Condition from esphome.const import CONF_DATA, CONF_DATA_TEMPLATE, CONF_ID, CONF_PASSWORD, CONF_PORT, \ - CONF_REBOOT_TIMEOUT, CONF_SERVICE, CONF_VARIABLES, CONF_SERVICES, CONF_TRIGGER_ID, CONF_EVENT + CONF_REBOOT_TIMEOUT, CONF_SERVICE, CONF_VARIABLES, CONF_SERVICES, CONF_TRIGGER_ID, CONF_EVENT, \ + CONF_TAG from esphome.core import coroutine_with_priority DEPENDENCIES = ['network'] @@ -137,6 +138,23 @@ def homeassistant_event_to_code(config, action_id, template_arg, args): yield var +HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA = cv.maybe_simple_value({ + cv.GenerateID(): cv.use_id(APIServer), + cv.Required(CONF_TAG): cv.templatable(cv.string_strict), +}, key=CONF_TAG) + + +@automation.register_action('homeassistant.tag_scanned', HomeAssistantServiceCallAction, + HOMEASSISTANT_TAG_SCANNED_ACTION_SCHEMA) +def homeassistant_tag_scanned_to_code(config, action_id, template_arg, args): + serv = yield cg.get_variable(config[CONF_ID]) + var = cg.new_Pvariable(action_id, template_arg, serv, True) + cg.add(var.set_service('esphome.tag_scanned')) + templ = yield cg.templatable(config[CONF_TAG], args, cg.std_string) + cg.add(var.add_data('tag_id', templ)) + yield var + + @automation.register_condition('api.connected', APIConnectedCondition, {}) def api_connected_to_code(config, condition_id, template_arg, args): yield cg.new_Pvariable(condition_id, template_arg) diff --git a/tests/test2.yaml b/tests/test2.yaml index 077ceab886..b57ef21f30 100644 --- a/tests/test2.yaml +++ b/tests/test2.yaml @@ -280,6 +280,9 @@ text_sensor: service: light.turn_on data: entity_id: light.my_light + - homeassistant.tag_scanned: + tag: 1234-abcd + - homeassistant.tag_scanned: 1234-abcd - platform: template name: "Template Text Sensor" lambda: |-