From 842763bd277b9f57f666ff9bb370779db72fda3b Mon Sep 17 00:00:00 2001 From: Robert Contreras Date: Fri, 21 Jun 2024 08:37:22 -0700 Subject: [PATCH] Add Home Connect binary_sensor unit tests (#115323) --- .coveragerc | 1 - .../home_connect/test_binary_sensor.py | 74 +++++++++++++++++++ tests/components/home_connect/test_init.py | 1 + 3 files changed, 75 insertions(+), 1 deletion(-) create mode 100644 tests/components/home_connect/test_binary_sensor.py diff --git a/.coveragerc b/.coveragerc index 56a93b586a4..350c39ca3d2 100644 --- a/.coveragerc +++ b/.coveragerc @@ -536,7 +536,6 @@ omit = homeassistant/components/hko/weather.py homeassistant/components/hlk_sw16/__init__.py homeassistant/components/hlk_sw16/switch.py - homeassistant/components/home_connect/binary_sensor.py homeassistant/components/home_connect/entity.py homeassistant/components/home_connect/light.py homeassistant/components/home_connect/switch.py diff --git a/tests/components/home_connect/test_binary_sensor.py b/tests/components/home_connect/test_binary_sensor.py new file mode 100644 index 00000000000..d21aec35045 --- /dev/null +++ b/tests/components/home_connect/test_binary_sensor.py @@ -0,0 +1,74 @@ +"""Tests for home_connect binary_sensor entities.""" + +from collections.abc import Awaitable, Callable, Generator +from typing import Any +from unittest.mock import MagicMock, Mock + +import pytest + +from homeassistant.components.home_connect.const import ( + BSH_DOOR_STATE, + BSH_DOOR_STATE_CLOSED, + BSH_DOOR_STATE_LOCKED, + BSH_DOOR_STATE_OPEN, +) +from homeassistant.config_entries import ConfigEntryState +from homeassistant.const import Platform +from homeassistant.core import HomeAssistant +from homeassistant.helpers.entity_component import async_update_entity + +from tests.common import MockConfigEntry + + +@pytest.fixture +def platforms() -> list[str]: + """Fixture to specify platforms to test.""" + return [Platform.BINARY_SENSOR] + + +async def test_binary_sensors( + bypass_throttle: Generator[None, Any, None], + hass: HomeAssistant, + config_entry: MockConfigEntry, + integration_setup: Callable[[], Awaitable[bool]], + setup_credentials: None, + get_appliances: MagicMock, + appliance: Mock, +) -> None: + """Test binary sensor entities.""" + get_appliances.return_value = [appliance] + assert config_entry.state == ConfigEntryState.NOT_LOADED + assert await integration_setup() + assert config_entry.state == ConfigEntryState.LOADED + + +@pytest.mark.parametrize( + ("state", "expected"), + [ + (BSH_DOOR_STATE_CLOSED, "off"), + (BSH_DOOR_STATE_LOCKED, "off"), + (BSH_DOOR_STATE_OPEN, "on"), + ("", "unavailable"), + ], +) +async def test_binary_sensors_door_states( + expected: str, + state: str, + bypass_throttle: Generator[None, Any, None], + hass: HomeAssistant, + config_entry: MockConfigEntry, + integration_setup: Callable[[], Awaitable[bool]], + setup_credentials: None, + get_appliances: MagicMock, + appliance: Mock, +) -> None: + """Tests for Appliance door states.""" + entity_id = "binary_sensor.washer_door" + get_appliances.return_value = [appliance] + assert config_entry.state == ConfigEntryState.NOT_LOADED + assert await integration_setup() + assert config_entry.state == ConfigEntryState.LOADED + appliance.status.update({BSH_DOOR_STATE: {"value": state}}) + await async_update_entity(hass, entity_id) + await hass.async_block_till_done() + assert hass.states.is_state(entity_id, expected) diff --git a/tests/components/home_connect/test_init.py b/tests/components/home_connect/test_init.py index 10e7d8ca911..616a82edebc 100644 --- a/tests/components/home_connect/test_init.py +++ b/tests/components/home_connect/test_init.py @@ -118,6 +118,7 @@ SERVICE_APPLIANCE_METHOD_MAPPING = { async def test_api_setup( + bypass_throttle: Generator[None, Any, None], hass: HomeAssistant, config_entry: MockConfigEntry, integration_setup: Callable[[], Awaitable[bool]],