Use attrs instead of properties in roku (#51735)

* Use attrs instead of properties in roku.

* Update media_player.py

* Update remote.py

* Update __init__.py

* Create entity.py

* Update entity.py

* Update media_player.py

* Update remote.py

* Update __init__.py

* Update media_player.py

* Update remote.py

* Update __init__.py

* Update __init__.py

* Update entity.py
This commit is contained in:
Chris Talkington 2021-06-11 06:51:18 -05:00 committed by GitHub
parent e0013648f6
commit fa3ae9b83c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 53 additions and 63 deletions

View file

@ -10,26 +10,14 @@ from rokuecp.models import Device
from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.components.remote import DOMAIN as REMOTE_DOMAIN
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import ATTR_NAME, CONF_HOST
from homeassistant.const import CONF_HOST
from homeassistant.core import HomeAssistant
from homeassistant.helpers import config_validation as cv
from homeassistant.helpers.aiohttp_client import async_get_clientsession
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import (
CoordinatorEntity,
DataUpdateCoordinator,
UpdateFailed,
)
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed
from homeassistant.util.dt import utcnow
from .const import (
ATTR_IDENTIFIERS,
ATTR_MANUFACTURER,
ATTR_MODEL,
ATTR_SOFTWARE_VERSION,
ATTR_SUGGESTED_AREA,
DOMAIN,
)
from .const import DOMAIN
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
@ -114,35 +102,3 @@ class RokuDataUpdateCoordinator(DataUpdateCoordinator[Device]):
return data
except RokuError as error:
raise UpdateFailed(f"Invalid response from API: {error}") from error
class RokuEntity(CoordinatorEntity):
"""Defines a base Roku entity."""
def __init__(
self, *, device_id: str, name: str, coordinator: RokuDataUpdateCoordinator
) -> None:
"""Initialize the Roku entity."""
super().__init__(coordinator)
self._device_id = device_id
self._name = name
@property
def name(self) -> str:
"""Return the name of the entity."""
return self._name
@property
def device_info(self) -> DeviceInfo:
"""Return device information about this Roku device."""
if self._device_id is None:
return None
return {
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
ATTR_NAME: self.name,
ATTR_MANUFACTURER: self.coordinator.data.info.brand,
ATTR_MODEL: self.coordinator.data.info.model_name,
ATTR_SOFTWARE_VERSION: self.coordinator.data.info.version,
ATTR_SUGGESTED_AREA: self.coordinator.data.info.device_location,
}

View file

@ -0,0 +1,42 @@
"""Base Entity for Roku."""
from __future__ import annotations
from homeassistant.const import ATTR_NAME
from homeassistant.helpers.entity import DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from . import RokuDataUpdateCoordinator
from .const import (
ATTR_IDENTIFIERS,
ATTR_MANUFACTURER,
ATTR_MODEL,
ATTR_SOFTWARE_VERSION,
ATTR_SUGGESTED_AREA,
DOMAIN,
)
class RokuEntity(CoordinatorEntity):
"""Defines a base Roku entity."""
def __init__(
self, *, device_id: str, coordinator: RokuDataUpdateCoordinator
) -> None:
"""Initialize the Roku entity."""
super().__init__(coordinator)
self._device_id = device_id
@property
def device_info(self) -> DeviceInfo:
"""Return device information about this Roku device."""
if self._device_id is None:
return None
return {
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
ATTR_NAME: self.name,
ATTR_MANUFACTURER: self.coordinator.data.info.brand,
ATTR_MODEL: self.coordinator.data.info.model_name,
ATTR_SOFTWARE_VERSION: self.coordinator.data.info.version,
ATTR_SUGGESTED_AREA: self.coordinator.data.info.device_location,
}

View file

@ -37,9 +37,10 @@ from homeassistant.const import (
from homeassistant.helpers import entity_platform
from homeassistant.helpers.network import is_internal_request
from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from . import RokuDataUpdateCoordinator, roku_exception_handler
from .browse_media import build_item_response, library_payload
from .const import ATTR_KEYWORD, DOMAIN, SERVICE_SEARCH
from .entity import RokuEntity
_LOGGER = logging.getLogger(__name__)
@ -82,11 +83,11 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
"""Initialize the Roku device."""
super().__init__(
coordinator=coordinator,
name=coordinator.data.info.name,
device_id=unique_id,
)
self._unique_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."""
@ -95,11 +96,6 @@ class RokuMediaPlayer(RokuEntity, MediaPlayerEntity):
return self.coordinator.data.media.duration > 0
@property
def unique_id(self) -> str:
"""Return the unique ID for this entity."""
return self._unique_id
@property
def device_class(self) -> str | None:
"""Return the class of this device."""

View file

@ -6,8 +6,9 @@ from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from . import RokuDataUpdateCoordinator, RokuEntity, roku_exception_handler
from . import RokuDataUpdateCoordinator, roku_exception_handler
from .const import DOMAIN
from .entity import RokuEntity
async def async_setup_entry(
@ -28,16 +29,11 @@ class RokuRemote(RokuEntity, RemoteEntity):
"""Initialize the Roku device."""
super().__init__(
device_id=unique_id,
name=coordinator.data.info.name,
coordinator=coordinator,
)
self._unique_id = unique_id
@property
def unique_id(self) -> str:
"""Return the unique ID for this entity."""
return self._unique_id
self._attr_name = coordinator.data.info.name
self._attr_unique_id = unique_id
@property
def is_on(self) -> bool: