Skip to content

Commit

Permalink
🕹 add learn_command for remote (#276)
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Sep 13, 2024
1 parent a113a45 commit 00de971
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions custom_components/xiaomi_miot/remote.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""Support remote entity for Xiaomi Miot."""
import logging
import asyncio
import time
from functools import partial

from homeassistant.const import (
CONF_HOST,
CONF_TOKEN,
)
from homeassistant.components import remote
from homeassistant.components import remote, persistent_notification
from homeassistant.components.remote import (
DOMAIN as ENTITY_DOMAIN,
RemoteEntity,
Expand Down Expand Up @@ -174,15 +175,32 @@ async def async_send_command(self, command, **kwargs):
partial(self.send_remote_command, command, **kwargs)
)

def learn_command(self, **kwargs):
async def async_learn_command(self, **kwargs):
"""Learn a command from a device."""
timeout = int(kwargs.get(remote.ATTR_TIMEOUT) or 30)
res = {}
try:
key = int(kwargs.get(remote.ATTR_DEVICE))
return self._device.learn(key)
key = int(kwargs.get(remote.ATTR_DEVICE, 999999))
for idx in range(timeout):
if idx == 0:
await self.hass.async_add_executor_job(self._device.learn, key)
await asyncio.sleep(1)
res = await self.hass.async_add_executor_job(self._device.read, key)
if isinstance(res, dict) and res.get('code'):
break
except (TypeError, ValueError, DeviceException) as exc:
self.logger.warning('%s: Learn command failed: %s, the device ID is used to store command '
'and must between 1 and 1000000.', self.name_model, exc)
return False
res = {'error': f'{exc}'}
self.logger.warning(
'%s: Learn command failed, the device ID must between 1 and 1000000. %s',
self.name_model, exc,
)
persistent_notification.async_create(
self.hass,
f'{res}',
'Remote learn result',
f'{DOMAIN}-remote-learn',
)
return res

def delete_command(self, **kwargs):
"""Delete commands from the database."""
Expand Down

0 comments on commit 00de971

Please # to comment.