Skip to content

Commit

Permalink
Migrate Epson to has entity name (home-assistant#98164)
Browse files Browse the repository at this point in the history
  • Loading branch information
joostlek authored Nov 26, 2023
1 parent bd27358 commit 4a5b1ab
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 17 deletions.
39 changes: 22 additions & 17 deletions homeassistant/components/epson/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,10 @@
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

Expand All @@ -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)
Expand All @@ -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
Expand All @@ -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)
)
Expand Down
49 changes: 49 additions & 0 deletions tests/components/epson/test_media_player.py
Original file line number Diff line number Diff line change
@@ -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"

0 comments on commit 4a5b1ab

Please sign in to comment.