Fix service annotations (#31402)

* Fix service annotations

* Filter area_id from service data

* Fix services not accepting entities

* Typo
This commit is contained in:
Paulus Schoutsen committed 2020-02-02 15:39:37 -08:00
1 parent fb26dd3028
commit 5b7a65c5ea
5 files changed
+78 -29

No files matched your search

@@ -143,11 +143,15 @@ async def async_setup(hass: HomeAssistantType, config: ConfigType) -> bool:
)
component.async_register_entity_service(
SERVICE_SELECT_NEXT, {}, lambda entity, call: entity.async_offset_index(1)
SERVICE_SELECT_NEXT,
{},
callback(lambda entity, call: entity.async_offset_index(1)),
)
component.async_register_entity_service(
SERVICE_SELECT_PREVIOUS, {}, lambda entity, call: entity.async_offset_index(-1)
SERVICE_SELECT_PREVIOUS,
{},
callback(lambda entity, call: entity.async_offset_index(-1)),
)
component.async_register_entity_service(
@@ -248,7 +252,8 @@ class InputSelect(RestoreEntity):
"""Return unique id for the entity."""
return self._config[CONF_ID]
async def async_select_option(self, option):
@callback
def async_select_option(self, option):
"""Select new option."""
if option not in self._options:
_LOGGER.warning(
@@ -260,14 +265,16 @@ class InputSelect(RestoreEntity):
self._current_option = option
self.async_write_ha_state()
async def async_offset_index(self, offset):
@callback
def async_offset_index(self, offset):
"""Offset current index."""
current_index = self._options.index(self._current_option)
new_index = (current_index + offset) % len(self._options)
self._current_option = self._options[new_index]
self.async_write_ha_state()
async def async_set_options(self, options):
@callback
def async_set_options(self, options):
"""Set options."""
self._current_option = options[0]
self._config[CONF_OPTIONS] = options
@@ -173,6 +173,23 @@ SCHEMA_WEBSOCKET_GET_THUMBNAIL = websocket_api.BASE_COMMAND_MESSAGE_SCHEMA.exten
)
def _rename_keys(**keys):
"""Create validator that renames keys.
Necessary because the service schema names do not match the command parameters.
Async friendly.
"""
def rename(value):
for to_key, from_key in keys.items():
if from_key in value:
value[to_key] = value.pop(from_key)
return value
return rename
async def async_setup(hass, config):
"""Track states and offer events for media_players."""
component = hass.data[DOMAIN] = EntityComponent(
@@ -238,30 +255,39 @@ async def async_setup(hass, config):
)
component.async_register_entity_service(
SERVICE_VOLUME_SET,
{vol.Required(ATTR_MEDIA_VOLUME_LEVEL): cv.small_float},
lambda entity, call: entity.async_set_volume_level(
volume=call.data[ATTR_MEDIA_VOLUME_LEVEL]
vol.All(
cv.make_entity_service_schema(
{vol.Required(ATTR_MEDIA_VOLUME_LEVEL): cv.small_float}
),
_rename_keys(volume=ATTR_MEDIA_VOLUME_LEVEL),
),
"async_set_volume_level",
[SUPPORT_VOLUME_SET],
)
component.async_register_entity_service(
SERVICE_VOLUME_MUTE,
{vol.Required(ATTR_MEDIA_VOLUME_MUTED): cv.boolean},
lambda entity, call: entity.async_mute_volume(
mute=call.data[ATTR_MEDIA_VOLUME_MUTED]
vol.All(
cv.make_entity_service_schema(
{vol.Required(ATTR_MEDIA_VOLUME_MUTED): cv.boolean}
),
_rename_keys(mute=ATTR_MEDIA_VOLUME_MUTED),
),
"async_mute_volume",
[SUPPORT_VOLUME_MUTE],
)
component.async_register_entity_service(
SERVICE_MEDIA_SEEK,
{
vol.Required(ATTR_MEDIA_SEEK_POSITION): vol.All(
vol.Coerce(float), vol.Range(min=0)
)
},
lambda entity, call: entity.async_media_seek(
position=call.data[ATTR_MEDIA_SEEK_POSITION]
vol.All(
cv.make_entity_service_schema(
{
vol.Required(ATTR_MEDIA_SEEK_POSITION): vol.All(
vol.Coerce(float), vol.Range(min=0)
)
}
),
_rename_keys(position=ATTR_MEDIA_SEEK_POSITION),
),
"async_media_seek",
[SUPPORT_SEEK],
)
component.async_register_entity_service(
@@ -278,12 +304,15 @@ async def async_setup(hass, config):
)
component.async_register_entity_service(
SERVICE_PLAY_MEDIA,
MEDIA_PLAYER_PLAY_MEDIA_SCHEMA,
lambda entity, call: entity.async_play_media(
media_type=call.data[ATTR_MEDIA_CONTENT_TYPE],
media_id=call.data[ATTR_MEDIA_CONTENT_ID],
enqueue=call.data.get(ATTR_MEDIA_ENQUEUE),
vol.All(
cv.make_entity_service_schema(MEDIA_PLAYER_PLAY_MEDIA_SCHEMA),
_rename_keys(
media_type=ATTR_MEDIA_CONTENT_TYPE,
media_id=ATTR_MEDIA_CONTENT_ID,
enqueue=ATTR_MEDIA_ENQUEUE,
),
),
"async_play_media",
[SUPPORT_PLAY_MEDIA],
)
component.async_register_entity_service(