From f59c0a5fea7189c3da6e10addbaf131f0c8a1330 Mon Sep 17 00:00:00 2001 From: Heyxk Date: Sun, 20 Aug 2023 02:27:09 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=8E=B5=20add=20support=20play=20media=20f?= =?UTF-8?q?or=20xiaoai=20(#1261)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 - custom_components/xiaomi_miot/media_player.py | 19 ++++++++++++++++--- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/.gitignore b/.gitignore index b22ec9270..2483976dc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,2 @@ .idea/ __pycache__/ - diff --git a/custom_components/xiaomi_miot/media_player.py b/custom_components/xiaomi_miot/media_player.py index e67535e4d..9cf5ba799 100644 --- a/custom_components/xiaomi_miot/media_player.py +++ b/custom_components/xiaomi_miot/media_player.py @@ -482,6 +482,20 @@ def turn_off(self): return self.call_action(self._act_turn_off) return False + async def async_play_media(self, media_type, media_id, **kwargs): + if not self.xiaoai_device: + return + aid = self.xiaoai_device.get('deviceID') + api = 'https://api2.mina.mi.com/remote/ubus' + dat = { + 'deviceId': aid, + 'path': 'mediaplayer', + 'method': 'player_play_url', + 'message': json.dumps({'url': media_id, 'type': 1, 'media': 'app_ios'}), + } + rdt = await self.xiaoai_cloud.async_request_api(api, data=dat, method='POST') or {} + self.logger.info('%s: Play media: %s', self.name_model, [dat, rdt]) + def intelligent_speaker(self, text, execute=False, silent=False, **kwargs): if srv := self._intelligent_speaker: anm = 'execute_text_directive' if execute else 'play_text' @@ -741,7 +755,7 @@ def turn_off(self): return self.call_action(act, ['熄屏']) return super().turn_off() - def play_media(self, media_type, media_id, **kwargs): + async def async_play_media(self, media_type, media_id, **kwargs): """Play a piece of media.""" tim = str(int(time.time() * 1000)) pms = { @@ -752,9 +766,8 @@ def play_media(self, media_type, media_id, **kwargs): 'ts': tim, 'sign': hashlib.md5(f'mitvsignsalt{media_id}{self._api_key}{tim[-5:]}'.encode()).hexdigest(), } - rdt = self.request_mitv_api('controller', params=pms) + rdt = await self.async_request_mitv_api('controller', params=pms) self.logger.info('%s: Play media: %s', self.name_model, [pms, rdt]) - return not not rdt @property def source(self):