Skip to content

Commit

Permalink
📺 add option source_list_append for mitv
Browse files Browse the repository at this point in the history
  • Loading branch information
al-one committed Apr 30, 2022
1 parent 95a3ba6 commit 50b70d4
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 3 deletions.
1 change: 1 addition & 0 deletions custom_components/xiaomi_miot/core/miot_local_devices.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@
'dreame.vacuum.p2259',
'fawad.airrtc.30011',
'fawad.airrtc.30012',
'fengmi.projector.fm05',
'fengmi.projector.l176',
'fengmi.projector.l176jp',
'fengmi.projector.l185',
Expand Down
50 changes: 47 additions & 3 deletions custom_components/xiaomi_miot/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ def __init__(self, miot_service: MiotService):
self._attr_source_list = self._prop_input.list_descriptions()
if self._prop_volume:
self._supported_features |= SUPPORT_VOLUME_SET
self._supported_features |= SUPPORT_VOLUME_STEP
if self._prop_mute:
self._supported_features |= SUPPORT_VOLUME_MUTE
if self._act_turn_on:
Expand Down Expand Up @@ -274,6 +275,7 @@ def source(self):
return None

def select_source(self, source):
"""Select input source."""
val = self._prop_input.list_value(source)
if val is not None:
return self.set_property(self._prop_input, val)
Expand Down Expand Up @@ -505,10 +507,22 @@ def __init__(self, config: dict, miot_service: MiotService):
'volumeup',
'volumedown',
]
self._apps = {}
self._supported_features |= SUPPORT_PLAY_MEDIA

async def async_added_to_hass(self):
await super().async_added_to_hass()
if sva := self.custom_config_list('sources_via_apps'):
if not self.custom_config_bool('source_list_append', True):
self._attr_source_list = []
self._attr_source_list.extend(sva)
self._vars['sources_via_apps'] = sva
if svk := self.custom_config_list('sources_via_keycodes'):
if not sva:
self._attr_source_list = []
self._attr_source_list.extend(svk)
self._vars['sources_via_keycodes'] = svk

if add_selects := self._add_entities.get('select'):
from .select import SelectSubEntity
sub = 'keycodes'
Expand Down Expand Up @@ -562,13 +576,13 @@ async def async_update_apps(self):
}
rdt = await self.async_request_mitv_api('controller', params=pms)
if lst := rdt.get('AppInfo', []):
ias = {
self._apps = {
a.get('PackageName'): a.get('AppName')
for a in lst
}
als = [
f'{v} - {k}'
for k, v in ias.items()
for k, v in self._apps.items()
]
add_selects = self._add_entities.get('select')
sub = 'apps'
Expand Down Expand Up @@ -622,11 +636,41 @@ def play_media(self, media_type, media_id, **kwargs):
'sign': hashlib.md5(f'mitvsignsalt{media_id}{self._api_key}{tim[-5:]}'.encode()).hexdigest(),
}
rdt = self.request_mitv_api('controller', params=pms)
self.logger.debug('%s: Play media: %s', self.name_model, [pms, rdt])
self.logger.info('%s: Play media: %s', self.name_model, [pms, rdt])
return not not rdt

@property
def source(self):
"""Name of the current input source."""
if self.app_name in self._vars.get('sources_via_apps', []):
return self.app_name
return super().source

def select_source(self, source):
"""Select input source."""
if source in self._apps:
return self.start_app(self._apps[source])
if source in self._apps.values():
return self.start_app(source)
if source in self._keycodes:
ret = self.press_key(source)
self._attr_app_name = source
self.async_write_ha_state()
return ret
if source in self.source_list:
return super().select_source(source)
return False

def start_app(self, app, **kwargs):
pkg = f'{app}'.split(' - ').pop(-1).strip()
if pkg not in self._apps:
pkg = None
for k, v in self._apps.items():
if v == app:
pkg = k
break
if pkg is None:
return False
pms = {
'action': 'startapp',
'type': 'packagename',
Expand Down

1 comment on commit 50b70d4

@al-one
Copy link
Owner Author

@al-one al-one commented on 50b70d4 Apr 30, 2022

Choose a reason for hiding this comment

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

添加自定义属性

# configuration.yaml
xiaomi_miot:
  device_customizes:

    'xiaomi.tv.*':
      # source_list_append: true # 为false时覆盖输入源,true时在输入源后面追加,默认为true
      sources_via_apps: 桌面,相册,百度网盘,爱看TV,星直播,设置
      sources_via_keycodes: menu,enter,back,up,down,left,right,volumeup,volumedown

    fengmi.projector.fm05:
      sources_via_apps: 桌面,相册,百度网盘,爱看TV,星直播,设置
      sources_via_keycodes: menu,enter,back

xiaomi miot mitv

Please sign in to comment.