Implement source switching for homekit_controller televisions (#32526)
This commit is contained in:
parent
0d667c1bd9
commit
2879081772
10 changed files with 177 additions and 16 deletions
|
@ -7,12 +7,14 @@ from aiohomekit.model.characteristics import (
|
|||
RemoteKeyValues,
|
||||
TargetMediaStateValues,
|
||||
)
|
||||
from aiohomekit.model.services import ServicesTypes
|
||||
from aiohomekit.utils import clamp_enum_to_char
|
||||
|
||||
from homeassistant.components.media_player import DEVICE_CLASS_TV, MediaPlayerDevice
|
||||
from homeassistant.components.media_player.const import (
|
||||
SUPPORT_PAUSE,
|
||||
SUPPORT_PLAY,
|
||||
SUPPORT_SELECT_SOURCE,
|
||||
SUPPORT_STOP,
|
||||
)
|
||||
from homeassistant.const import STATE_IDLE, STATE_PAUSED, STATE_PLAYING
|
||||
|
@ -63,8 +65,15 @@ class HomeKitTelevision(HomeKitEntity, MediaPlayerDevice):
|
|||
CharacteristicsTypes.CURRENT_MEDIA_STATE,
|
||||
CharacteristicsTypes.TARGET_MEDIA_STATE,
|
||||
CharacteristicsTypes.REMOTE_KEY,
|
||||
CharacteristicsTypes.ACTIVE_IDENTIFIER,
|
||||
# Characterics that are on the linked INPUT_SOURCE services
|
||||
CharacteristicsTypes.CONFIGURED_NAME,
|
||||
CharacteristicsTypes.IDENTIFIER,
|
||||
]
|
||||
|
||||
def _setup_active_identifier(self, char):
|
||||
self._features |= SUPPORT_SELECT_SOURCE
|
||||
|
||||
def _setup_target_media_state(self, char):
|
||||
self._supported_target_media_state = clamp_enum_to_char(
|
||||
TargetMediaStateValues, char
|
||||
|
@ -94,6 +103,43 @@ class HomeKitTelevision(HomeKitEntity, MediaPlayerDevice):
|
|||
"""Flag media player features that are supported."""
|
||||
return self._features
|
||||
|
||||
@property
|
||||
def source_list(self):
|
||||
"""List of all input sources for this television."""
|
||||
sources = []
|
||||
|
||||
this_accessory = self._accessory.entity_map.aid(self._aid)
|
||||
this_tv = this_accessory.services.iid(self._iid)
|
||||
|
||||
input_sources = this_accessory.services.filter(
|
||||
service_type=ServicesTypes.INPUT_SOURCE, parent_service=this_tv,
|
||||
)
|
||||
|
||||
for input_source in input_sources:
|
||||
char = input_source[CharacteristicsTypes.CONFIGURED_NAME]
|
||||
sources.append(char.value)
|
||||
return sources
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
"""Name of the current input source."""
|
||||
active_identifier = self.get_hk_char_value(
|
||||
CharacteristicsTypes.ACTIVE_IDENTIFIER
|
||||
)
|
||||
if not active_identifier:
|
||||
return None
|
||||
|
||||
this_accessory = self._accessory.entity_map.aid(self._aid)
|
||||
this_tv = this_accessory.services.iid(self._iid)
|
||||
|
||||
input_source = this_accessory.services.first(
|
||||
service_type=ServicesTypes.INPUT_SOURCE,
|
||||
characteristics={CharacteristicsTypes.IDENTIFIER: active_identifier},
|
||||
parent_service=this_tv,
|
||||
)
|
||||
char = input_source[CharacteristicsTypes.CONFIGURED_NAME]
|
||||
return char.value
|
||||
|
||||
@property
|
||||
def state(self):
|
||||
"""State of the tv."""
|
||||
|
@ -167,3 +213,28 @@ class HomeKitTelevision(HomeKitEntity, MediaPlayerDevice):
|
|||
}
|
||||
]
|
||||
await self._accessory.put_characteristics(characteristics)
|
||||
|
||||
async def async_select_source(self, source):
|
||||
"""Switch to a different media source."""
|
||||
this_accessory = self._accessory.entity_map.aid(self._aid)
|
||||
this_tv = this_accessory.services.iid(self._iid)
|
||||
|
||||
input_source = this_accessory.services.first(
|
||||
service_type=ServicesTypes.INPUT_SOURCE,
|
||||
characteristics={CharacteristicsTypes.CONFIGURED_NAME: source},
|
||||
parent_service=this_tv,
|
||||
)
|
||||
|
||||
if not input_source:
|
||||
raise ValueError(f"Could not find source {source}")
|
||||
|
||||
identifier = input_source[CharacteristicsTypes.IDENTIFIER]
|
||||
|
||||
characteristics = [
|
||||
{
|
||||
"aid": self._aid,
|
||||
"iid": self._chars["active-identifier"],
|
||||
"value": identifier.value,
|
||||
}
|
||||
]
|
||||
await self._accessory.put_characteristics(characteristics)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue