Add Marantz PM8006 power amp direct buttons (#191693)

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
vsg005
2026-11-24 19:50:38 -05:00
committed by GitHub
co-authored by Copilot Autofix powered by AI Claude
parent 39842000c5
commit eeac2e7ebf
6 changed files with 420 additions and 1 deletions
@@ -6,7 +6,7 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.const import Platform
from homeassistant.core import HomeAssistant
PLATFORMS = [Platform.BUTTON, Platform.MEDIA_PLAYER]
PLATFORMS = [Platform.BUTTON, Platform.MEDIA_PLAYER, Platform.SWITCH]
@dataclass
@@ -23,6 +23,8 @@ from .entity import MarantzIrEntity
PARALLEL_UPDATES = 1
SOURCE_TO_CODE: dict[str, MarantzAudioCode] = {
"aux_1": MarantzAudioCode.SOURCE_AUX_1,
"aux_pm8006": MarantzAudioCode.SOURCE_AUX_PM8006,
"cd": MarantzAudioCode.SOURCE_CD,
"coax": MarantzAudioCode.SOURCE_COAX,
"laserdisc": MarantzAudioCode.SOURCE_LD,
@@ -36,6 +36,8 @@
"state_attributes": {
"source": {
"state": {
"aux_1": "Aux",
"aux_pm8006": "Aux",
"cd": "CD",
"coax": "Coax",
"laserdisc": "LaserDisc",
@@ -53,6 +55,17 @@
}
}
}
},
"switch": {
"power_amp_direct": {
"name": "Power amp direct"
},
"speaker_a": {
"name": "Speaker A"
},
"speaker_b": {
"name": "Speaker B"
}
}
}
}
@@ -0,0 +1,107 @@
"""Switch platform for Marantz IR integration.
Features with separate on and off IR codes are switches with an assumed state,
because infrared cannot read the state back from the amplifier.
"""
from dataclasses import dataclass
from typing import Any, override
from infrared_protocols.codes.marantz.audio import MarantzAudioCode
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.const import CONF_MODEL, STATE_OFF, STATE_ON
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
from homeassistant.helpers.restore_state import RestoreEntity
from . import MarantzIrConfigEntry
from .const import CONF_INFRARED_EMITTER_ENTITY_ID, MODELS
from .entity import MarantzIrEntity
PARALLEL_UPDATES = 1
@dataclass(frozen=True, kw_only=True)
class MarantzIrSwitchEntityDescription(SwitchEntityDescription):
"""Describes a Marantz IR switch backed by separate on and off codes."""
on_code: MarantzAudioCode
off_code: MarantzAudioCode
SWITCH_DESCRIPTIONS: tuple[MarantzIrSwitchEntityDescription, ...] = (
MarantzIrSwitchEntityDescription(
key="speaker_a",
translation_key="speaker_a",
on_code=MarantzAudioCode.SPEAKER_A_ON,
off_code=MarantzAudioCode.SPEAKER_A_OFF,
),
MarantzIrSwitchEntityDescription(
key="speaker_b",
translation_key="speaker_b",
on_code=MarantzAudioCode.SPEAKER_B_ON,
off_code=MarantzAudioCode.SPEAKER_B_OFF,
),
MarantzIrSwitchEntityDescription(
key="power_amp_direct",
translation_key="power_amp_direct",
on_code=MarantzAudioCode.POWER_AMP_DIRECT_ON,
off_code=MarantzAudioCode.POWER_AMP_DIRECT_OFF,
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: MarantzIrConfigEntry,
async_add_entities: AddConfigEntryEntitiesCallback,
) -> None:
"""Set up Marantz IR switches from config entry."""
infrared_entity_id = entry.data[CONF_INFRARED_EMITTER_ENTITY_ID]
model_codes = MODELS[entry.data[CONF_MODEL]].codes
async_add_entities(
MarantzIrSwitch(entry, infrared_entity_id, description)
for description in SWITCH_DESCRIPTIONS
if description.on_code in model_codes and description.off_code in model_codes
)
class MarantzIrSwitch(MarantzIrEntity, SwitchEntity, RestoreEntity):
"""Marantz IR switch entity."""
_attr_assumed_state = True
entity_description: MarantzIrSwitchEntityDescription
def __init__(
self,
entry: MarantzIrConfigEntry,
infrared_entity_id: str,
description: MarantzIrSwitchEntityDescription,
) -> None:
"""Initialize Marantz IR switch."""
super().__init__(entry, infrared_entity_id, unique_id_suffix=description.key)
self.entity_description = description
@override
async def async_added_to_hass(self) -> None:
"""Restore the last assumed state."""
await super().async_added_to_hass()
if (last_state := await self.async_get_last_state()) is not None and (
last_state.state in (STATE_ON, STATE_OFF)
):
self._attr_is_on = last_state.state == STATE_ON
@override
async def async_turn_on(self, **kwargs: Any) -> None:
"""Send the on command."""
await self._send_marantz_command(self.entity_description.on_code)
self._attr_is_on = True
self.async_write_ha_state()
@override
async def async_turn_off(self, **kwargs: Any) -> None:
"""Send the off command."""
await self._send_marantz_command(self.entity_description.off_code)
self._attr_is_on = False
self.async_write_ha_state()
@@ -0,0 +1,154 @@
# serializer version: 1
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_power_amp_direct-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('marantz_infrared', '01JTEST0000000000000000000')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_power_amp_direct',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Power amp direct',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Power amp direct',
'platform': 'marantz_infrared',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'power_amp_direct',
'unique_id': '01JTEST0000000000000000000_power_amp_direct',
'unit_of_measurement': None,
})
# ---
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_power_amp_direct-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.ASSUMED_STATE: 'assumed_state'>: True,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Marantz PM8006 Integrated Amplifier Power amp direct',
}),
'context': <ANY>,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_power_amp_direct',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_speaker_a-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('marantz_infrared', '01JTEST0000000000000000000')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_speaker_a',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Speaker A',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Speaker A',
'platform': 'marantz_infrared',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'speaker_a',
'unique_id': '01JTEST0000000000000000000_speaker_a',
'unit_of_measurement': None,
})
# ---
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_speaker_a-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.ASSUMED_STATE: 'assumed_state'>: True,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Marantz PM8006 Integrated Amplifier Speaker A',
}),
'context': <ANY>,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_speaker_a',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_speaker_b-entry]
EntityRegistryEntrySnapshot({
'aliases': list([
None,
]),
'area_id': None,
'capabilities': None,
'config_entry_id': <ANY>,
'config_subentry_id': None,
'device_class': None,
'device_id': <DeviceEntry identifiers=[('marantz_infrared', '01JTEST0000000000000000000')]>,
'disabled_by': None,
'domain': 'switch',
'entity_category': None,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_speaker_b',
'has_entity_name': True,
'hidden_by': None,
'icon': None,
'id': <ANY>,
'labels': set({
}),
'name': None,
'object_id_base': 'Speaker B',
'options': dict({
}),
'original_device_class': None,
'original_icon': None,
'original_name': 'Speaker B',
'platform': 'marantz_infrared',
'previous_unique_id': None,
'suggested_object_id': None,
'supported_features': 0,
'translation_key': 'speaker_b',
'unique_id': '01JTEST0000000000000000000_speaker_b',
'unit_of_measurement': None,
})
# ---
# name: test_entities[pm8006_integrated_amplifier][switch.marantz_pm8006_integrated_amplifier_speaker_b-state]
StateSnapshot({
'attributes': ReadOnlyDict({
<EntityStateAttribute.ASSUMED_STATE: 'assumed_state'>: True,
<EntityStateAttribute.FRIENDLY_NAME: 'friendly_name'>: 'Marantz PM8006 Integrated Amplifier Speaker B',
}),
'context': <ANY>,
'entity_id': 'switch.marantz_pm8006_integrated_amplifier_speaker_b',
'last_changed': <ANY>,
'last_reported': <ANY>,
'last_updated': <ANY>,
'state': 'unknown',
})
# ---
@@ -0,0 +1,143 @@
"""Tests for the Marantz Infrared switch platform."""
from infrared_protocols.codes.marantz.audio import MarantzAudioCode
import pytest
from pytest_homeassistant.common import (
MockConfigEntry,
mock_restore_cache,
snapshot_platform,
)
from pytest_homeassistant.components.common import (
assert_availability_follows_source_entity,
)
from pytest_homeassistant.components.infrared.common import MockInfraredEmitterEntity
from syrupy.assertion import SnapshotAssertion
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchService
from homeassistant.const import STATE_OFF, STATE_ON, STATE_UNKNOWN, Platform, TargetKey
from homeassistant.core import HomeAssistant, State
from homeassistant.helpers import entity_registry as er
from .conftest import MOCK_INFRARED_EMITTER_ENTITY_ID
PM8006 = "pm8006_integrated_amplifier"
SWITCH_ENTITY_ID_SPEAKER_A = "switch.marantz_pm8006_integrated_amplifier_speaker_a"
SWITCH_ENTITY_ID_SPEAKER_B = "switch.marantz_pm8006_integrated_amplifier_speaker_b"
SWITCH_ENTITY_ID_POWER_AMP_DIRECT = (
"switch.marantz_pm8006_integrated_amplifier_power_amp_direct"
)
@pytest.fixture
def platforms() -> list[Platform]:
"""Return platforms to set up."""
return [Platform.SWITCH]
@pytest.mark.parametrize("model", [PM8006], indirect=True)
@pytest.mark.usefixtures("init_integration")
async def test_entities(
hass: HomeAssistant,
snapshot: SnapshotAssertion,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test all switch entities are created with correct attributes."""
await snapshot_platform(hass, entity_registry, snapshot, mock_config_entry.entry_id)
@pytest.mark.parametrize("model", [PM8006], indirect=True)
@pytest.mark.parametrize(
("entity_id", "on_code", "off_code"),
[
(
SWITCH_ENTITY_ID_SPEAKER_A,
MarantzAudioCode.SPEAKER_A_ON,
MarantzAudioCode.SPEAKER_A_OFF,
),
(
SWITCH_ENTITY_ID_SPEAKER_B,
MarantzAudioCode.SPEAKER_B_ON,
MarantzAudioCode.SPEAKER_B_OFF,
),
(
SWITCH_ENTITY_ID_POWER_AMP_DIRECT,
MarantzAudioCode.POWER_AMP_DIRECT_ON,
MarantzAudioCode.POWER_AMP_DIRECT_OFF,
),
],
)
@pytest.mark.usefixtures("init_integration")
async def test_turn_on_off_sends_code_and_sets_state(
hass: HomeAssistant,
mock_infrared_emitter_entity: MockInfraredEmitterEntity,
entity_id: str,
on_code: MarantzAudioCode,
off_code: MarantzAudioCode,
) -> None:
"""Test turning a switch on and off sends its codes and updates the state."""
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_UNKNOWN
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_ON,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_ON
await hass.services.async_call(
SWITCH_DOMAIN,
SwitchService.TURN_OFF,
{TargetKey.ENTITY_ID: entity_id},
blocking=True,
)
state = hass.states.get(entity_id)
assert state is not None
assert state.state == STATE_OFF
assert mock_infrared_emitter_entity.send_command_calls == [on_code, off_code]
@pytest.mark.usefixtures("init_integration")
async def test_only_supported_switches_created(
hass: HomeAssistant,
entity_registry: er.EntityRegistry,
mock_config_entry: MockConfigEntry,
) -> None:
"""Test that a model without the discrete codes gets no switches."""
assert not er.async_entries_for_config_entry(
entity_registry, mock_config_entry.entry_id
)
@pytest.mark.parametrize("model", [PM8006], indirect=True)
@pytest.mark.parametrize("restored_state", [STATE_ON, STATE_OFF])
@pytest.mark.usefixtures("mock_infrared_emitter_entity", "mock_marantz_to_command")
async def test_restores_state(
hass: HomeAssistant,
mock_config_entry: MockConfigEntry,
restored_state: str,
) -> None:
"""Test the assumed state survives a restart."""
mock_restore_cache(hass, [State(SWITCH_ENTITY_ID_POWER_AMP_DIRECT, restored_state)])
mock_config_entry.add_to_hass(hass)
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get(SWITCH_ENTITY_ID_POWER_AMP_DIRECT)
assert state is not None
assert state.state == restored_state
@pytest.mark.parametrize("model", [PM8006], indirect=True)
@pytest.mark.usefixtures("init_integration")
async def test_switch_availability_follows_ir_entity(hass: HomeAssistant) -> None:
"""Test switch becomes unavailable when IR entity is unavailable."""
await assert_availability_follows_source_entity(
hass, SWITCH_ENTITY_ID_POWER_AMP_DIRECT, [MOCK_INFRARED_EMITTER_ENTITY_ID]
)