Implement source switching for homekit_controller televisions (#32526)
This commit is contained in:
10 files changed
+177
-16
No files matched your search
@@ -95,6 +95,16 @@ class HomeKitEntity(Entity):
|
||||
continue
|
||||
self._setup_characteristic(char)
|
||||
|
||||
accessory = self._accessory.entity_map.aid(self._aid)
|
||||
this_service = accessory.services.iid(self._iid)
|
||||
for child_service in accessory.services.filter(
|
||||
parent_service=this_service
|
||||
):
|
||||
for char in child_service.characteristics:
|
||||
if char.type not in characteristic_types:
|
||||
continue
|
||||
self._setup_characteristic(char.to_accessory_and_service_list())
|
||||
|
||||
def _setup_characteristic(self, char):
|
||||
"""Configure an entity based on a HomeKit characteristics metadata."""
|
||||
# Build up a list of (aid, iid) tuples to poll on update()
|
||||
|
||||
@@ -8,6 +8,7 @@ from aiohomekit.exceptions import (
|
||||
AccessoryNotFoundError,
|
||||
EncryptionError,
|
||||
)
|
||||
from aiohomekit.model import Accessories
|
||||
from aiohomekit.model.characteristics import CharacteristicsTypes
|
||||
from aiohomekit.model.services import ServicesTypes
|
||||
|
||||
@@ -69,9 +70,11 @@ class HKDevice:
|
||||
self.pairing_data["AccessoryPairingID"], self.pairing_data
|
||||
)
|
||||
|
||||
self.accessories = {}
|
||||
self.accessories = None
|
||||
self.config_num = 0
|
||||
|
||||
self.entity_map = Accessories()
|
||||
|
||||
# A list of callbacks that turn HK service metadata into entities
|
||||
self.listeners = []
|
||||
|
||||
@@ -153,6 +156,8 @@ class HKDevice:
|
||||
self.accessories = cache["accessories"]
|
||||
self.config_num = cache["config_num"]
|
||||
|
||||
self.entity_map = Accessories.from_list(self.accessories)
|
||||
|
||||
self._polling_interval_remover = async_track_time_interval(
|
||||
self.hass, self.async_update, DEFAULT_SCAN_INTERVAL
|
||||
)
|
||||
@@ -213,6 +218,8 @@ class HKDevice:
|
||||
# later when Bonjour spots c# is still not up to date.
|
||||
return False
|
||||
|
||||
self.entity_map = Accessories.from_list(self.accessories)
|
||||
|
||||
self.hass.data[ENTITY_MAP].async_create_or_update_map(
|
||||
self.unique_id, config_num, self.accessories
|
||||
)
|
||||
@@ -318,6 +325,10 @@ class HKDevice:
|
||||
accessory = self.current_state.setdefault(aid, {})
|
||||
accessory[cid] = value
|
||||
|
||||
# self.current_state will be replaced by entity_map in a future PR
|
||||
# For now we update both
|
||||
self.entity_map.process_changes(new_values_dict)
|
||||
|
||||
self.hass.helpers.dispatcher.async_dispatcher_send(self.signal_state_updated)
|
||||
|
||||
async def get_characteristics(self, *args, **kwargs):
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
"name": "HomeKit Controller",
|
||||
"config_flow": true,
|
||||
"documentation": "https://www.home-assistant.io/integrations/homekit_controller",
|
||||
"requirements": ["aiohomekit[IP]==0.2.17"],
|
||||
"requirements": ["aiohomekit[IP]==0.2.21"],
|
||||
"dependencies": [],
|
||||
"zeroconf": ["_hap._tcp.local."],
|
||||
"codeowners": ["@Jc2k"]
|
||||
|
||||
@@ -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)
|
||||
Reference in new issue
Block a user