Add ESPHome event generation and user-defined service array support (#24595)

* Add ESPHome event generation and user-defined service array support

* Comments

* Lint
This commit is contained in:
Otto Winter 2019-06-18 17:43:11 +02:00 committed by GitHub
parent ee5540f351
commit 024ce0e8eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 21 additions and 8 deletions

View File

@ -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):

View File

@ -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."],

View File

@ -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

View File

@ -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