diff --git a/homeassistant/components/xiaomi_miio/const.py b/homeassistant/components/xiaomi_miio/const.py index aaeaf19c5f9..77f398aa3ad 100644 --- a/homeassistant/components/xiaomi_miio/const.py +++ b/homeassistant/components/xiaomi_miio/const.py @@ -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" diff --git a/homeassistant/components/xiaomi_miio/remote.py b/homeassistant/components/xiaomi_miio/remote.py index fb188368127..6fbf4b8a0f6 100644 --- a/homeassistant/components/xiaomi_miio/remote.py +++ b/homeassistant/components/xiaomi_miio/remote.py @@ -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, ) diff --git a/homeassistant/components/xiaomi_miio/services.yaml b/homeassistant/components/xiaomi_miio/services.yaml index a5308b08d6d..a92e46f11a1 100644 --- a/homeassistant/components/xiaomi_miio/services.yaml +++ b/homeassistant/components/xiaomi_miio/services.yaml @@ -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: