From 20432ccc7601f46ce0f84f5b539ada2d9b5b3381 Mon Sep 17 00:00:00 2001 From: Chris Talkington Date: Thu, 14 Jul 2022 07:02:13 -0500 Subject: [PATCH] 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 --- .../components/roku/binary_sensor.py | 6 +-- homeassistant/components/roku/entity.py | 52 ++++++++++--------- homeassistant/components/roku/media_player.py | 22 ++++---- homeassistant/components/roku/remote.py | 22 ++++---- tests/components/roku/test_binary_sensor.py | 12 ++--- 5 files changed, 55 insertions(+), 59 deletions(-) diff --git a/homeassistant/components/roku/binary_sensor.py b/homeassistant/components/roku/binary_sensor.py index 5b6da073dd1..243ea994dfa 100644 --- a/homeassistant/components/roku/binary_sensor.py +++ b/homeassistant/components/roku/binary_sensor.py @@ -36,7 +36,7 @@ class RokuBinarySensorEntityDescription( BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = ( RokuBinarySensorEntityDescription( key="headphones_connected", - name="Headphones Connected", + name="Headphones connected", icon="mdi:headphones", value_fn=lambda device: device.info.headphones_connected, ), @@ -49,14 +49,14 @@ BINARY_SENSORS: tuple[RokuBinarySensorEntityDescription, ...] = ( ), RokuBinarySensorEntityDescription( key="supports_ethernet", - name="Supports Ethernet", + name="Supports ethernet", icon="mdi:ethernet", entity_category=EntityCategory.DIAGNOSTIC, value_fn=lambda device: device.info.ethernet_support, ), RokuBinarySensorEntityDescription( key="supports_find_remote", - name="Supports Find Remote", + name="Supports find remote", icon="mdi:remote", entity_category=EntityCategory.DIAGNOSTIC, value_fn=lambda device: device.info.supports_find_remote, diff --git a/homeassistant/components/roku/entity.py b/homeassistant/components/roku/entity.py index 39373c96c6a..a85024f8220 100644 --- a/homeassistant/components/roku/entity.py +++ b/homeassistant/components/roku/entity.py @@ -25,30 +25,32 @@ class RokuEntity(CoordinatorEntity[RokuDataUpdateCoordinator]): if description is not None: self.entity_description = description - self._attr_name = f"{coordinator.data.info.name} {description.name}" - if device_id is not None: + + if device_id is None: + self._attr_name = f"{coordinator.data.info.name} {description.name}" + + 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}" + else: + self._attr_unique_id = device_id - @property - def device_info(self) -> DeviceInfo | None: - """Return device information about this Roku device.""" - if self._device_id is None: - return None - - return DeviceInfo( - identifiers={(DOMAIN, self._device_id)}, - connections={ - (CONNECTION_NETWORK_MAC, mac_address) - for mac_address in ( - self.coordinator.data.info.wifi_mac, - self.coordinator.data.info.ethernet_mac, - ) - if mac_address is not None - }, - name=self.coordinator.data.info.name, - manufacturer=self.coordinator.data.info.brand, - model=self.coordinator.data.info.model_name, - hw_version=self.coordinator.data.info.model_number, - sw_version=self.coordinator.data.info.version, - suggested_area=self.coordinator.data.info.device_location, - ) + self._attr_device_info = DeviceInfo( + identifiers={(DOMAIN, device_id)}, + connections={ + (CONNECTION_NETWORK_MAC, mac_address) + for mac_address in ( + self.coordinator.data.info.wifi_mac, + self.coordinator.data.info.ethernet_mac, + ) + if mac_address is not None + }, + name=self.coordinator.data.info.name, + manufacturer=self.coordinator.data.info.brand, + model=self.coordinator.data.info.model_name, + hw_version=self.coordinator.data.info.model_number, + sw_version=self.coordinator.data.info.version, + suggested_area=self.coordinator.data.info.device_location, + ) diff --git a/homeassistant/components/roku/media_player.py b/homeassistant/components/roku/media_player.py index a47432694dd..d7b9a3489c9 100644 --- a/homeassistant/components/roku/media_player.py +++ b/homeassistant/components/roku/media_player.py @@ -99,7 +99,15 @@ async def async_setup_entry( """Set up the Roku config entry.""" coordinator: RokuDataUpdateCoordinator = hass.data[DOMAIN][entry.entry_id] 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() @@ -127,18 +135,6 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity): | 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: """Detect if we have enough media data to track playback.""" if self.coordinator.data.media is None or self.coordinator.data.media.live: diff --git a/homeassistant/components/roku/remote.py b/homeassistant/components/roku/remote.py index 6d1312c0b03..fceac67a477 100644 --- a/homeassistant/components/roku/remote.py +++ b/homeassistant/components/roku/remote.py @@ -21,24 +21,22 @@ async def async_setup_entry( async_add_entities: AddEntitiesCallback, ) -> None: """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 - async_add_entities([RokuRemote(unique_id, coordinator)], True) + async_add_entities( + [ + RokuRemote( + device_id=unique_id, + coordinator=coordinator, + ) + ], + True, + ) class RokuRemote(RokuEntity, RemoteEntity): """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 def is_on(self) -> bool: """Return true if device is on.""" diff --git a/tests/components/roku/test_binary_sensor.py b/tests/components/roku/test_binary_sensor.py index 24f92b0b11b..706b1eceddb 100644 --- a/tests/components/roku/test_binary_sensor.py +++ b/tests/components/roku/test_binary_sensor.py @@ -29,7 +29,7 @@ async def test_roku_binary_sensors( assert entry.unique_id == f"{UPNP_SERIAL}_headphones_connected" assert entry.entity_category is None 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 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.entity_category == EntityCategory.DIAGNOSTIC 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 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.entity_category == EntityCategory.DIAGNOSTIC 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 ATTR_DEVICE_CLASS not in state.attributes @@ -105,7 +105,7 @@ async def test_rokutv_binary_sensors( assert state.state == STATE_OFF assert ( 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 ATTR_DEVICE_CLASS not in state.attributes @@ -131,7 +131,7 @@ async def test_rokutv_binary_sensors( assert entry.entity_category == EntityCategory.DIAGNOSTIC assert state.state == STATE_ON 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 ATTR_DEVICE_CLASS not in state.attributes @@ -147,7 +147,7 @@ async def test_rokutv_binary_sensors( assert state.state == STATE_ON assert ( 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 ATTR_DEVICE_CLASS not in state.attributes