Migrate CPUSpeed to new entity naming style (#75080)

This commit is contained in:
Franck Nijhof 2022-07-20 11:31:17 +02:00 committed by GitHub
parent b62ebbe974
commit 05d2b955ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 17 additions and 2 deletions

View File

@ -7,8 +7,11 @@ from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import FREQUENCY_GIGAHERTZ
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import DOMAIN
ATTR_BRAND = "brand"
ATTR_HZ = "ghz_advertised"
ATTR_ARCH = "arch"
@ -30,12 +33,16 @@ class CPUSpeedSensor(SensorEntity):
"""Representation of a CPU sensor."""
_attr_icon = "mdi:pulse"
_attr_name = "CPU Speed"
_attr_has_entity_name = True
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
def __init__(self, entry: ConfigEntry) -> None:
"""Initialize the CPU sensor."""
self._attr_unique_id = entry.entry_id
self._attr_device_info = DeviceInfo(
name="CPU Speed",
identifiers={(DOMAIN, entry.entry_id)},
)
def update(self) -> None:
"""Get the latest data and updates the state."""

View File

@ -2,6 +2,7 @@
from unittest.mock import MagicMock
from homeassistant.components.cpuspeed.const import DOMAIN
from homeassistant.components.cpuspeed.sensor import ATTR_ARCH, ATTR_BRAND, ATTR_HZ
from homeassistant.components.homeassistant import (
DOMAIN as HOME_ASSISTANT_DOMAIN,
@ -15,7 +16,7 @@ from homeassistant.const import (
STATE_UNKNOWN,
)
from homeassistant.core import HomeAssistant
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers import device_registry as dr, entity_registry as er
from homeassistant.setup import async_setup_component
from tests.common import MockConfigEntry
@ -29,6 +30,7 @@ async def test_sensor(
"""Test the CPU Speed sensor."""
await async_setup_component(hass, "homeassistant", {})
entity_registry = er.async_get(hass)
device_registry = dr.async_get(hass)
entry = entity_registry.async_get("sensor.cpu_speed")
assert entry
@ -62,6 +64,12 @@ async def test_sensor(
assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7"
assert state.attributes.get(ATTR_HZ) == 3.6
assert entry.device_id
device_entry = device_registry.async_get(entry.device_id)
assert device_entry
assert device_entry.identifiers == {(DOMAIN, entry.config_entry_id)}
assert device_entry.name == "CPU Speed"
async def test_sensor_partial_info(
hass: HomeAssistant,