diff --git a/.coveragerc b/.coveragerc index ecd80cb79a2..b63c2cbaa1f 100644 --- a/.coveragerc +++ b/.coveragerc @@ -179,8 +179,6 @@ omit = homeassistant/components/coolmaster/climate.py homeassistant/components/coolmaster/const.py homeassistant/components/cppm_tracker/device_tracker.py - homeassistant/components/cpuspeed/__init__.py - homeassistant/components/cpuspeed/sensor.py homeassistant/components/crownstone/__init__.py homeassistant/components/crownstone/const.py homeassistant/components/crownstone/listeners.py diff --git a/homeassistant/components/cpuspeed/sensor.py b/homeassistant/components/cpuspeed/sensor.py index 376a2422901..86c02ae9ee9 100644 --- a/homeassistant/components/cpuspeed/sensor.py +++ b/homeassistant/components/cpuspeed/sensor.py @@ -74,12 +74,12 @@ class CPUSpeedSensor(SensorEntity): """Get the latest data and updates the state.""" info = cpuinfo.get_cpu_info() - if info is not None and HZ_ACTUAL in info: + if info and HZ_ACTUAL in info: self._attr_native_value = round(float(info[HZ_ACTUAL][0]) / 10 ** 9, 2) else: self._attr_native_value = None - if info is not None: + if info: self._attr_extra_state_attributes = { ATTR_ARCH: info["arch_string_raw"], ATTR_BRAND: info["brand_raw"], diff --git a/tests/components/cpuspeed/conftest.py b/tests/components/cpuspeed/conftest.py index 97eb668b02e..a5d7d1837ba 100644 --- a/tests/components/cpuspeed/conftest.py +++ b/tests/components/cpuspeed/conftest.py @@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch import pytest from homeassistant.components.cpuspeed.const import DOMAIN +from homeassistant.core import HomeAssistant from tests.common import MockConfigEntry @@ -43,3 +44,33 @@ def mock_setup_entry() -> Generator[AsyncMock, None, None]: "homeassistant.components.cpuspeed.async_setup_entry", return_value=True ) as mock_setup: yield mock_setup + + +@pytest.fixture +def mock_cpuinfo() -> Generator[MagicMock, None, None]: + """Return a mocked get_cpu_info.""" + info = { + "hz_actual": (3200000001, 0), + "arch_string_raw": "aargh", + "brand_raw": "Intel Ryzen 7", + "hz_advertised": (3600000001, 0), + } + + with patch( + "homeassistant.components.cpuspeed.cpuinfo.get_cpu_info", + return_value=info, + ) as cpuinfo_mock: + yield cpuinfo_mock + + +@pytest.fixture +async def init_integration( + hass: HomeAssistant, mock_config_entry: MockConfigEntry, mock_cpuinfo: MagicMock +) -> MockConfigEntry: + """Set up the CPU Speed integration for testing.""" + mock_config_entry.add_to_hass(hass) + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + return mock_config_entry diff --git a/tests/components/cpuspeed/test_init.py b/tests/components/cpuspeed/test_init.py new file mode 100644 index 00000000000..2352e411b8e --- /dev/null +++ b/tests/components/cpuspeed/test_init.py @@ -0,0 +1,66 @@ +"""Tests for the CPU Speed integration.""" +from unittest.mock import MagicMock + +import pytest + +from homeassistant.components.cpuspeed.const import DOMAIN +from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN +from homeassistant.config_entries import ConfigEntryState +from homeassistant.core import HomeAssistant +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +async def test_load_unload_config_entry( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_cpuinfo: MagicMock, +) -> None: + """Test the CPU Speed configuration entry loading/unloading.""" + mock_config_entry.add_to_hass(hass) + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.LOADED + assert len(mock_cpuinfo.mock_calls) == 2 + + await hass.config_entries.async_unload(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert not hass.data.get(DOMAIN) + assert mock_config_entry.state is ConfigEntryState.NOT_LOADED + + +async def test_config_entry_not_compatible( + hass: HomeAssistant, + mock_config_entry: MockConfigEntry, + mock_cpuinfo: MagicMock, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the CPU Speed configuration entry loading on an unsupported system.""" + mock_config_entry.add_to_hass(hass) + mock_cpuinfo.return_value = {} + + await hass.config_entries.async_setup(mock_config_entry.entry_id) + await hass.async_block_till_done() + + assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR + assert len(mock_cpuinfo.mock_calls) == 1 + assert "is not compatible with your system" in caplog.text + + +async def test_import_config( + hass: HomeAssistant, + mock_cpuinfo: MagicMock, + caplog: pytest.LogCaptureFixture, +) -> None: + """Test the CPU Speed being set up from config via import.""" + assert await async_setup_component( + hass, SENSOR_DOMAIN, {SENSOR_DOMAIN: {"platform": DOMAIN}} + ) + await hass.async_block_till_done() + + assert len(hass.config_entries.async_entries(DOMAIN)) == 1 + assert len(mock_cpuinfo.mock_calls) == 3 + assert "the CPU Speed platform in YAML is deprecated" in caplog.text diff --git a/tests/components/cpuspeed/test_sensor.py b/tests/components/cpuspeed/test_sensor.py new file mode 100644 index 00000000000..134d19b31ea --- /dev/null +++ b/tests/components/cpuspeed/test_sensor.py @@ -0,0 +1,63 @@ +"""Tests for the sensor provided by the CPU Speed integration.""" + +from unittest.mock import MagicMock + +from homeassistant.components.cpuspeed.sensor import ATTR_ARCH, ATTR_BRAND, ATTR_HZ +from homeassistant.components.homeassistant import ( + DOMAIN as HOME_ASSISTANT_DOMAIN, + SERVICE_UPDATE_ENTITY, +) +from homeassistant.const import ( + ATTR_DEVICE_CLASS, + ATTR_ENTITY_ID, + ATTR_FRIENDLY_NAME, + ATTR_ICON, + STATE_UNKNOWN, +) +from homeassistant.core import HomeAssistant +from homeassistant.helpers import entity_registry as er +from homeassistant.setup import async_setup_component + +from tests.common import MockConfigEntry + + +async def test_sensor( + hass: HomeAssistant, + mock_cpuinfo: MagicMock, + init_integration: MockConfigEntry, +) -> None: + """Test the CPU Speed sensor.""" + await async_setup_component(hass, "homeassistant", {}) + entity_registry = er.async_get(hass) + + entry = entity_registry.async_get("sensor.cpu_speed") + assert entry + assert entry.unique_id == entry.config_entry_id + assert entry.entity_category is None + + state = hass.states.get("sensor.cpu_speed") + assert state + assert state.state == "3.2" + assert state.attributes.get(ATTR_FRIENDLY_NAME) == "CPU Speed" + assert state.attributes.get(ATTR_ICON) == "mdi:pulse" + assert ATTR_DEVICE_CLASS not in state.attributes + + assert state.attributes.get(ATTR_ARCH) == "aargh" + assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7" + assert state.attributes.get(ATTR_HZ) == 3.6 + + mock_cpuinfo.return_value = {} + await hass.services.async_call( + HOME_ASSISTANT_DOMAIN, + SERVICE_UPDATE_ENTITY, + {ATTR_ENTITY_ID: "sensor.cpu_speed"}, + blocking=True, + ) + await hass.async_block_till_done() + + state = hass.states.get("sensor.cpu_speed") + assert state + assert state.state == STATE_UNKNOWN + assert state.attributes.get(ATTR_ARCH) == "aargh" + assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7" + assert state.attributes.get(ATTR_HZ) == 3.6