mirror of
https://github.com/home-assistant/core.git
synced 2026-12-05 21:29:58 -04:00
Move xiaomi_miio remote service registrations to services module (#184805)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
34b5f0c595
commit
c985c4d32a
@@ -23,6 +23,7 @@ CONF_CLOUD_USERNAME = "cloud_username"
|
||||
CONF_CLOUD_PASSWORD = "cloud_password"
|
||||
CONF_CLOUD_COUNTRY = "cloud_country"
|
||||
CONF_MANUAL = "manual"
|
||||
CONF_SLOT = "slot"
|
||||
|
||||
# Options flow
|
||||
CONF_CLOUD_SUBDEVICES = "cloud_subdevices"
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
"""Support for the Xiaomi IR Remote (Chuangmi IR)."""
|
||||
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
import time
|
||||
from typing import Any, override
|
||||
@@ -9,7 +7,6 @@ from typing import Any, override
|
||||
from miio import ChuangmiIr, DeviceException
|
||||
import probatio
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
from homeassistant.components.remote import (
|
||||
ATTR_DELAY_SECS,
|
||||
ATTR_NUM_REPEATS,
|
||||
@@ -26,18 +23,16 @@ from homeassistant.const import (
|
||||
)
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.exceptions import PlatformNotReady
|
||||
from homeassistant.helpers import config_validation as cv, entity_platform
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .const import SERVICE_LEARN, SERVICE_SET_REMOTE_LED_OFF, SERVICE_SET_REMOTE_LED_ON
|
||||
from .const import CONF_SLOT
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DATA_KEY = "remote.xiaomi_miio"
|
||||
|
||||
CONF_SLOT = "slot"
|
||||
CONF_COMMANDS = "commands"
|
||||
|
||||
DEFAULT_TIMEOUT = 10
|
||||
@@ -114,71 +109,6 @@ async def async_setup_platform(
|
||||
|
||||
async_add_entities([xiaomi_miio_remote])
|
||||
|
||||
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."""
|
||||
device = entity.device
|
||||
|
||||
slot = service.data.get(CONF_SLOT, entity.slot)
|
||||
|
||||
await hass.async_add_executor_job(device.learn, slot)
|
||||
|
||||
timeout = service.data.get(CONF_TIMEOUT, entity.timeout)
|
||||
|
||||
_LOGGER.info("Press the key you want Home Assistant to learn")
|
||||
start_time = utcnow()
|
||||
while (utcnow() - start_time) < timedelta(seconds=timeout):
|
||||
message = await hass.async_add_executor_job(device.read, slot)
|
||||
_LOGGER.debug("Message received from device: '%s'", message)
|
||||
|
||||
if code := message.get("code"):
|
||||
log_msg = f"Received command is: {code}"
|
||||
_LOGGER.info(log_msg)
|
||||
persistent_notification.async_create(
|
||||
hass, log_msg, title="Xiaomi Miio Remote"
|
||||
)
|
||||
return
|
||||
|
||||
if "error" in message and message["error"]["message"] == "learn timeout":
|
||||
await hass.async_add_executor_job(device.learn, slot)
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
_LOGGER.error("Timeout. No infrared command captured")
|
||||
persistent_notification.async_create(
|
||||
hass, "Timeout. No infrared command captured", title="Xiaomi Miio Remote"
|
||||
)
|
||||
|
||||
platform = entity_platform.async_get_current_platform()
|
||||
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_LEARN,
|
||||
{
|
||||
probatio.Optional(CONF_TIMEOUT, default=10): cv.positive_int,
|
||||
probatio.Optional(CONF_SLOT, default=1): probatio.All(
|
||||
int, probatio.Range(min=1, max=1000000)
|
||||
),
|
||||
},
|
||||
async_service_learn_handler,
|
||||
)
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SET_REMOTE_LED_ON,
|
||||
None,
|
||||
async_service_led_on_handler,
|
||||
)
|
||||
platform.async_register_entity_service(
|
||||
SERVICE_SET_REMOTE_LED_OFF,
|
||||
None,
|
||||
async_service_led_off_handler,
|
||||
)
|
||||
|
||||
|
||||
class XiaomiMiioRemote(RemoteEntity):
|
||||
"""Representation of a Xiaomi Miio Remote device."""
|
||||
|
||||
@@ -1,25 +1,32 @@
|
||||
"""Xiaomi services."""
|
||||
|
||||
import asyncio
|
||||
from collections.abc import Callable, Coroutine
|
||||
from datetime import timedelta
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
import probatio
|
||||
|
||||
from homeassistant.components import persistent_notification
|
||||
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
|
||||
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
|
||||
from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN
|
||||
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
|
||||
from homeassistant.components.vacuum import DOMAIN as VACUUM_DOMAIN
|
||||
from homeassistant.const import ATTR_MODE
|
||||
from homeassistant.const import ATTR_MODE, CONF_TIMEOUT
|
||||
from homeassistant.core import HomeAssistant, ServiceCall, callback
|
||||
from homeassistant.helpers import config_validation as cv, service
|
||||
from homeassistant.helpers.entity import Entity
|
||||
from homeassistant.util.dt import utcnow
|
||||
|
||||
from .const import (
|
||||
ATTR_SCENE,
|
||||
CONF_SLOT,
|
||||
DOMAIN,
|
||||
SERVICE_EYECARE_MODE_OFF,
|
||||
SERVICE_EYECARE_MODE_ON,
|
||||
SERVICE_LEARN,
|
||||
SERVICE_NIGHT_LIGHT_MODE_OFF,
|
||||
SERVICE_NIGHT_LIGHT_MODE_ON,
|
||||
SERVICE_REMINDER_OFF,
|
||||
@@ -29,6 +36,8 @@ from .const import (
|
||||
SERVICE_SET_EXTRA_FEATURES,
|
||||
SERVICE_SET_POWER_MODE,
|
||||
SERVICE_SET_POWER_PRICE,
|
||||
SERVICE_SET_REMOTE_LED_OFF,
|
||||
SERVICE_SET_REMOTE_LED_ON,
|
||||
SERVICE_SET_SCENE,
|
||||
SERVICE_SET_WIFI_LED_OFF,
|
||||
SERVICE_SET_WIFI_LED_ON,
|
||||
@@ -79,6 +88,56 @@ def _async_service_method(
|
||||
return _async_call_method
|
||||
|
||||
|
||||
async def _async_remote_led_off(entity, service_call: ServiceCall) -> None:
|
||||
"""Handle set_led_off command."""
|
||||
await service_call.hass.async_add_executor_job(
|
||||
entity.device.set_indicator_led, False
|
||||
)
|
||||
|
||||
|
||||
async def _async_remote_led_on(entity, service_call: ServiceCall) -> None:
|
||||
"""Handle set_led_on command."""
|
||||
await service_call.hass.async_add_executor_job(
|
||||
entity.device.set_indicator_led, True
|
||||
)
|
||||
|
||||
|
||||
async def _async_remote_learn(entity, service_call: ServiceCall) -> None:
|
||||
"""Handle a learn command."""
|
||||
hass = service_call.hass
|
||||
device = entity.device
|
||||
|
||||
slot = service_call.data.get(CONF_SLOT, entity.slot)
|
||||
|
||||
await hass.async_add_executor_job(device.learn, slot)
|
||||
|
||||
timeout = service_call.data.get(CONF_TIMEOUT, entity.timeout)
|
||||
|
||||
_LOGGER.info("Press the key you want Home Assistant to learn")
|
||||
start_time = utcnow()
|
||||
while (utcnow() - start_time) < timedelta(seconds=timeout):
|
||||
message = await hass.async_add_executor_job(device.read, slot)
|
||||
_LOGGER.debug("Message received from device: '%s'", message)
|
||||
|
||||
if code := message.get("code"):
|
||||
log_msg = f"Received command is: {code}"
|
||||
_LOGGER.info(log_msg)
|
||||
persistent_notification.async_create(
|
||||
hass, log_msg, title="Xiaomi Miio Remote"
|
||||
)
|
||||
return
|
||||
|
||||
if "error" in message and message["error"]["message"] == "learn timeout":
|
||||
await hass.async_add_executor_job(device.learn, slot)
|
||||
|
||||
await asyncio.sleep(1)
|
||||
|
||||
_LOGGER.error("Timeout. No infrared command captured")
|
||||
persistent_notification.async_create(
|
||||
hass, "Timeout. No infrared command captured", title="Xiaomi Miio Remote"
|
||||
)
|
||||
|
||||
|
||||
@callback
|
||||
def async_setup_services(hass: HomeAssistant) -> None:
|
||||
"""Set up services."""
|
||||
@@ -176,6 +235,37 @@ def async_setup_services(hass: HomeAssistant) -> None:
|
||||
func=_async_service_method("async_set_extra_features", ATTR_FEATURES),
|
||||
)
|
||||
|
||||
# Remote Services
|
||||
service.async_register_platform_entity_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_LEARN,
|
||||
entity_domain=REMOTE_DOMAIN,
|
||||
func=_async_remote_learn,
|
||||
schema={
|
||||
probatio.Optional(CONF_TIMEOUT, default=10): cv.positive_int,
|
||||
probatio.Optional(CONF_SLOT, default=1): probatio.All(
|
||||
int, probatio.Range(min=1, max=1000000)
|
||||
),
|
||||
},
|
||||
)
|
||||
service.async_register_platform_entity_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_SET_REMOTE_LED_ON,
|
||||
entity_domain=REMOTE_DOMAIN,
|
||||
func=_async_remote_led_on,
|
||||
schema=None,
|
||||
)
|
||||
service.async_register_platform_entity_service(
|
||||
hass,
|
||||
DOMAIN,
|
||||
SERVICE_SET_REMOTE_LED_OFF,
|
||||
entity_domain=REMOTE_DOMAIN,
|
||||
func=_async_remote_led_off,
|
||||
schema=None,
|
||||
)
|
||||
|
||||
# Vacuum Services
|
||||
service.async_register_platform_entity_service(
|
||||
hass,
|
||||
|
||||
Reference in New Issue
Block a user