Skip to content

Commit

Permalink
Add ESPHome event generation and user-defined service array support (#…
Browse files Browse the repository at this point in the history
…24595)

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

* Comments

* Lint
  • Loading branch information
OttoWinter committed Jun 18, 2019
1 parent ee5540f commit 024ce0e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 8 deletions.
23 changes: 18 additions & 5 deletions homeassistant/components/esphome/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

from aioesphomeapi import (
APIClient, APIConnectionError, DeviceInfo, EntityInfo, EntityState,
ServiceCall, UserService, UserServiceArgType)
HomeassistantServiceCall, UserService, UserServiceArgType)
import voluptuous as vol

from homeassistant import const
Expand Down Expand Up @@ -86,7 +86,7 @@ def async_on_state(state: EntityState) -> None:
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
Expand All @@ -102,8 +102,17 @@ def async_on_service_call(service: ServiceCall) -> None:
_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:
Expand Down Expand Up @@ -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)
Expand All @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/components/esphome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -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."],
Expand Down
2 changes: 1 addition & 1 deletion requirements_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion requirements_test_all.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 024ce0e

Please sign in to comment.