From 024ce0e8eb6608da7330aed8e022d3c5241148de Mon Sep 17 00:00:00 2001 From: Otto Winter Date: Tue, 18 Jun 2019 17:43:11 +0200 Subject: [PATCH] Add ESPHome event generation and user-defined service array support (#24595) * Add ESPHome event generation and user-defined service array support * Comments * Lint --- homeassistant/components/esphome/__init__.py | 23 +++++++++++++++---- .../components/esphome/manifest.json | 2 +- requirements_all.txt | 2 +- requirements_test_all.txt | 2 +- 4 files changed, 21 insertions(+), 8 deletions(-) diff --git a/homeassistant/components/esphome/__init__.py b/homeassistant/components/esphome/__init__.py index 338968a4fd7..db5aeea2aa1 100644 --- a/homeassistant/components/esphome/__init__.py +++ b/homeassistant/components/esphome/__init__.py @@ -6,7 +6,7 @@ from typing import Any, Callable, Dict, List, Optional from aioesphomeapi import ( APIClient, APIConnectionError, DeviceInfo, EntityInfo, EntityState, - ServiceCall, UserService, UserServiceArgType) + HomeassistantServiceCall, UserService, UserServiceArgType) import voluptuous as vol from homeassistant import const @@ -86,7 +86,7 @@ async def async_setup_entry(hass: HomeAssistantType, entry_data.async_update_state(hass, state) @callback - def async_on_service_call(service: ServiceCall) -> None: + def async_on_service_call(service: HomeassistantServiceCall) -> None: """Call service when user automation in ESPHome config is triggered.""" domain, service_name = service.service.split('.', 1) service_data = service.data @@ -102,8 +102,17 @@ async def async_setup_entry(hass: HomeAssistantType, _LOGGER.error('Error rendering data template: %s', ex) return - hass.async_create_task(hass.services.async_call( - domain, service_name, service_data, blocking=True)) + if service.is_event: + # ESPHome uses servicecall packet for both events and service calls + # Ensure the user can only send events of form 'esphome.xyz' + if domain != 'esphome': + _LOGGER.error("Can only generate events under esphome " + "domain!") + return + hass.bus.async_fire(service.service, service_data) + else: + hass.async_create_task(hass.services.async_call( + domain, service_name, service_data, blocking=True)) async def send_home_assistant_state(entity_id: str, _, new_state: Optional[State]) -> None: @@ -222,7 +231,7 @@ async def _async_setup_device_registry(hass: HomeAssistantType, entry: ConfigEntry, device_info: DeviceInfo): """Set up device registry feature for a particular config entry.""" - sw_version = device_info.esphome_core_version + sw_version = device_info.esphome_version if device_info.compilation_time: sw_version += ' ({})'.format(device_info.compilation_time) device_registry = await dr.async_get_registry(hass) @@ -249,6 +258,10 @@ async def _register_service(hass: HomeAssistantType, UserServiceArgType.INT: vol.Coerce(int), UserServiceArgType.FLOAT: vol.Coerce(float), UserServiceArgType.STRING: cv.string, + UserServiceArgType.BOOL_ARRAY: [cv.boolean], + UserServiceArgType.INT_ARRAY: [vol.Coerce(int)], + UserServiceArgType.FLOAT_ARRAY: [vol.Coerce(float)], + UserServiceArgType.STRING_ARRAY: [cv.string], }[arg.type_] async def execute_service(call): diff --git a/homeassistant/components/esphome/manifest.json b/homeassistant/components/esphome/manifest.json index a986a864189..43987cce2c9 100644 --- a/homeassistant/components/esphome/manifest.json +++ b/homeassistant/components/esphome/manifest.json @@ -4,7 +4,7 @@ "config_flow": true, "documentation": "https://www.home-assistant.io/components/esphome", "requirements": [ - "aioesphomeapi==2.1.0" + "aioesphomeapi==2.2.0" ], "dependencies": [], "zeroconf": ["_esphomelib._tcp.local."], diff --git a/requirements_all.txt b/requirements_all.txt index 6fe24a40c82..a820ce2b61f 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -129,7 +129,7 @@ aiobotocore==0.10.2 aiodns==2.0.0 # homeassistant.components.esphome -aioesphomeapi==2.1.0 +aioesphomeapi==2.2.0 # homeassistant.components.freebox aiofreepybox==0.0.8 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 7fe8c1a8b0b..651053a14dd 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -48,7 +48,7 @@ aioautomatic==0.6.5 aiobotocore==0.10.2 # homeassistant.components.esphome -aioesphomeapi==2.1.0 +aioesphomeapi==2.2.0 # homeassistant.components.emulated_hue # homeassistant.components.http