mirror of
				https://github.com/esphome/esphome.git
				synced 2025-10-31 15:12:06 +00:00 
			
		
		
		
	[web_server] Event component grouping (#7586)
This commit is contained in:
		| @@ -1,6 +1,6 @@ | |||||||
| from esphome import automation | from esphome import automation | ||||||
| import esphome.codegen as cg | import esphome.codegen as cg | ||||||
| from esphome.components import mqtt | from esphome.components import mqtt, web_server | ||||||
| import esphome.config_validation as cv | import esphome.config_validation as cv | ||||||
| from esphome.const import ( | from esphome.const import ( | ||||||
|     CONF_DEVICE_CLASS, |     CONF_DEVICE_CLASS, | ||||||
| @@ -11,6 +11,7 @@ from esphome.const import ( | |||||||
|     CONF_MQTT_ID, |     CONF_MQTT_ID, | ||||||
|     CONF_ON_EVENT, |     CONF_ON_EVENT, | ||||||
|     CONF_TRIGGER_ID, |     CONF_TRIGGER_ID, | ||||||
|  |     CONF_WEB_SERVER, | ||||||
|     DEVICE_CLASS_BUTTON, |     DEVICE_CLASS_BUTTON, | ||||||
|     DEVICE_CLASS_DOORBELL, |     DEVICE_CLASS_DOORBELL, | ||||||
|     DEVICE_CLASS_EMPTY, |     DEVICE_CLASS_EMPTY, | ||||||
| @@ -40,17 +41,21 @@ EventTrigger = event_ns.class_("EventTrigger", automation.Trigger.template()) | |||||||
|  |  | ||||||
| validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_") | validate_device_class = cv.one_of(*DEVICE_CLASSES, lower=True, space="_") | ||||||
|  |  | ||||||
| EVENT_SCHEMA = cv.ENTITY_BASE_SCHEMA.extend(cv.MQTT_COMPONENT_SCHEMA).extend( | EVENT_SCHEMA = ( | ||||||
|     { |     cv.ENTITY_BASE_SCHEMA.extend(web_server.WEBSERVER_SORTING_SCHEMA) | ||||||
|         cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTEventComponent), |     .extend(cv.MQTT_COMPONENT_SCHEMA) | ||||||
|         cv.GenerateID(): cv.declare_id(Event), |     .extend( | ||||||
|         cv.Optional(CONF_DEVICE_CLASS): validate_device_class, |         { | ||||||
|         cv.Optional(CONF_ON_EVENT): automation.validate_automation( |             cv.OnlyWith(CONF_MQTT_ID, "mqtt"): cv.declare_id(mqtt.MQTTEventComponent), | ||||||
|             { |             cv.GenerateID(): cv.declare_id(Event), | ||||||
|                 cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(EventTrigger), |             cv.Optional(CONF_DEVICE_CLASS): validate_device_class, | ||||||
|             } |             cv.Optional(CONF_ON_EVENT): automation.validate_automation( | ||||||
|         ), |                 { | ||||||
|     } |                     cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(EventTrigger), | ||||||
|  |                 } | ||||||
|  |             ), | ||||||
|  |         } | ||||||
|  |     ) | ||||||
| ) | ) | ||||||
|  |  | ||||||
| _UNDEF = object() | _UNDEF = object() | ||||||
| @@ -97,6 +102,9 @@ async def setup_event_core_(var, config, *, event_types: list[str]): | |||||||
|         mqtt_ = cg.new_Pvariable(mqtt_id, var) |         mqtt_ = cg.new_Pvariable(mqtt_id, var) | ||||||
|         await mqtt.register_mqtt_component(mqtt_, config) |         await mqtt.register_mqtt_component(mqtt_, config) | ||||||
|  |  | ||||||
|  |     if web_server_config := config.get(CONF_WEB_SERVER): | ||||||
|  |         await web_server.add_entity_config(var, web_server_config) | ||||||
|  |  | ||||||
|  |  | ||||||
| async def register_event(var, config, *, event_types: list[str]): | async def register_event(var, config, *, event_types: list[str]): | ||||||
|     if not CORE.has_id(config[CONF_ID]): |     if not CORE.has_id(config[CONF_ID]): | ||||||
|   | |||||||
| @@ -1443,7 +1443,7 @@ void WebServer::on_event(event::Event *obj, const std::string &event_type) { | |||||||
| } | } | ||||||
|  |  | ||||||
| std::string WebServer::event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config) { | std::string WebServer::event_json(event::Event *obj, const std::string &event_type, JsonDetail start_config) { | ||||||
|   return json::build_json([obj, event_type, start_config](JsonObject root) { |   return json::build_json([this, obj, event_type, start_config](JsonObject root) { | ||||||
|     set_json_id(root, obj, "event-" + obj->get_object_id(), start_config); |     set_json_id(root, obj, "event-" + obj->get_object_id(), start_config); | ||||||
|     if (!event_type.empty()) { |     if (!event_type.empty()) { | ||||||
|       root["event_type"] = event_type; |       root["event_type"] = event_type; | ||||||
| @@ -1454,6 +1454,12 @@ std::string WebServer::event_json(event::Event *obj, const std::string &event_ty | |||||||
|         event_types.add(event_type); |         event_types.add(event_type); | ||||||
|       } |       } | ||||||
|       root["device_class"] = obj->get_device_class(); |       root["device_class"] = obj->get_device_class(); | ||||||
|  |       if (this->sorting_entitys_.find(obj) != this->sorting_entitys_.end()) { | ||||||
|  |         root["sorting_weight"] = this->sorting_entitys_[obj].weight; | ||||||
|  |         if (this->sorting_groups_.find(this->sorting_entitys_[obj].group_id) != this->sorting_groups_.end()) { | ||||||
|  |           root["sorting_group"] = this->sorting_groups_[this->sorting_entitys_[obj].group_id].name; | ||||||
|  |         } | ||||||
|  |       } | ||||||
|     } |     } | ||||||
|   }); |   }); | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user