Migrate CPUSpeed to new entity naming style (#75080)
This commit is contained in:
parent
b62ebbe974
commit
05d2b955ee
2 changed files with 17 additions and 2 deletions
|
@ -7,8 +7,11 @@ from homeassistant.components.sensor import SensorEntity
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
from homeassistant.const import FREQUENCY_GIGAHERTZ
|
from homeassistant.const import FREQUENCY_GIGAHERTZ
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
|
|
||||||
|
from .const import DOMAIN
|
||||||
|
|
||||||
ATTR_BRAND = "brand"
|
ATTR_BRAND = "brand"
|
||||||
ATTR_HZ = "ghz_advertised"
|
ATTR_HZ = "ghz_advertised"
|
||||||
ATTR_ARCH = "arch"
|
ATTR_ARCH = "arch"
|
||||||
|
@ -30,12 +33,16 @@ class CPUSpeedSensor(SensorEntity):
|
||||||
"""Representation of a CPU sensor."""
|
"""Representation of a CPU sensor."""
|
||||||
|
|
||||||
_attr_icon = "mdi:pulse"
|
_attr_icon = "mdi:pulse"
|
||||||
_attr_name = "CPU Speed"
|
_attr_has_entity_name = True
|
||||||
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
_attr_native_unit_of_measurement = FREQUENCY_GIGAHERTZ
|
||||||
|
|
||||||
def __init__(self, entry: ConfigEntry) -> None:
|
def __init__(self, entry: ConfigEntry) -> None:
|
||||||
"""Initialize the CPU sensor."""
|
"""Initialize the CPU sensor."""
|
||||||
self._attr_unique_id = entry.entry_id
|
self._attr_unique_id = entry.entry_id
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
name="CPU Speed",
|
||||||
|
identifiers={(DOMAIN, entry.entry_id)},
|
||||||
|
)
|
||||||
|
|
||||||
def update(self) -> None:
|
def update(self) -> None:
|
||||||
"""Get the latest data and updates the state."""
|
"""Get the latest data and updates the state."""
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from unittest.mock import MagicMock
|
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.cpuspeed.sensor import ATTR_ARCH, ATTR_BRAND, ATTR_HZ
|
||||||
from homeassistant.components.homeassistant import (
|
from homeassistant.components.homeassistant import (
|
||||||
DOMAIN as HOME_ASSISTANT_DOMAIN,
|
DOMAIN as HOME_ASSISTANT_DOMAIN,
|
||||||
|
@ -15,7 +16,7 @@ from homeassistant.const import (
|
||||||
STATE_UNKNOWN,
|
STATE_UNKNOWN,
|
||||||
)
|
)
|
||||||
from homeassistant.core import HomeAssistant
|
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 homeassistant.setup import async_setup_component
|
||||||
|
|
||||||
from tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
@ -29,6 +30,7 @@ async def test_sensor(
|
||||||
"""Test the CPU Speed sensor."""
|
"""Test the CPU Speed sensor."""
|
||||||
await async_setup_component(hass, "homeassistant", {})
|
await async_setup_component(hass, "homeassistant", {})
|
||||||
entity_registry = er.async_get(hass)
|
entity_registry = er.async_get(hass)
|
||||||
|
device_registry = dr.async_get(hass)
|
||||||
|
|
||||||
entry = entity_registry.async_get("sensor.cpu_speed")
|
entry = entity_registry.async_get("sensor.cpu_speed")
|
||||||
assert entry
|
assert entry
|
||||||
|
@ -62,6 +64,12 @@ async def test_sensor(
|
||||||
assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7"
|
assert state.attributes.get(ATTR_BRAND) == "Intel Ryzen 7"
|
||||||
assert state.attributes.get(ATTR_HZ) == 3.6
|
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(
|
async def test_sensor_partial_info(
|
||||||
hass: HomeAssistant,
|
hass: HomeAssistant,
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue