Migrate Moon to new entity naming style (#75085)

This commit is contained in:
Franck Nijhof 2022-07-20 11:24:15 +02:00 committed by GitHub
parent 3d31e62683
commit 0e59e8b925
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 5 deletions

View File

@ -12,6 +12,8 @@ from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
from homeassistant.const import CONF_NAME
from homeassistant.core import HomeAssistant
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.typing import ConfigType, DiscoveryInfoType
import homeassistant.util.dt as dt_util
@ -72,11 +74,17 @@ class MoonSensorEntity(SensorEntity):
"""Representation of a Moon sensor."""
_attr_device_class = "moon__phase"
_attr_has_entity_name = True
_attr_name = "Phase"
def __init__(self, entry: ConfigEntry) -> None:
"""Initialize the moon sensor."""
self._attr_name = entry.title
self._attr_unique_id = entry.entry_id
self._attr_device_info = DeviceInfo(
name="Moon",
identifiers={(DOMAIN, entry.entry_id)},
entry_type=DeviceEntryType.SERVICE,
)
async def async_update(self) -> None:
"""Get the time and updates the states."""

View File

@ -16,9 +16,9 @@ from homeassistant.components.moon.sensor import (
STATE_WAXING_CRESCENT,
STATE_WAXING_GIBBOUS,
)
from homeassistant.const import ATTR_ICON
from homeassistant.const import ATTR_FRIENDLY_NAME, ATTR_ICON
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
@ -52,12 +52,20 @@ async def test_moon_day(
await hass.config_entries.async_setup(mock_config_entry.entry_id)
await hass.async_block_till_done()
state = hass.states.get("sensor.moon")
state = hass.states.get("sensor.moon_phase")
assert state
assert state.state == native_value
assert state.attributes[ATTR_ICON] == icon
assert state.attributes[ATTR_FRIENDLY_NAME] == "Moon Phase"
entity_registry = er.async_get(hass)
entry = entity_registry.async_get("sensor.moon")
entry = entity_registry.async_get("sensor.moon_phase")
assert entry
assert entry.unique_id == mock_config_entry.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.name == "Moon"
assert device_entry.entry_type is dr.DeviceEntryType.SERVICE