mirror of
https://github.com/home-assistant/core.git
synced 2025-04-23 08:47:57 +00:00
Migrate Epson to has entity name (#98164)
This commit is contained in:
parent
bd27358398
commit
4a5b1ab301
@ -37,7 +37,10 @@ from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import entity_platform
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
from homeassistant.helpers.device_registry import DeviceInfo
|
||||
from homeassistant.helpers.device_registry import (
|
||||
DeviceInfo,
|
||||
async_get as async_get_device_registry,
|
||||
)
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.helpers.entity_registry import async_get as async_get_entity_registry
|
||||
|
||||
@ -55,8 +58,7 @@ async def async_setup_entry(
|
||||
projector: Projector = hass.data[DOMAIN][config_entry.entry_id]
|
||||
projector_entity = EpsonProjectorMediaPlayer(
|
||||
projector=projector,
|
||||
name=config_entry.title,
|
||||
unique_id=config_entry.unique_id,
|
||||
unique_id=config_entry.unique_id or config_entry.entry_id,
|
||||
entry=config_entry,
|
||||
)
|
||||
async_add_entities([projector_entity], True)
|
||||
@ -71,6 +73,9 @@ async def async_setup_entry(
|
||||
class EpsonProjectorMediaPlayer(MediaPlayerEntity):
|
||||
"""Representation of Epson Projector Device."""
|
||||
|
||||
_attr_has_entity_name = True
|
||||
_attr_name = None
|
||||
|
||||
_attr_supported_features = (
|
||||
MediaPlayerEntityFeature.TURN_ON
|
||||
| MediaPlayerEntityFeature.TURN_OFF
|
||||
@ -82,38 +87,38 @@ class EpsonProjectorMediaPlayer(MediaPlayerEntity):
|
||||
)
|
||||
|
||||
def __init__(
|
||||
self, projector: Projector, name: str, unique_id: str | None, entry: ConfigEntry
|
||||
self, projector: Projector, unique_id: str, entry: ConfigEntry
|
||||
) -> None:
|
||||
"""Initialize entity to control Epson projector."""
|
||||
self._projector = projector
|
||||
self._entry = entry
|
||||
self._attr_name = name
|
||||
self._attr_available = False
|
||||
self._cmode = None
|
||||
self._attr_source_list = list(DEFAULT_SOURCES.values())
|
||||
self._attr_unique_id = unique_id
|
||||
if unique_id:
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
manufacturer="Epson",
|
||||
model="Epson",
|
||||
name="Epson projector",
|
||||
via_device=(DOMAIN, unique_id),
|
||||
)
|
||||
self._attr_device_info = DeviceInfo(
|
||||
identifiers={(DOMAIN, unique_id)},
|
||||
manufacturer="Epson",
|
||||
model="Epson",
|
||||
)
|
||||
|
||||
async def set_unique_id(self) -> bool:
|
||||
"""Set unique id for projector config entry."""
|
||||
_LOGGER.debug("Setting unique_id for projector")
|
||||
if self.unique_id:
|
||||
if self._entry.unique_id:
|
||||
return False
|
||||
if uid := await self._projector.get_serial_number():
|
||||
self.hass.config_entries.async_update_entry(self._entry, unique_id=uid)
|
||||
registry = async_get_entity_registry(self.hass)
|
||||
old_entity_id = registry.async_get_entity_id(
|
||||
ent_reg = async_get_entity_registry(self.hass)
|
||||
old_entity_id = ent_reg.async_get_entity_id(
|
||||
"media_player", DOMAIN, self._entry.entry_id
|
||||
)
|
||||
if old_entity_id is not None:
|
||||
registry.async_update_entity(old_entity_id, new_unique_id=uid)
|
||||
ent_reg.async_update_entity(old_entity_id, new_unique_id=uid)
|
||||
dev_reg = async_get_device_registry(self.hass)
|
||||
device = dev_reg.async_get_device({(DOMAIN, self._entry.entry_id)})
|
||||
if device is not None:
|
||||
dev_reg.async_update_device(device.id, new_identifiers={(DOMAIN, uid)})
|
||||
self.hass.async_create_task(
|
||||
self.hass.config_entries.async_reload(self._entry.entry_id)
|
||||
)
|
||||
|
49
tests/components/epson/test_media_player.py
Normal file
49
tests/components/epson/test_media_player.py
Normal file
@ -0,0 +1,49 @@
|
||||
"""Tests for the epson integration."""
|
||||
from datetime import timedelta
|
||||
from unittest.mock import patch
|
||||
|
||||
from freezegun.api import FrozenDateTimeFactory
|
||||
|
||||
from homeassistant.components.epson.const import DOMAIN
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import device_registry as dr, entity_registry as er
|
||||
|
||||
from tests.common import MockConfigEntry, async_fire_time_changed
|
||||
|
||||
|
||||
async def test_set_unique_id(
|
||||
hass: HomeAssistant,
|
||||
entity_registry: er.EntityRegistry,
|
||||
device_registry: dr.DeviceRegistry,
|
||||
freezer: FrozenDateTimeFactory,
|
||||
):
|
||||
"""Test the unique id is set on runtime."""
|
||||
entry = MockConfigEntry(
|
||||
domain=DOMAIN,
|
||||
title="Epson",
|
||||
data={CONF_HOST: "1.1.1.1"},
|
||||
entry_id="1cb78c095906279574a0442a1f0003ef",
|
||||
)
|
||||
entry.add_to_hass(hass)
|
||||
with patch("homeassistant.components.epson.Projector.get_power"):
|
||||
await hass.config_entries.async_setup(entry.entry_id)
|
||||
await hass.async_block_till_done()
|
||||
assert entry.unique_id is None
|
||||
entity = entity_registry.async_get("media_player.epson")
|
||||
assert entity
|
||||
assert entity.unique_id == entry.entry_id
|
||||
with patch(
|
||||
"homeassistant.components.epson.Projector.get_power", return_value="01"
|
||||
), patch(
|
||||
"homeassistant.components.epson.Projector.get_serial_number", return_value="123"
|
||||
), patch(
|
||||
"homeassistant.components.epson.Projector.get_property"
|
||||
):
|
||||
freezer.tick(timedelta(seconds=30))
|
||||
async_fire_time_changed(hass)
|
||||
await hass.async_block_till_done()
|
||||
entity = entity_registry.async_get("media_player.epson")
|
||||
assert entity
|
||||
assert entity.unique_id == "123"
|
||||
assert entry.unique_id == "123"
|
Loading…
x
Reference in New Issue
Block a user