Add xiaomi miio services remote_set_led_on/off (#35805)

* Add services remote_set_led_on/remote_set_led_off to control changmi_ir led

* pylint: elif => if

* Fix services.async_register => platform.async_register_entity_service

* Update homeassistant/components/xiaomi_miio/remote.py

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>

* more fixes

* async

* more fixes

* fix

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
pull/35833/head
Boris Kaplounovsky 2020-05-20 01:23:18 +03:00 committed by GitHub
parent 648df6d984
commit 37f9b24a85
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 44 additions and 28 deletions

View File

@ -34,6 +34,8 @@ SERVICE_EYECARE_MODE_OFF = "light_eyecare_mode_off"
# Remote Services
SERVICE_LEARN = "remote_learn_command"
SERVICE_SET_LED_ON = "remote_set_led_on"
SERVICE_SET_LED_OFF = "remote_set_led_off"
# Switch Services
SERVICE_SET_WIFI_LED_ON = "switch_set_wifi_led_on"

View File

@ -15,7 +15,6 @@ from homeassistant.components.remote import (
RemoteEntity,
)
from homeassistant.const import (
ATTR_ENTITY_ID,
CONF_COMMAND,
CONF_HOST,
CONF_NAME,
@ -23,10 +22,10 @@ from homeassistant.const import (
CONF_TOKEN,
)
from homeassistant.exceptions import PlatformNotReady
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers import config_validation as cv, entity_platform
from homeassistant.util.dt import utcnow
from .const import DOMAIN, SERVICE_LEARN
from .const import SERVICE_LEARN, SERVICE_SET_LED_OFF, SERVICE_SET_LED_ON
_LOGGER = logging.getLogger(__name__)
@ -38,14 +37,6 @@ CONF_COMMANDS = "commands"
DEFAULT_TIMEOUT = 10
DEFAULT_SLOT = 1
LEARN_COMMAND_SCHEMA = vol.Schema(
{
vol.Required(ATTR_ENTITY_ID): vol.All(str),
vol.Optional(CONF_TIMEOUT, default=10): vol.All(int, vol.Range(min=0)),
vol.Optional(CONF_SLOT, default=1): vol.All(int, vol.Range(min=1, max=1000000)),
}
)
COMMAND_SCHEMA = vol.Schema(
{vol.Required(CONF_COMMAND): vol.All(cv.ensure_list, [cv.string])}
)
@ -112,22 +103,16 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities([xiaomi_miio_remote])
async def async_service_handler(service):
async def async_service_led_off_handler(entity, service):
"""Handle set_led_off command."""
await hass.async_add_executor_job(entity.device.set_indicator_led, False)
async def async_service_led_on_handler(entity, service):
"""Handle set_led_on command."""
await hass.async_add_executor_job(entity.device.set_indicator_led, True)
async def async_service_learn_handler(entity, service):
"""Handle a learn command."""
if service.service != SERVICE_LEARN:
_LOGGER.error("We should not handle service: %s", service.service)
return
entity_id = service.data.get(ATTR_ENTITY_ID)
entity = None
for remote in hass.data[DATA_KEY].values():
if remote.entity_id == entity_id:
entity = remote
if not entity:
_LOGGER.error("entity_id: '%s' not found", entity_id)
return
device = entity.device
slot = service.data.get(CONF_SLOT, entity.slot)
@ -160,8 +145,23 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
"Timeout. No infrared command captured", title="Xiaomi Miio Remote"
)
hass.services.async_register(
DOMAIN, SERVICE_LEARN, async_service_handler, schema=LEARN_COMMAND_SCHEMA
platform = entity_platform.current_platform.get()
platform.async_register_entity_service(
SERVICE_LEARN,
{
vol.Optional(CONF_TIMEOUT, default=10): vol.All(int, vol.Range(min=0)),
vol.Optional(CONF_SLOT, default=1): vol.All(
int, vol.Range(min=1, max=1000000)
),
},
async_service_learn_handler,
)
platform.async_register_entity_service(
SERVICE_SET_LED_ON, {}, async_service_led_on_handler,
)
platform.async_register_entity_service(
SERVICE_SET_LED_OFF, {}, async_service_led_off_handler,
)

View File

@ -224,6 +224,20 @@ remote_learn_command:
description: "Define the timeout in seconds, before which the command must be learned."
example: "30"
remote_set_led_on:
description: 'Turn on blue LED.'
fields:
entity_id:
description: "Name of the entity to turn LED on."
example: "remote.xiaomi_miio"
remote_set_led_off:
description: 'Turn off blue LED.'
fields:
entity_id:
description: "Name of the entity to turn LED off."
example: "remote.xiaomi_miio"
switch_set_wifi_led_on:
description: Turn the wifi led on.
fields: