Fix Onkyo action select_hdmi_output (#125115)

* Fix Onkyo service select_hdmi_output

* Move Hasskey directly under Onkyo domain
This commit is contained in:
Artur Pragacz 2024-09-03 12:43:31 +02:00 committed by Bram Kragten
parent 393a0ac0df
commit be3b16b7fa

View File

@ -11,7 +11,7 @@ import pyeiscp
import voluptuous as vol import voluptuous as vol
from homeassistant.components.media_player import ( from homeassistant.components.media_player import (
DOMAIN, DOMAIN as MEDIA_PLAYER_DOMAIN,
PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA, PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA,
MediaPlayerEntity, MediaPlayerEntity,
MediaPlayerEntityFeature, MediaPlayerEntityFeature,
@ -28,9 +28,14 @@ from homeassistant.core import HomeAssistant, ServiceCall, callback
from homeassistant.helpers import config_validation as cv from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.hass_dict import HassKey
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
DOMAIN = "onkyo"
DATA_MP_ENTITIES: HassKey[list[dict[str, OnkyoMediaPlayer]]] = HassKey(DOMAIN)
CONF_SOURCES = "sources" CONF_SOURCES = "sources"
CONF_MAX_VOLUME = "max_volume" CONF_MAX_VOLUME = "max_volume"
CONF_RECEIVER_MAX_VOLUME = "receiver_max_volume" CONF_RECEIVER_MAX_VOLUME = "receiver_max_volume"
@ -148,6 +153,33 @@ class ReceiverInfo:
identifier: str identifier: str
async def async_register_services(hass: HomeAssistant) -> None:
"""Register Onkyo services."""
async def async_service_handle(service: ServiceCall) -> None:
"""Handle for services."""
entity_ids = service.data[ATTR_ENTITY_ID]
targets: list[OnkyoMediaPlayer] = []
for receiver_entities in hass.data[DATA_MP_ENTITIES]:
targets.extend(
entity
for entity in receiver_entities.values()
if entity.entity_id in entity_ids
)
for target in targets:
if service.service == SERVICE_SELECT_HDMI_OUTPUT:
await target.async_select_output(service.data[ATTR_HDMI_OUTPUT])
hass.services.async_register(
MEDIA_PLAYER_DOMAIN,
SERVICE_SELECT_HDMI_OUTPUT,
async_service_handle,
schema=ONKYO_SELECT_OUTPUT_SCHEMA,
)
async def async_setup_platform( async def async_setup_platform(
hass: HomeAssistant, hass: HomeAssistant,
config: ConfigType, config: ConfigType,
@ -155,29 +187,10 @@ async def async_setup_platform(
discovery_info: DiscoveryInfoType | None = None, discovery_info: DiscoveryInfoType | None = None,
) -> None: ) -> None:
"""Set up the Onkyo platform.""" """Set up the Onkyo platform."""
await async_register_services(hass)
receivers: dict[str, pyeiscp.Connection] = {} # indexed by host receivers: dict[str, pyeiscp.Connection] = {} # indexed by host
entities: dict[str, dict[str, OnkyoMediaPlayer]] = {} # indexed by host and zone all_entities = hass.data.setdefault(DATA_MP_ENTITIES, [])
async def async_service_handle(service: ServiceCall) -> None:
"""Handle for services."""
entity_ids = service.data[ATTR_ENTITY_ID]
targets = [
entity
for h in entities.values()
for entity in h.values()
if entity.entity_id in entity_ids
]
for target in targets:
if service.service == SERVICE_SELECT_HDMI_OUTPUT:
await target.async_select_output(service.data[ATTR_HDMI_OUTPUT])
hass.services.async_register(
DOMAIN,
SERVICE_SELECT_HDMI_OUTPUT,
async_service_handle,
schema=ONKYO_SELECT_OUTPUT_SCHEMA,
)
host = config.get(CONF_HOST) host = config.get(CONF_HOST)
name = config.get(CONF_NAME) name = config.get(CONF_NAME)
@ -188,6 +201,9 @@ async def async_setup_platform(
async def async_setup_receiver( async def async_setup_receiver(
info: ReceiverInfo, discovered: bool, name: str | None info: ReceiverInfo, discovered: bool, name: str | None
) -> None: ) -> None:
entities: dict[str, OnkyoMediaPlayer] = {}
all_entities.append(entities)
@callback @callback
def async_onkyo_update_callback( def async_onkyo_update_callback(
message: tuple[str, str, Any], origin: str message: tuple[str, str, Any], origin: str
@ -199,7 +215,7 @@ async def async_setup_platform(
) )
zone, _, value = message zone, _, value = message
entity = entities[origin].get(zone) entity = entities.get(zone)
if entity is not None: if entity is not None:
if entity.enabled: if entity.enabled:
entity.process_update(message) entity.process_update(message)
@ -210,7 +226,7 @@ async def async_setup_platform(
zone_entity = OnkyoMediaPlayer( zone_entity = OnkyoMediaPlayer(
receiver, sources, zone, max_volume, receiver_max_volume receiver, sources, zone, max_volume, receiver_max_volume
) )
entities[origin][zone] = zone_entity entities[zone] = zone_entity
async_add_entities([zone_entity]) async_add_entities([zone_entity])
@callback @callback
@ -221,7 +237,7 @@ async def async_setup_platform(
"Receiver (re)connected: %s (%s)", receiver.name, receiver.host "Receiver (re)connected: %s (%s)", receiver.name, receiver.host
) )
for entity in entities[origin].values(): for entity in entities.values():
entity.backfill_state() entity.backfill_state()
_LOGGER.debug("Creating receiver: %s (%s)", info.model_name, info.host) _LOGGER.debug("Creating receiver: %s (%s)", info.model_name, info.host)
@ -237,9 +253,7 @@ async def async_setup_platform(
receiver.name = name or info.model_name receiver.name = name or info.model_name
receiver.discovered = discovered receiver.discovered = discovered
# Store the receiver object and create a dictionary to store its entities.
receivers[receiver.host] = receiver receivers[receiver.host] = receiver
entities[receiver.host] = {}
# Discover what zones are available for the receiver by querying the power. # Discover what zones are available for the receiver by querying the power.
# If we get a response for the specific zone, it means it is available. # If we get a response for the specific zone, it means it is available.
@ -251,7 +265,7 @@ async def async_setup_platform(
main_entity = OnkyoMediaPlayer( main_entity = OnkyoMediaPlayer(
receiver, sources, "main", max_volume, receiver_max_volume receiver, sources, "main", max_volume, receiver_max_volume
) )
entities[receiver.host]["main"] = main_entity entities["main"] = main_entity
async_add_entities([main_entity]) async_add_entities([main_entity])
if host is not None: if host is not None: