Add toggle to remotes (#8483)

* Add toggle to remotes

* Only include activity if specified, and add service description
This commit is contained in:
Alan Fischer 2017-07-31 11:46:12 -06:00 committed by Martin Hjelmare
parent 91b062f9b7
commit e6be560e00
5 changed files with 44 additions and 10 deletions

View File

@ -18,7 +18,8 @@ from homeassistant.helpers.entity_component import EntityComponent
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.const import ( from homeassistant.const import (
STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, ATTR_ENTITY_ID) STATE_ON, SERVICE_TURN_ON, SERVICE_TURN_OFF, SERVICE_TOGGLE,
ATTR_ENTITY_ID)
from homeassistant.components import group from homeassistant.components import group
from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa from homeassistant.helpers.config_validation import PLATFORM_SCHEMA # noqa
@ -51,7 +52,7 @@ REMOTE_SERVICE_SCHEMA = vol.Schema({
vol.Required(ATTR_ENTITY_ID): cv.entity_ids, vol.Required(ATTR_ENTITY_ID): cv.entity_ids,
}) })
REMOTE_SERVICE_TURN_ON_SCHEMA = REMOTE_SERVICE_SCHEMA.extend({ REMOTE_SERVICE_ACTIVITY_SCHEMA = REMOTE_SERVICE_SCHEMA.extend({
vol.Optional(ATTR_ACTIVITY): cv.string vol.Optional(ATTR_ACTIVITY): cv.string
}) })
@ -80,12 +81,31 @@ def turn_on(hass, activity=None, entity_id=None):
@bind_hass @bind_hass
def turn_off(hass, entity_id=None): def turn_off(hass, activity=None, entity_id=None):
"""Turn all or specified remote off.""" """Turn all or specified remote off."""
data = {ATTR_ENTITY_ID: entity_id} if entity_id else None data = {}
if activity:
data[ATTR_ACTIVITY] = activity
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_TURN_OFF, data) hass.services.call(DOMAIN, SERVICE_TURN_OFF, data)
@bind_hass
def toggle(hass, activity=None, entity_id=None):
"""Toggle all or specified remote."""
data = {}
if activity:
data[ATTR_ACTIVITY] = activity
if entity_id:
data[ATTR_ENTITY_ID] = entity_id
hass.services.call(DOMAIN, SERVICE_TOGGLE, data)
@bind_hass @bind_hass
def send_command(hass, device, command, entity_id=None, def send_command(hass, device, command, entity_id=None,
num_repeats=None, delay_secs=None): num_repeats=None, delay_secs=None):
@ -124,12 +144,14 @@ def async_setup(hass, config):
for remote in target_remotes: for remote in target_remotes:
if service.service == SERVICE_TURN_ON: if service.service == SERVICE_TURN_ON:
yield from remote.async_turn_on(activity=activity_id) yield from remote.async_turn_on(activity=activity_id)
elif service.service == SERVICE_TOGGLE:
yield from remote.async_toggle(activity=activity_id)
elif service.service == SERVICE_SEND_COMMAND: elif service.service == SERVICE_SEND_COMMAND:
yield from remote.async_send_command( yield from remote.async_send_command(
device=device, command=command, device=device, command=command,
num_repeats=num_repeats, delay_secs=delay_secs) num_repeats=num_repeats, delay_secs=delay_secs)
else: else:
yield from remote.async_turn_off() yield from remote.async_turn_off(activity=activity_id)
update_tasks = [] update_tasks = []
for remote in target_remotes: for remote in target_remotes:
@ -152,11 +174,15 @@ def async_setup(hass, config):
hass.services.async_register( hass.services.async_register(
DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service, DOMAIN, SERVICE_TURN_OFF, async_handle_remote_service,
descriptions.get(SERVICE_TURN_OFF), descriptions.get(SERVICE_TURN_OFF),
schema=REMOTE_SERVICE_SCHEMA) schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
hass.services.async_register( hass.services.async_register(
DOMAIN, SERVICE_TURN_ON, async_handle_remote_service, DOMAIN, SERVICE_TURN_ON, async_handle_remote_service,
descriptions.get(SERVICE_TURN_ON), descriptions.get(SERVICE_TURN_ON),
schema=REMOTE_SERVICE_TURN_ON_SCHEMA) schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
hass.services.async_register(
DOMAIN, SERVICE_TOGGLE, async_handle_remote_service,
descriptions.get(SERVICE_TOGGLE),
schema=REMOTE_SERVICE_ACTIVITY_SCHEMA)
hass.services.async_register( hass.services.async_register(
DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service, DOMAIN, SERVICE_SEND_COMMAND, async_handle_remote_service,
descriptions.get(SERVICE_SEND_COMMAND), descriptions.get(SERVICE_SEND_COMMAND),

View File

@ -68,7 +68,7 @@ class AppleTVRemote(remote.RemoteDevice):
self._power.set_power_on(True) self._power.set_power_on(True)
@asyncio.coroutine @asyncio.coroutine
def async_turn_off(self): def async_turn_off(self, **kwargs):
"""Turn the device off. """Turn the device off.
This method is a coroutine. This method is a coroutine.

View File

@ -202,7 +202,7 @@ class HarmonyRemote(remote.RemoteDevice):
else: else:
_LOGGER.error("No activity specified with turn_on service") _LOGGER.error("No activity specified with turn_on service")
def turn_off(self): def turn_off(self, **kwargs):
"""Start the PowerOff activity.""" """Start the PowerOff activity."""
import pyharmony import pyharmony
pyharmony.ha_power_off(self._token, self.host, self._port) pyharmony.ha_power_off(self._token, self.host, self._port)

View File

@ -102,7 +102,7 @@ class ITachIP2IRRemote(remote.RemoteDevice):
self.itachip2ir.send(self._name, "ON", 1) self.itachip2ir.send(self._name, "ON", 1)
self.schedule_update_ha_state() self.schedule_update_ha_state()
def turn_off(self): def turn_off(self, **kwargs):
"""Turn the device off.""" """Turn the device off."""
self._power = False self._power = False
self.itachip2ir.send(self._name, "OFF", 1) self.itachip2ir.send(self._name, "OFF", 1)

View File

@ -11,6 +11,14 @@ turn_on:
description: Activity ID or Activity Name to start description: Activity ID or Activity Name to start
example: 'BedroomTV' example: 'BedroomTV'
toggle:
description: Toggles a device
fields:
entity_id:
description: Name(s) of entities to toggle
example: 'remote.family_room'
turn_off: turn_off:
description: Sends the Power Off Command description: Sends the Power Off Command