From dc9238a8068f0c5751bf8a9cd17eb73a9d105a15 Mon Sep 17 00:00:00 2001 From: pattyland Date: Sat, 9 May 2026 16:12:54 +0200 Subject: [PATCH] Use standby for HDMI-CEC turn off --- .../components/hdmi_cec/media_player.py | 3 ++- homeassistant/components/hdmi_cec/switch.py | 5 +++-- tests/components/hdmi_cec/__init__.py | 15 ++++++++++++++- tests/components/hdmi_cec/test_media_player.py | 10 ++++++++-- tests/components/hdmi_cec/test_switch.py | 18 +++++++++++++++--- 5 files changed, 42 insertions(+), 9 deletions(-) diff --git a/homeassistant/components/hdmi_cec/media_player.py b/homeassistant/components/hdmi_cec/media_player.py index b3d68a04ac801..ac4ad314e3390 100644 --- a/homeassistant/components/hdmi_cec/media_player.py +++ b/homeassistant/components/hdmi_cec/media_player.py @@ -4,6 +4,7 @@ from pycec.commands import CecCommand, KeyPressCommand, KeyReleaseCommand from pycec.const import ( + CMD_STANDBY, KEY_BACKWARD, KEY_FORWARD, KEY_MUTE_TOGGLE, @@ -93,7 +94,7 @@ async def async_turn_on(self) -> None: async def async_turn_off(self) -> None: """Turn device off.""" - self._device.turn_off() + self._device.send_command(CecCommand(CMD_STANDBY, dst=self._logical_address)) self._attr_state = MediaPlayerState.OFF self.async_write_ha_state() diff --git a/homeassistant/components/hdmi_cec/switch.py b/homeassistant/components/hdmi_cec/switch.py index b85ea10b88190..a0dc6459b4d58 100644 --- a/homeassistant/components/hdmi_cec/switch.py +++ b/homeassistant/components/hdmi_cec/switch.py @@ -3,7 +3,8 @@ import logging from typing import Any -from pycec.const import POWER_OFF, POWER_ON +from pycec.commands import CecCommand +from pycec.const import CMD_STANDBY, POWER_OFF, POWER_ON from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN, SwitchEntity from homeassistant.core import HomeAssistant @@ -50,7 +51,7 @@ async def async_turn_on(self, **kwargs: Any) -> None: async def async_turn_off(self, **kwargs: Any) -> None: """Turn device off.""" - self._device.turn_off() + self._device.send_command(CecCommand(CMD_STANDBY, dst=self._logical_address)) self._attr_is_on = False self.async_write_ha_state() diff --git a/tests/components/hdmi_cec/__init__.py b/tests/components/hdmi_cec/__init__.py index 1d51fa0cc506a..cbdc53fbb6346 100644 --- a/tests/components/hdmi_cec/__init__.py +++ b/tests/components/hdmi_cec/__init__.py @@ -2,7 +2,11 @@ from unittest.mock import AsyncMock, Mock -from homeassistant.components.hdmi_cec import KeyPressCommand, KeyReleaseCommand +from homeassistant.components.hdmi_cec import ( + CecCommand, + KeyPressCommand, + KeyReleaseCommand, +) class MockHDMIDevice: @@ -49,3 +53,12 @@ def assert_key_press_release(fnc, count=0, *, dst, key): assert press_arg.dst == dst assert isinstance(release_arg, KeyReleaseCommand) assert release_arg.dst == dst + + +def assert_cec_command(fnc, *, cmd, dst): + """Assert that correct CecCommand was sent.""" + fnc.assert_called_once() + command = fnc.call_args.args[0] + assert isinstance(command, CecCommand) + assert command.cmd == cmd + assert command.dst == dst diff --git a/tests/components/hdmi_cec/test_media_player.py b/tests/components/hdmi_cec/test_media_player.py index 14564b7c41a85..e2787e48f7dbc 100644 --- a/tests/components/hdmi_cec/test_media_player.py +++ b/tests/components/hdmi_cec/test_media_player.py @@ -3,6 +3,7 @@ from typing import Any from pycec.const import ( + CMD_STANDBY, DEVICE_TYPE_NAMES, KEY_BACKWARD, KEY_FORWARD, @@ -54,7 +55,7 @@ ) from homeassistant.core import HomeAssistant -from . import MockHDMIDevice, assert_key_press_release +from . import MockHDMIDevice, assert_cec_command, assert_key_press_release from .conftest import CecEntityCreator, HDMINetworkCreator @@ -148,7 +149,12 @@ async def test_service_off( blocking=True, ) - mock_hdmi_device.turn_off.assert_called_once_with() + mock_hdmi_device.turn_off.assert_not_called() + assert_cec_command( + mock_hdmi_device.send_command, + cmd=CMD_STANDBY, + dst=mock_hdmi_device.logical_address, + ) state = hass.states.get("media_player.hdmi_3") assert state.state == STATE_OFF diff --git a/tests/components/hdmi_cec/test_switch.py b/tests/components/hdmi_cec/test_switch.py index 6ef6ce835ceb3..49a70c2caae5f 100644 --- a/tests/components/hdmi_cec/test_switch.py +++ b/tests/components/hdmi_cec/test_switch.py @@ -1,6 +1,13 @@ """Tests for the HDMI-CEC switch platform.""" -from pycec.const import POWER_OFF, POWER_ON, STATUS_PLAY, STATUS_STILL, STATUS_STOP +from pycec.const import ( + CMD_STANDBY, + POWER_OFF, + POWER_ON, + STATUS_PLAY, + STATUS_STILL, + STATUS_STOP, +) from pycec.network import PhysicalAddress import pytest @@ -16,7 +23,7 @@ ) from homeassistant.core import HomeAssistant -from . import MockHDMIDevice +from . import MockHDMIDevice, assert_cec_command from .conftest import CecEntityCreator, HDMINetworkCreator @@ -107,7 +114,12 @@ async def test_service_off( blocking=True, ) - mock_hdmi_device.turn_off.assert_called_once_with() + mock_hdmi_device.turn_off.assert_not_called() + assert_cec_command( + mock_hdmi_device.send_command, + cmd=CMD_STANDBY, + dst=mock_hdmi_device.logical_address, + ) state = hass.states.get("switch.hdmi_3") assert state.state == STATE_OFF