Migrate roku to new entity naming (#74819)

* migrate roku to new entity naming

* Update binary_sensor.py

* Update sensor.py

* Update test_binary_sensor.py

* Update sensor.py

* Update entity.py

* Update media_player.py

* Update remote.py

* Update media_player.py

* Update remote.py

* Update entity.py

* Update entity.py

* Update entity.py

* Update entity.py
This commit is contained in:
Chris Talkington 2022-07-14 07:02:13 -05:00 committed by GitHub
parent e16bd1e471
commit 20432ccc76
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 55 additions and 59 deletions

View File

@ -36,7 +36,7 @@ class RokuBinarySensorEntityDescription(
BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = ( BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = (
RokuBinarySensorEntityDescription( RokuBinarySensorEntityDescription(
key="headphones_connected", key="headphones_connected",
name="Headphones Connected", name="Headphones connected",
icon="mdi:headphones", icon="mdi:headphones",
value_fn=lambda device: device.info.headphones_connected, value_fn=lambda device: device.info.headphones_connected,
), ),
@ -49,14 +49,14 @@ BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = (
), ),
RokuBinarySensorEntityDescription( RokuBinarySensorEntityDescription(
key="supports_ethernet", key="supports_ethernet",
name="Supports Ethernet", name="Supports ethernet",
icon="mdi:ethernet", icon="mdi:ethernet",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda device: device.info.ethernet_support, value_fn=lambda device: device.info.ethernet_support,
), ),
RokuBinarySensorEntityDescription( RokuBinarySensorEntityDescription(
key="supports_find_remote", key="supports_find_remote",
name="Supports Find Remote", name="Supports find remote",
icon="mdi:remote", icon="mdi:remote",
entity_category=EntityCategory.DIAGNOSTIC, entity_category=EntityCategory.DIAGNOSTIC,
value_fn=lambda device: device.info.supports_find_remote, value_fn=lambda device: device.info.supports_find_remote,

View File

@ -25,18 +25,20 @@ class RokuEntity(CoordinatorEntity[RokuDataUpdateCoordinator]):
if description is not None: if description is not None:
self.entity_description = description self.entity_description = description
if device_id is None:
self._attr_name = f"{coordinator.data.info.name} {description.name}" self._attr_name = f"{coordinator.data.info.name} {description.name}"
if device_id is not None: if device_id is not None:
self._attr_has_entity_name = True
if description is not None:
self._attr_unique_id = f"{device_id}_{description.key}" self._attr_unique_id = f"{device_id}_{description.key}"
else:
self._attr_unique_id = device_id
@property self._attr_device_info = DeviceInfo(
def device_info(self) -> DeviceInfo | None: identifiers={(DOMAIN, device_id)},
"""Return device information about this Roku device."""
if self._device_id is None:
return None
return DeviceInfo(
identifiers={(DOMAIN, self._device_id)},
connections={ connections={
(CONNECTION_NETWORK_MAC, mac_address) (CONNECTION_NETWORK_MAC, mac_address)
for mac_address in ( for mac_address in (

View File

@ -99,7 +99,15 @@ async def async_setup_entry(
"""Set up the Roku config entry.""" """Set up the Roku config entry."""
coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
unique_id = coordinator.data.info.serial_number unique_id = coordinator.data.info.serial_number
async_add_entities([RokuMediaPlayer(unique_id, coordinator)], True) async_add_entities(
[
RokuMediaPlayer(
device_id=unique_id,
coordinator=coordinator,
)
],
True,
)
platform = entity_platform.async_get_current_platform() platform = entity_platform.async_get_current_platform()
@ -127,18 +135,6 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
| MediaPlayerEntityFeature.BROWSE_MEDIA | MediaPlayerEntityFeature.BROWSE_MEDIA
) )
def __init__(
self, unique_id: str | None, coordinator: RokuDataUpdateCoordinator
) -> None:
"""Initialize the Roku device."""
super().__init__(
coordinator=coordinator,
device_id=unique_id,
)
self._attr_name = coordinator.data.info.name
self._attr_unique_id = unique_id
def _media_playback_trackable(self) -> bool: def _media_playback_trackable(self) -> bool:
"""Detect if we have enough media data to track playback.""" """Detect if we have enough media data to track playback."""
if self.coordinator.data.media is None or self.coordinator.data.media.live: if self.coordinator.data.media is None or self.coordinator.data.media.live:

View File

@ -21,24 +21,22 @@ async def async_setup_entry(
async_add_entities: AddEntitiesCallback, async_add_entities: AddEntitiesCallback,
) -> None: ) -> None:
"""Load Roku remote based on a config entry.""" """Load Roku remote based on a config entry."""
coordinator = hass.data[DOMAIN][entry.entry_id] coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id]
unique_id = coordinator.data.info.serial_number unique_id = coordinator.data.info.serial_number
async_add_entities([RokuRemote(unique_id, coordinator)], True) async_add_entities(
[
RokuRemote(
device_id=unique_id,
coordinator=coordinator,
)
],
True,
)
class RokuRemote(RokuEntity, RemoteEntity): class RokuRemote(RokuEntity, RemoteEntity):
"""Device that sends commands to an Roku.""" """Device that sends commands to an Roku."""
def __init__(self, unique_id: str, coordinator: RokuDataUpdateCoordinator) -> None:
"""Initialize the Roku device."""
super().__init__(
device_id=unique_id,
coordinator=coordinator,
)
self._attr_name = coordinator.data.info.name
self._attr_unique_id = unique_id
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
"""Return true if device is on.""" """Return true if device is on."""

View File

@ -29,7 +29,7 @@ async def test_roku_binary_sensors(
assert entry.unique_id == f"{UPNP_SERIAL}_headphones_connected" assert entry.unique_id == f"{UPNP_SERIAL}_headphones_connected"
assert entry.entity_category is None assert entry.entity_category is None
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Headphones Connected" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Headphones connected"
assert state.attributes.get(ATTR_ICON) == "mdi:headphones" assert state.attributes.get(ATTR_ICON) == "mdi:headphones"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
@ -51,7 +51,7 @@ async def test_roku_binary_sensors(
assert entry.unique_id == f"{UPNP_SERIAL}_supports_ethernet" assert entry.unique_id == f"{UPNP_SERIAL}_supports_ethernet"
assert entry.entity_category == EntityCategory.DIAGNOSTIC assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == STATE_ON assert state.state == STATE_ON
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Supports Ethernet" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Supports ethernet"
assert state.attributes.get(ATTR_ICON) == "mdi:ethernet" assert state.attributes.get(ATTR_ICON) == "mdi:ethernet"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
@ -62,7 +62,7 @@ async def test_roku_binary_sensors(
assert entry.unique_id == f"{UPNP_SERIAL}_supports_find_remote" assert entry.unique_id == f"{UPNP_SERIAL}_supports_find_remote"
assert entry.entity_category == EntityCategory.DIAGNOSTIC assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Supports Find Remote" assert state.attributes.get(ATTR_FRIENDLY_NAME) == "My Roku 3 Supports find remote"
assert state.attributes.get(ATTR_ICON) == "mdi:remote" assert state.attributes.get(ATTR_ICON) == "mdi:remote"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
@ -105,7 +105,7 @@ async def test_rokutv_binary_sensors(
assert state.state == STATE_OFF assert state.state == STATE_OFF
assert ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) state.attributes.get(ATTR_FRIENDLY_NAME)
== '58" Onn Roku TV Headphones Connected' == '58" Onn Roku TV Headphones connected'
) )
assert state.attributes.get(ATTR_ICON) == "mdi:headphones" assert state.attributes.get(ATTR_ICON) == "mdi:headphones"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
@ -131,7 +131,7 @@ async def test_rokutv_binary_sensors(
assert entry.entity_category == EntityCategory.DIAGNOSTIC assert entry.entity_category == EntityCategory.DIAGNOSTIC
assert state.state == STATE_ON assert state.state == STATE_ON
assert ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) == '58" Onn Roku TV Supports Ethernet' state.attributes.get(ATTR_FRIENDLY_NAME) == '58" Onn Roku TV Supports ethernet'
) )
assert state.attributes.get(ATTR_ICON) == "mdi:ethernet" assert state.attributes.get(ATTR_ICON) == "mdi:ethernet"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes
@ -147,7 +147,7 @@ async def test_rokutv_binary_sensors(
assert state.state == STATE_ON assert state.state == STATE_ON
assert ( assert (
state.attributes.get(ATTR_FRIENDLY_NAME) state.attributes.get(ATTR_FRIENDLY_NAME)
== '58" Onn Roku TV Supports Find Remote' == '58" Onn Roku TV Supports find remote'
) )
assert state.attributes.get(ATTR_ICON) == "mdi:remote" assert state.attributes.get(ATTR_ICON) == "mdi:remote"
assert ATTR_DEVICE_CLASS not in state.attributes assert ATTR_DEVICE_CLASS not in state.attributes