diff --git a/homeassistant/components/madvr/sensor.py b/homeassistant/components/madvr/sensor.py index d0f5556cc5d..6f0933ac879 100644 --- a/homeassistant/components/madvr/sensor.py +++ b/homeassistant/components/madvr/sensor.py @@ -58,7 +58,7 @@ def get_temperature(coordinator: MadVRCoordinator, key: str) -> float | None: """Get temperature value if valid, otherwise return None.""" try: temp = float(coordinator.data.get(key, 0)) - except ValueError: + except (AttributeError, ValueError): return None else: return temp if is_valid_temperature(temp) else None diff --git a/tests/components/madvr/const.py b/tests/components/madvr/const.py index 6bfa3a77167..8c5e122377b 100644 --- a/tests/components/madvr/const.py +++ b/tests/components/madvr/const.py @@ -8,3 +8,11 @@ MOCK_CONFIG = { } MOCK_MAC = "00:11:22:33:44:55" + +TEST_CON_ERROR = ConnectionError("Connection failed") +TEST_IMP_ERROR = NotImplementedError("Not implemented") + +TEST_FAILED_ON = "Failed to turn on device" +TEST_FAILED_OFF = "Failed to turn off device" +TEST_FAILED_CMD = "Failed to send command" +TEST_COMMAND = "test" diff --git a/tests/components/madvr/test_remote.py b/tests/components/madvr/test_remote.py index fc6471bf664..6fc507534d6 100644 --- a/tests/components/madvr/test_remote.py +++ b/tests/components/madvr/test_remote.py @@ -4,10 +4,15 @@ from __future__ import annotations from unittest.mock import AsyncMock, patch +import pytest from syrupy import SnapshotAssertion -from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN +from homeassistant.components.remote import ( + DOMAIN as REMOTE_DOMAIN, + SERVICE_SEND_COMMAND, +) from homeassistant.const import ( + ATTR_COMMAND, ATTR_ENTITY_ID, SERVICE_TURN_OFF, SERVICE_TURN_ON, @@ -18,6 +23,14 @@ from homeassistant.core import HomeAssistant import homeassistant.helpers.entity_registry as er from . import setup_integration +from .const import ( + TEST_COMMAND, + TEST_CON_ERROR, + TEST_FAILED_CMD, + TEST_FAILED_OFF, + TEST_FAILED_ON, + TEST_IMP_ERROR, +) from tests.common import MockConfigEntry, snapshot_platform @@ -39,6 +52,7 @@ async def test_remote_power( hass: HomeAssistant, mock_madvr_client: AsyncMock, mock_config_entry: MockConfigEntry, + caplog: pytest.LogCaptureFixture, ) -> None: """Test turning on the remote entity.""" @@ -61,11 +75,47 @@ async def test_remote_power( mock_madvr_client.power_on.assert_called_once() + # cover exception cases + caplog.clear() + mock_madvr_client.power_off.side_effect = TEST_CON_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + assert TEST_FAILED_OFF in caplog.text + + # Test turning off with NotImplementedError + caplog.clear() + mock_madvr_client.power_off.side_effect = TEST_IMP_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, SERVICE_TURN_OFF, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + assert TEST_FAILED_OFF in caplog.text + + # Reset side_effect for power_off + mock_madvr_client.power_off.side_effect = None + + # Test turning on with ConnectionError + caplog.clear() + mock_madvr_client.power_on.side_effect = TEST_CON_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + assert TEST_FAILED_ON in caplog.text + + # Test turning on with NotImplementedError + caplog.clear() + mock_madvr_client.power_on.side_effect = TEST_IMP_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, SERVICE_TURN_ON, {ATTR_ENTITY_ID: entity_id}, blocking=True + ) + assert TEST_FAILED_ON in caplog.text + async def test_send_command( hass: HomeAssistant, mock_madvr_client: AsyncMock, mock_config_entry: MockConfigEntry, + caplog: pytest.LogCaptureFixture, ) -> None: """Test sending command to the remote entity.""" @@ -77,9 +127,29 @@ async def test_send_command( await hass.services.async_call( REMOTE_DOMAIN, - "send_command", - {ATTR_ENTITY_ID: entity_id, "command": "test"}, + SERVICE_SEND_COMMAND, + {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: TEST_COMMAND}, blocking=True, ) - mock_madvr_client.add_command_to_queue.assert_called_once_with(["test"]) + mock_madvr_client.add_command_to_queue.assert_called_once_with([TEST_COMMAND]) + # cover exceptions + # Test ConnectionError + mock_madvr_client.add_command_to_queue.side_effect = TEST_CON_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, + SERVICE_SEND_COMMAND, + {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: TEST_COMMAND}, + blocking=True, + ) + assert TEST_FAILED_CMD in caplog.text + + # Test NotImplementedError + mock_madvr_client.add_command_to_queue.side_effect = TEST_IMP_ERROR + await hass.services.async_call( + REMOTE_DOMAIN, + SERVICE_SEND_COMMAND, + {ATTR_ENTITY_ID: entity_id, ATTR_COMMAND: TEST_COMMAND}, + blocking=True, + ) + assert TEST_FAILED_CMD in caplog.text diff --git a/tests/components/madvr/test_sensors.py b/tests/components/madvr/test_sensors.py index 5a918dcd433..25dcc1cdcca 100644 --- a/tests/components/madvr/test_sensors.py +++ b/tests/components/madvr/test_sensors.py @@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, patch import pytest from syrupy import SnapshotAssertion +from homeassistant.components.madvr.sensor import get_temperature from homeassistant.const import STATE_UNKNOWN, Platform from homeassistant.core import HomeAssistant import homeassistant.helpers.entity_registry as er @@ -89,3 +90,6 @@ async def test_sensor_setup_and_states( update_callback({"temp_gpu": 41.2}) await hass.async_block_till_done() assert hass.states.get("sensor.madvr_envy_gpu_temperature").state == "41.2" + + # test get_temperature ValueError + assert get_temperature(None, "temp_key") is None