Migrate Uptime to new entity naming style (#75090)
This commit is contained in:
parent
0f3cc4a4aa
commit
8d88562d40
2 changed files with 18 additions and 2 deletions
|
@ -12,6 +12,8 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||||
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
from homeassistant.const import CONF_NAME, CONF_UNIT_OF_MEASUREMENT
|
||||||
from homeassistant.core import HomeAssistant
|
from homeassistant.core import HomeAssistant
|
||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||||
|
from homeassistant.helpers.entity import DeviceInfo
|
||||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||||
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
|
||||||
import homeassistant.util.dt as dt_util
|
import homeassistant.util.dt as dt_util
|
||||||
|
@ -58,10 +60,15 @@ class UptimeSensor(SensorEntity):
|
||||||
"""Representation of an uptime sensor."""
|
"""Representation of an uptime sensor."""
|
||||||
|
|
||||||
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
_attr_device_class = SensorDeviceClass.TIMESTAMP
|
||||||
|
_attr_has_entity_name = True
|
||||||
_attr_should_poll = False
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, entry: ConfigEntry) -> None:
|
def __init__(self, entry: ConfigEntry) -> None:
|
||||||
"""Initialize the uptime sensor."""
|
"""Initialize the uptime sensor."""
|
||||||
self._attr_name = entry.title
|
|
||||||
self._attr_native_value = dt_util.utcnow()
|
self._attr_native_value = dt_util.utcnow()
|
||||||
self._attr_unique_id = entry.entry_id
|
self._attr_unique_id = entry.entry_id
|
||||||
|
self._attr_device_info = DeviceInfo(
|
||||||
|
name=entry.title,
|
||||||
|
identifiers={(DOMAIN, entry.entry_id)},
|
||||||
|
entry_type=DeviceEntryType.SERVICE,
|
||||||
|
)
|
||||||
|
|
|
@ -2,9 +2,10 @@
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from homeassistant.components.sensor import SensorDeviceClass
|
from homeassistant.components.sensor import SensorDeviceClass
|
||||||
|
from homeassistant.components.uptime.const import DOMAIN
|
||||||
from homeassistant.const import ATTR_DEVICE_CLASS
|
from homeassistant.const import ATTR_DEVICE_CLASS
|
||||||
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 tests.common import MockConfigEntry
|
from tests.common import MockConfigEntry
|
||||||
|
|
||||||
|
@ -25,3 +26,11 @@ async def test_uptime_sensor(
|
||||||
entry = entity_registry.async_get("sensor.uptime")
|
entry = entity_registry.async_get("sensor.uptime")
|
||||||
assert entry
|
assert entry
|
||||||
assert entry.unique_id == init_integration.entry_id
|
assert entry.unique_id == init_integration.entry_id
|
||||||
|
|
||||||
|
device_registry = dr.async_get(hass)
|
||||||
|
assert entry.device_id
|
||||||
|
device_entry = device_registry.async_get(entry.device_id)
|
||||||
|
assert device_entry
|
||||||
|
assert device_entry.identifiers == {(DOMAIN, init_integration.entry_id)}
|
||||||
|
assert device_entry.name == init_integration.title
|
||||||
|
assert device_entry.entry_type == dr.DeviceEntryType.SERVICE
|
||||||
|
|
Loading…
Add table
Reference in a new issue