mirror of
https://github.com/home-assistant/core.git
synced 2025-04-25 09:47:52 +00:00
Add full test coverage to CPU Speed (#63042)
This commit is contained in:
parent
709afc63b0
commit
c6230fef08
@ -179,8 +179,6 @@ omit =
|
|||||||
homeassistant/components/coolmaster/climate.py
|
homeassistant/components/coolmaster/climate.py
|
||||||
homeassistant/components/coolmaster/const.py
|
homeassistant/components/coolmaster/const.py
|
||||||
homeassistant/components/cppm_tracker/device_tracker.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/__init__.py
|
||||||
homeassistant/components/crownstone/const.py
|
homeassistant/components/crownstone/const.py
|
||||||
homeassistant/components/crownstone/listeners.py
|
homeassistant/components/crownstone/listeners.py
|
||||||
|
@ -74,12 +74,12 @@ class CPUSpeedSensor(SensorEntity):
|
|||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
info = cpuinfo.get_cpu_info()
|
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)
|
self._attr_native_value = round(float(info[HZ_ACTUAL][0]) / 10 ** 9, 2)
|
||||||
else:
|
else:
|
||||||
self._attr_native_value = None
|
self._attr_native_value = None
|
||||||
|
|
||||||
if info is not None:
|
if info:
|
||||||
self._attr_extra_state_attributes = {
|
self._attr_extra_state_attributes = {
|
||||||
ATTR_ARCH: info["arch_string_raw"],
|
ATTR_ARCH: info["arch_string_raw"],
|
||||||
ATTR_BRAND: info["brand_raw"],
|
ATTR_BRAND: info["brand_raw"],
|
||||||
|
@ -7,6 +7,7 @@ from unittest.mock import AsyncMock, MagicMock, patch
|
|||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.cpuspeed.const import DOMAIN
|
from homeassistant.components.cpuspeed.const import DOMAIN
|
||||||
|
from homeassistant.core import HomeAssistant
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
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
|
"homeassistant.components.cpuspeed.async_setup_entry", return_value=True
|
||||||
) as mock_setup:
|
) as mock_setup:
|
||||||
yield 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
|
||||||
|
66
tests/components/cpuspeed/test_init.py
Normal file
66
tests/components/cpuspeed/test_init.py
Normal file
@ -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
|
63
tests/components/cpuspeed/test_sensor.py
Normal file
63
tests/components/cpuspeed/test_sensor.py
Normal file
@ -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
|
Loading…
x
Reference in New Issue
Block a user