Use attrs instead of properties for directv (#51918)
* Use attrs instead of properties for directv * Update __init__.py * Create entity.py * Update media_player.py * Update remote.py * Update media_player.py * Update remote.py * Update media_player.py * Update remote.py * Update entity.py * Update __init__.py * Update media_player.py * Update remote.py * Update media_player.py * Update media_player.py
This commit is contained in:
parent
2351f2d95e
commit
4e88b44286
4 changed files with 68 additions and 108 deletions
homeassistant/components/directv
|
@ -6,21 +6,13 @@ from datetime import timedelta
|
|||
from directv import DIRECTV, DIRECTVError
|
||||
|
||||
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.exceptions import ConfigEntryNotReady
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import (
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_MODEL,
|
||||
ATTR_SOFTWARE_VERSION,
|
||||
ATTR_VIA_DEVICE,
|
||||
DOMAIN,
|
||||
)
|
||||
from .const import DOMAIN
|
||||
|
||||
CONFIG_SCHEMA = cv.deprecated(DOMAIN)
|
||||
|
||||
|
@ -52,32 +44,3 @@ async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
|||
hass.data[DOMAIN].pop(entry.entry_id)
|
||||
|
||||
return unload_ok
|
||||
|
||||
|
||||
class DIRECTVEntity(Entity):
|
||||
"""Defines a base DirecTV entity."""
|
||||
|
||||
def __init__(self, *, dtv: DIRECTV, name: str, address: str = "0") -> None:
|
||||
"""Initialize the DirecTV entity."""
|
||||
self._address = address
|
||||
self._device_id = address if address != "0" else dtv.device.info.receiver_id
|
||||
self._is_client = address != "0"
|
||||
self._name = name
|
||||
self.dtv = dtv
|
||||
|
||||
@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 DirecTV receiver."""
|
||||
return {
|
||||
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
|
||||
ATTR_NAME: self.name,
|
||||
ATTR_MANUFACTURER: self.dtv.device.info.brand,
|
||||
ATTR_MODEL: None,
|
||||
ATTR_SOFTWARE_VERSION: self.dtv.device.info.version,
|
||||
ATTR_VIA_DEVICE: (DOMAIN, self.dtv.device.info.receiver_id),
|
||||
}
|
||||
|
|
39
homeassistant/components/directv/entity.py
Normal file
39
homeassistant/components/directv/entity.py
Normal file
|
@ -0,0 +1,39 @@
|
|||
"""Base DirecTV Entity."""
|
||||
from __future__ import annotations
|
||||
|
||||
from directv import DIRECTV
|
||||
|
||||
from homeassistant.const import ATTR_NAME
|
||||
from homeassistant.helpers.entity import DeviceInfo, Entity
|
||||
|
||||
from .const import (
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_MODEL,
|
||||
ATTR_SOFTWARE_VERSION,
|
||||
ATTR_VIA_DEVICE,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
|
||||
class DIRECTVEntity(Entity):
|
||||
"""Defines a base DirecTV entity."""
|
||||
|
||||
def __init__(self, *, dtv: DIRECTV, address: str = "0") -> None:
|
||||
"""Initialize the DirecTV entity."""
|
||||
self._address = address
|
||||
self._device_id = address if address != "0" else dtv.device.info.receiver_id
|
||||
self._is_client = address != "0"
|
||||
self.dtv = dtv
|
||||
|
||||
@property
|
||||
def device_info(self) -> DeviceInfo:
|
||||
"""Return device information about this DirecTV receiver."""
|
||||
return {
|
||||
ATTR_IDENTIFIERS: {(DOMAIN, self._device_id)},
|
||||
ATTR_NAME: self.name,
|
||||
ATTR_MANUFACTURER: self.dtv.device.info.brand,
|
||||
ATTR_MODEL: None,
|
||||
ATTR_SOFTWARE_VERSION: self.dtv.device.info.version,
|
||||
ATTR_VIA_DEVICE: (DOMAIN, self.dtv.device.info.receiver_id),
|
||||
}
|
|
@ -29,7 +29,6 @@ from homeassistant.core import HomeAssistant
|
|||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import DIRECTVEntity
|
||||
from .const import (
|
||||
ATTR_MEDIA_CURRENTLY_RECORDING,
|
||||
ATTR_MEDIA_RATING,
|
||||
|
@ -37,6 +36,7 @@ from .const import (
|
|||
ATTR_MEDIA_START_TIME,
|
||||
DOMAIN,
|
||||
)
|
||||
from .entity import DIRECTVEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -91,12 +91,15 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
"""Initialize DirecTV media player."""
|
||||
super().__init__(
|
||||
dtv=dtv,
|
||||
name=name,
|
||||
address=address,
|
||||
)
|
||||
|
||||
self._assumed_state = None
|
||||
self._available = False
|
||||
self._attr_unique_id = self._device_id
|
||||
self._attr_name = name
|
||||
self._attr_device_class = DEVICE_CLASS_RECEIVER
|
||||
self._attr_available = False
|
||||
self._attr_assumed_state = None
|
||||
|
||||
self._is_recorded = None
|
||||
self._is_standby = True
|
||||
self._last_position = None
|
||||
|
@ -108,12 +111,12 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
async def async_update(self):
|
||||
"""Retrieve latest state."""
|
||||
self._state = await self.dtv.state(self._address)
|
||||
self._available = self._state.available
|
||||
self._attr_available = self._state.available
|
||||
self._is_standby = self._state.standby
|
||||
self._program = self._state.program
|
||||
|
||||
if self._is_standby:
|
||||
self._assumed_state = False
|
||||
self._attr_assumed_state = False
|
||||
self._is_recorded = None
|
||||
self._last_position = None
|
||||
self._last_update = None
|
||||
|
@ -123,7 +126,7 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
self._is_recorded = self._program.recorded
|
||||
self._last_position = self._program.position
|
||||
self._last_update = self._state.at
|
||||
self._assumed_state = self._is_recorded
|
||||
self._attr_assumed_state = self._is_recorded
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self):
|
||||
|
@ -137,24 +140,6 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
ATTR_MEDIA_START_TIME: self.media_start_time,
|
||||
}
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
"""Return the name of the device."""
|
||||
return self._name
|
||||
|
||||
@property
|
||||
def device_class(self) -> str | None:
|
||||
"""Return the class of this device."""
|
||||
return DEVICE_CLASS_RECEIVER
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID to use for this media player."""
|
||||
if self._address == "0":
|
||||
return self.dtv.device.info.receiver_id
|
||||
|
||||
return self._address
|
||||
|
||||
# MediaPlayerEntity properties and methods
|
||||
@property
|
||||
def state(self):
|
||||
|
@ -170,16 +155,6 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
|
||||
return STATE_PLAYING
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if able to retrieve information from DVR or not."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def assumed_state(self):
|
||||
"""Return if we assume the state or not."""
|
||||
return self._assumed_state
|
||||
|
||||
@property
|
||||
def media_content_id(self):
|
||||
"""Return the content ID of current playing media."""
|
||||
|
@ -316,7 +291,7 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
if self._is_client:
|
||||
raise NotImplementedError()
|
||||
|
||||
_LOGGER.debug("Turn on %s", self._name)
|
||||
_LOGGER.debug("Turn on %s", self.name)
|
||||
await self.dtv.remote("poweron", self._address)
|
||||
|
||||
async def async_turn_off(self):
|
||||
|
@ -324,32 +299,32 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
if self._is_client:
|
||||
raise NotImplementedError()
|
||||
|
||||
_LOGGER.debug("Turn off %s", self._name)
|
||||
_LOGGER.debug("Turn off %s", self.name)
|
||||
await self.dtv.remote("poweroff", self._address)
|
||||
|
||||
async def async_media_play(self):
|
||||
"""Send play command."""
|
||||
_LOGGER.debug("Play on %s", self._name)
|
||||
_LOGGER.debug("Play on %s", self.name)
|
||||
await self.dtv.remote("play", self._address)
|
||||
|
||||
async def async_media_pause(self):
|
||||
"""Send pause command."""
|
||||
_LOGGER.debug("Pause on %s", self._name)
|
||||
_LOGGER.debug("Pause on %s", self.name)
|
||||
await self.dtv.remote("pause", self._address)
|
||||
|
||||
async def async_media_stop(self):
|
||||
"""Send stop command."""
|
||||
_LOGGER.debug("Stop on %s", self._name)
|
||||
_LOGGER.debug("Stop on %s", self.name)
|
||||
await self.dtv.remote("stop", self._address)
|
||||
|
||||
async def async_media_previous_track(self):
|
||||
"""Send rewind command."""
|
||||
_LOGGER.debug("Rewind on %s", self._name)
|
||||
_LOGGER.debug("Rewind on %s", self.name)
|
||||
await self.dtv.remote("rew", self._address)
|
||||
|
||||
async def async_media_next_track(self):
|
||||
"""Send fast forward command."""
|
||||
_LOGGER.debug("Fast forward on %s", self._name)
|
||||
_LOGGER.debug("Fast forward on %s", self.name)
|
||||
await self.dtv.remote("ffwd", self._address)
|
||||
|
||||
async def async_play_media(self, media_type, media_id, **kwargs):
|
||||
|
@ -362,5 +337,5 @@ class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerEntity):
|
|||
)
|
||||
return
|
||||
|
||||
_LOGGER.debug("Changing channel on %s to %s", self._name, media_id)
|
||||
_LOGGER.debug("Changing channel on %s to %s", self.name, media_id)
|
||||
await self.dtv.tune(media_id, self._address)
|
||||
|
|
|
@ -13,8 +13,8 @@ from homeassistant.config_entries import ConfigEntry
|
|||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
||||
|
||||
from . import DIRECTVEntity
|
||||
from .const import DOMAIN
|
||||
from .entity import DIRECTVEntity
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
|
@ -49,41 +49,24 @@ class DIRECTVRemote(DIRECTVEntity, RemoteEntity):
|
|||
"""Initialize DirecTV remote."""
|
||||
super().__init__(
|
||||
dtv=dtv,
|
||||
name=name,
|
||||
address=address,
|
||||
)
|
||||
|
||||
self._available = False
|
||||
self._is_on = True
|
||||
|
||||
@property
|
||||
def available(self):
|
||||
"""Return if able to retrieve information from device or not."""
|
||||
return self._available
|
||||
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID."""
|
||||
if self._address == "0":
|
||||
return self.dtv.device.info.receiver_id
|
||||
|
||||
return self._address
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return True if entity is on."""
|
||||
return self._is_on
|
||||
self._attr_unique_id = self._device_id
|
||||
self._attr_name = name
|
||||
self._attr_available = False
|
||||
self._attr_is_on = True
|
||||
|
||||
async def async_update(self) -> None:
|
||||
"""Update device state."""
|
||||
status = await self.dtv.status(self._address)
|
||||
|
||||
if status in ("active", "standby"):
|
||||
self._available = True
|
||||
self._is_on = status == "active"
|
||||
self._attr_available = True
|
||||
self._attr_is_on = status == "active"
|
||||
else:
|
||||
self._available = False
|
||||
self._is_on = False
|
||||
self._attr_available = False
|
||||
self._attr_is_on = False
|
||||
|
||||
async def async_turn_on(self, **kwargs: Any) -> None:
|
||||
"""Turn the device on."""
|
||||
|
|
Loading…
Add table
Reference in a new issue