Skip to content

Commit

Permalink
Fix Onkyo action select_hdmi_output (home-assistant#125115)
Browse files Browse the repository at this point in the history
* Fix Onkyo service select_hdmi_output

* Move Hasskey directly under Onkyo domain
  • Loading branch information
arturpragacz authored and bramkragten committed Sep 3, 2024
1 parent 393a0ac commit be3b16b
Showing 1 changed file with 37 additions and 23 deletions.
60 changes: 37 additions & 23 deletions homeassistant/components/onkyo/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import voluptuous as vol

from homeassistant.components.media_player import (
DOMAIN,
DOMAIN as MEDIA_PLAYER_DOMAIN,
PLATFORM_SCHEMA as MEDIA_PLAYER_PLATFORM_SCHEMA,
MediaPlayerEntity,
MediaPlayerEntityFeature,
Expand All @@ -28,9 +28,14 @@
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
from homeassistant.util.hass_dict import HassKey

_LOGGER = logging.getLogger(__name__)

DOMAIN = "onkyo"

DATA_MP_ENTITIES: HassKey[list[dict[str, OnkyoMediaPlayer]]] = HassKey(DOMAIN)

CONF_SOURCES = "sources"
CONF_MAX_VOLUME = "max_volume"
CONF_RECEIVER_MAX_VOLUME = "receiver_max_volume"
Expand Down Expand Up @@ -148,37 +153,45 @@ class ReceiverInfo:
identifier: str


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Onkyo platform."""
receivers: dict[str, pyeiscp.Connection] = {} # indexed by host
entities: dict[str, dict[str, OnkyoMediaPlayer]] = {} # indexed by host and zone
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 = [
entity
for h in entities.values()
for entity in h.values()
if entity.entity_id in entity_ids
]

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(
DOMAIN,
MEDIA_PLAYER_DOMAIN,
SERVICE_SELECT_HDMI_OUTPUT,
async_service_handle,
schema=ONKYO_SELECT_OUTPUT_SCHEMA,
)


async def async_setup_platform(
hass: HomeAssistant,
config: ConfigType,
async_add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up the Onkyo platform."""
await async_register_services(hass)

receivers: dict[str, pyeiscp.Connection] = {} # indexed by host
all_entities = hass.data.setdefault(DATA_MP_ENTITIES, [])

host = config.get(CONF_HOST)
name = config.get(CONF_NAME)
max_volume = config[CONF_MAX_VOLUME]
Expand All @@ -188,6 +201,9 @@ async def async_service_handle(service: ServiceCall) -> None:
async def async_setup_receiver(
info: ReceiverInfo, discovered: bool, name: str | None
) -> None:
entities: dict[str, OnkyoMediaPlayer] = {}
all_entities.append(entities)

@callback
def async_onkyo_update_callback(
message: tuple[str, str, Any], origin: str
Expand All @@ -199,7 +215,7 @@ def async_onkyo_update_callback(
)

zone, _, value = message
entity = entities[origin].get(zone)
entity = entities.get(zone)
if entity is not None:
if entity.enabled:
entity.process_update(message)
Expand All @@ -210,7 +226,7 @@ def async_onkyo_update_callback(
zone_entity = OnkyoMediaPlayer(
receiver, sources, zone, max_volume, receiver_max_volume
)
entities[origin][zone] = zone_entity
entities[zone] = zone_entity
async_add_entities([zone_entity])

@callback
Expand All @@ -221,7 +237,7 @@ def async_onkyo_connect_callback(origin: str) -> None:
"Receiver (re)connected: %s (%s)", receiver.name, receiver.host
)

for entity in entities[origin].values():
for entity in entities.values():
entity.backfill_state()

_LOGGER.debug("Creating receiver: %s (%s)", info.model_name, info.host)
Expand All @@ -237,9 +253,7 @@ def async_onkyo_connect_callback(origin: str) -> None:
receiver.name = name or info.model_name
receiver.discovered = discovered

# Store the receiver object and create a dictionary to store its entities.
receivers[receiver.host] = receiver
entities[receiver.host] = {}

# 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.
Expand All @@ -251,7 +265,7 @@ def async_onkyo_connect_callback(origin: str) -> None:
main_entity = OnkyoMediaPlayer(
receiver, sources, "main", max_volume, receiver_max_volume
)
entities[receiver.host]["main"] = main_entity
entities["main"] = main_entity
async_add_entities([main_entity])

if host is not None:
Expand Down

0 comments on commit be3b16b

Please sign in to comment.