Skip to content

Commit

Permalink
馃摵 add support homekit remote for mitv
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Apr 30, 2022
1 parent 50b70d4 commit e0995ef
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions custom_components/xiaomi_miot/media_player.py
Expand Up @@ -19,6 +19,7 @@
DEVICE_CLASS_RECEIVER,
)
from homeassistant.components.media_player.const import *
from homeassistant.components.homekit.const import EVENT_HOMEKIT_TV_REMOTE_KEY_PRESSED
import homeassistant.helpers.config_validation as cv

from . import (
Expand Down Expand Up @@ -533,6 +534,19 @@ async def async_added_to_hass(self):
add_selects([self._subs[sub]], update_before_add=False)
await self.async_update_apps()

self._vars['homekit_remote_unsub'] = self.hass.bus.async_listen(
EVENT_HOMEKIT_TV_REMOTE_KEY_PRESSED,
self.async_homekit_remote_event_handler,
)

async def async_will_remove_from_hass(self):
"""Run when entity will be removed from hass.
To be extended by integrations.
"""
await super().async_will_remove_from_hass()
if unsub := self._vars.pop('homekit_remote_unsub'):
unsub()

async def async_update(self):
await super().async_update()
if not self._available:
Expand Down Expand Up @@ -605,18 +619,23 @@ def state(self):
return sta

def turn_on(self):
if eid := self.custom_config('bind_xiaoai'):
if self._local_state and self._state_attrs.get('6095_state'):
# tv is on
pass
elif eid := self.custom_config('bind_xiaoai'):
nam = self.device_info.get('name')
nam = self.custom_config('television_name', nam)
sil = self.custom_config_bool('xiaoai_silent', True)
if not nam:
sta = self.hass.states.get(self.entity_id)
nam = sta.attributes.get(ATTR_FRIENDLY_NAME)
if nam and self.hass.states.get(eid):
txt = f'{nam}浜睆' if self._local_state else f'鎵撳紑{nam}'
self.hass.services.call(DOMAIN, 'intelligent_speaker', {
'entity_id': eid,
'text': f'鎵撳紑{nam}',
'text': txt,
'execute': True,
'silent': self.custom_config_bool('xiaoai_silent', True),
'silent': sil,
})
return super().turn_on()

Expand Down Expand Up @@ -685,6 +704,24 @@ def press_key(self, key, **kwargs):
}
return self.request_mitv_api('controller', params=pms)

async def async_homekit_remote_event_handler(self, event):
eid = event.data.get('entity_id')
if eid != self.entity_id:
return
dic = {
'arrow_up': 'up',
'arrow_down': 'down',
'arrow_left': 'left',
'arrow_right': 'right',
'back': 'back',
'select': 'enter',
'information': 'menu',
}
key = dic.get(event.data.get('key_name', ''))
if not key:
return
return self.hass.async_add_executor_job(self.press_key, key)

def with_opaque(self, pms: dict, token=None):
if token is None:
token = self._api_key
Expand Down

1 comment on commit e0995ef

@al-one
Copy link
Owner Author

@al-one al-one commented on e0995ef Apr 30, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

1651328532323622.mp4

Please sign in to comment.