Refactor DirecTV Integration to Async (#33114)
* switch to directv==0.1.1 * work on directv async. * Update const.py * Update __init__.py * Update media_player.py * Update __init__.py * Update __init__.py * Update __init__.py * Update media_player.py * Update test_config_flow.py * Update media_player.py * Update media_player.py * work on tests and coverage. * Update __init__.py * Update __init__.py * squash.
This commit is contained in:
1 parent
3566803d2e
commit
b892dbc6ea
19 files changed
+749
-845
No files matched your search
@@ -1,19 +1,27 @@
|
||||
"""The DirecTV integration."""
|
||||
import asyncio
|
||||
from datetime import timedelta
|
||||
from typing import Dict
|
||||
from typing import Any, Dict
|
||||
|
||||
from DirectPy import DIRECTV
|
||||
from requests.exceptions import RequestException
|
||||
from directv import DIRECTV, DIRECTVError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
from homeassistant.const import CONF_HOST
|
||||
from homeassistant.const import ATTR_NAME, 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 Entity
|
||||
|
||||
from .const import DATA_CLIENT, DATA_LOCATIONS, DATA_VERSION_INFO, DEFAULT_PORT, DOMAIN
|
||||
from .const import (
|
||||
ATTR_IDENTIFIERS,
|
||||
ATTR_MANUFACTURER,
|
||||
ATTR_MODEL,
|
||||
ATTR_SOFTWARE_VERSION,
|
||||
ATTR_VIA_DEVICE,
|
||||
DOMAIN,
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{
|
||||
@@ -28,21 +36,6 @@ PLATFORMS = ["media_player"]
|
||||
SCAN_INTERVAL = timedelta(seconds=30)
|
||||
|
||||
|
||||
def get_dtv_data(
|
||||
hass: HomeAssistant, host: str, port: int = DEFAULT_PORT, client_addr: str = "0"
|
||||
) -> dict:
|
||||
"""Retrieve a DIRECTV instance, locations list, and version info for the receiver device."""
|
||||
dtv = DIRECTV(host, port, client_addr, determine_state=False)
|
||||
locations = dtv.get_locations()
|
||||
version_info = dtv.get_version()
|
||||
|
||||
return {
|
||||
DATA_CLIENT: dtv,
|
||||
DATA_LOCATIONS: locations,
|
||||
DATA_VERSION_INFO: version_info,
|
||||
}
|
||||
|
||||
|
||||
async def async_setup(hass: HomeAssistant, config: Dict) -> bool:
|
||||
"""Set up the DirecTV component."""
|
||||
hass.data.setdefault(DOMAIN, {})
|
||||
@@ -60,14 +53,14 @@ async def async_setup(hass: HomeAssistant, config: Dict) -> bool:
|
||||
|
||||
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Set up DirecTV from a config entry."""
|
||||
dtv = DIRECTV(entry.data[CONF_HOST], session=async_get_clientsession(hass))
|
||||
|
||||
try:
|
||||
dtv_data = await hass.async_add_executor_job(
|
||||
get_dtv_data, hass, entry.data[CONF_HOST]
|
||||
)
|
||||
except RequestException:
|
||||
await dtv.update()
|
||||
except DIRECTVError:
|
||||
raise ConfigEntryNotReady
|
||||
|
||||
hass.data[DOMAIN][entry.entry_id] = dtv_data
|
||||
hass.data[DOMAIN][entry.entry_id] = dtv
|
||||
|
||||
for component in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
@@ -92,3 +85,32 @@ 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) -> Dict[str, Any]:
|
||||
"""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),
|
||||
}
|
||||
@@ -3,18 +3,20 @@ import logging
|
||||
from typing import Any, Dict, Optional
|
||||
from urllib.parse import urlparse
|
||||
|
||||
from DirectPy import DIRECTV
|
||||
from requests.exceptions import RequestException
|
||||
from directv import DIRECTV, DIRECTVError
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.components.ssdp import ATTR_SSDP_LOCATION, ATTR_UPNP_SERIAL
|
||||
from homeassistant.config_entries import CONN_CLASS_LOCAL_POLL, ConfigFlow
|
||||
from homeassistant.const import CONF_HOST, CONF_NAME
|
||||
from homeassistant.core import callback
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
from homeassistant.helpers.typing import DiscoveryInfoType
|
||||
from homeassistant.helpers.aiohttp_client import async_get_clientsession
|
||||
from homeassistant.helpers.typing import (
|
||||
ConfigType,
|
||||
DiscoveryInfoType,
|
||||
HomeAssistantType,
|
||||
)
|
||||
|
||||
from .const import DEFAULT_PORT
|
||||
from .const import CONF_RECEIVER_ID
|
||||
from .const import DOMAIN # pylint: disable=unused-import
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -22,22 +24,17 @@ _LOGGER = logging.getLogger(__name__)
|
||||
ERROR_CANNOT_CONNECT = "cannot_connect"
|
||||
ERROR_UNKNOWN = "unknown"
|
||||
|
||||
DATA_SCHEMA = vol.Schema({vol.Required(CONF_HOST): str})
|
||||
|
||||
|
||||
def validate_input(data: Dict) -> Dict:
|
||||
async def validate_input(hass: HomeAssistantType, data: dict) -> Dict[str, Any]:
|
||||
"""Validate the user input allows us to connect.
|
||||
|
||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||
"""
|
||||
dtv = DIRECTV(data["host"], DEFAULT_PORT, determine_state=False)
|
||||
version_info = dtv.get_version()
|
||||
session = async_get_clientsession(hass)
|
||||
directv = DIRECTV(data[CONF_HOST], session=session)
|
||||
device = await directv.update()
|
||||
|
||||
return {
|
||||
"title": data["host"],
|
||||
"host": data["host"],
|
||||
"receiver_id": "".join(version_info["receiverId"].split()),
|
||||
}
|
||||
return {CONF_RECEIVER_ID: device.info.receiver_id}
|
||||
|
||||
|
||||
class DirecTVConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
@@ -46,84 +43,91 @@ class DirecTVConfigFlow(ConfigFlow, domain=DOMAIN):
|
||||
VERSION = 1
|
||||
CONNECTION_CLASS = CONN_CLASS_LOCAL_POLL
|
||||
|
||||
@callback
|
||||
def _show_form(self, errors: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""Show the form to the user."""
|
||||
return self.async_show_form(
|
||||
step_id="user", data_schema=DATA_SCHEMA, errors=errors or {},
|
||||
)
|
||||
def __init__(self):
|
||||
"""Set up the instance."""
|
||||
self.discovery_info = {}
|
||||
|
||||
async def async_step_import(
|
||||
self, user_input: Optional[Dict] = None
|
||||
self, user_input: Optional[ConfigType] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle a flow initialized by yaml file."""
|
||||
"""Handle a flow initiated by configuration file."""
|
||||
return await self.async_step_user(user_input)
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: Optional[Dict] = None
|
||||
self, user_input: Optional[ConfigType] = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle a flow initialized by user."""
|
||||
if not user_input:
|
||||
return self._show_form()
|
||||
|
||||
errors = {}
|
||||
"""Handle a flow initiated by the user."""
|
||||
if user_input is None:
|
||||
return self._show_setup_form()
|
||||
|
||||
try:
|
||||
info = await self.hass.async_add_executor_job(validate_input, user_input)
|
||||
user_input[CONF_HOST] = info[CONF_HOST]
|
||||
except RequestException:
|
||||
errors["base"] = ERROR_CANNOT_CONNECT
|
||||
return self._show_form(errors)
|
||||
info = await validate_input(self.hass, user_input)
|
||||
except DIRECTVError:
|
||||
return self._show_setup_form({"base": ERROR_CANNOT_CONNECT})
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
return self.async_abort(reason=ERROR_UNKNOWN)
|
||||
|
||||
await self.async_set_unique_id(info["receiver_id"])
|
||||
self._abort_if_unique_id_configured()
|
||||
user_input[CONF_RECEIVER_ID] = info[CONF_RECEIVER_ID]
|
||||
|
||||
return self.async_create_entry(title=info["title"], data=user_input)
|
||||
await self.async_set_unique_id(user_input[CONF_RECEIVER_ID])
|
||||
self._abort_if_unique_id_configured(updates={CONF_HOST: user_input[CONF_HOST]})
|
||||
|
||||
return self.async_create_entry(title=user_input[CONF_HOST], data=user_input)
|
||||
|
||||
async def async_step_ssdp(
|
||||
self, discovery_info: Optional[DiscoveryInfoType] = None
|
||||
self, discovery_info: DiscoveryInfoType
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle a flow initialized by discovery."""
|
||||
"""Handle SSDP discovery."""
|
||||
host = urlparse(discovery_info[ATTR_SSDP_LOCATION]).hostname
|
||||
receiver_id = discovery_info[ATTR_UPNP_SERIAL][4:] # strips off RID-
|
||||
receiver_id = None
|
||||
|
||||
await self.async_set_unique_id(receiver_id)
|
||||
self._abort_if_unique_id_configured(updates={CONF_HOST: host})
|
||||
if discovery_info.get(ATTR_UPNP_SERIAL):
|
||||
receiver_id = discovery_info[ATTR_UPNP_SERIAL][4:] # strips off RID-
|
||||
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
self.context.update(
|
||||
{CONF_HOST: host, CONF_NAME: host, "title_placeholders": {"name": host}}
|
||||
self.context.update({"title_placeholders": {"name": host}})
|
||||
|
||||
self.discovery_info.update(
|
||||
{CONF_HOST: host, CONF_NAME: host, CONF_RECEIVER_ID: receiver_id}
|
||||
)
|
||||
|
||||
try:
|
||||
info = await validate_input(self.hass, self.discovery_info)
|
||||
except DIRECTVError:
|
||||
return self.async_abort(reason=ERROR_CANNOT_CONNECT)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
return self.async_abort(reason=ERROR_UNKNOWN)
|
||||
|
||||
self.discovery_info[CONF_RECEIVER_ID] = info[CONF_RECEIVER_ID]
|
||||
|
||||
await self.async_set_unique_id(self.discovery_info[CONF_RECEIVER_ID])
|
||||
self._abort_if_unique_id_configured(
|
||||
updates={CONF_HOST: self.discovery_info[CONF_HOST]}
|
||||
)
|
||||
|
||||
return await self.async_step_ssdp_confirm()
|
||||
|
||||
async def async_step_ssdp_confirm(
|
||||
self, user_input: Optional[Dict] = None
|
||||
self, user_input: ConfigType = None
|
||||
) -> Dict[str, Any]:
|
||||
"""Handle user-confirmation of discovered device."""
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
name = self.context.get(CONF_NAME)
|
||||
"""Handle a confirmation flow initiated by SSDP."""
|
||||
if user_input is None:
|
||||
return self.async_show_form(
|
||||
step_id="ssdp_confirm",
|
||||
description_placeholders={"name": self.discovery_info[CONF_NAME]},
|
||||
errors={},
|
||||
)
|
||||
|
||||
if user_input is not None:
|
||||
# pylint: disable=no-member # https://github.com/PyCQA/pylint/issues/3167
|
||||
user_input[CONF_HOST] = self.context.get(CONF_HOST)
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(validate_input, user_input)
|
||||
return self.async_create_entry(title=name, data=user_input)
|
||||
except (OSError, RequestException):
|
||||
return self.async_abort(reason=ERROR_CANNOT_CONNECT)
|
||||
except Exception: # pylint: disable=broad-except
|
||||
_LOGGER.exception("Unexpected exception")
|
||||
return self.async_abort(reason=ERROR_UNKNOWN)
|
||||
|
||||
return self.async_show_form(
|
||||
step_id="ssdp_confirm", description_placeholders={"name": name},
|
||||
return self.async_create_entry(
|
||||
title=self.discovery_info[CONF_NAME], data=self.discovery_info,
|
||||
)
|
||||
|
||||
|
||||
class CannotConnect(HomeAssistantError):
|
||||
"""Error to indicate we cannot connect."""
|
||||
def _show_setup_form(self, errors: Optional[Dict] = None) -> Dict[str, Any]:
|
||||
"""Show the setup form to the user."""
|
||||
return self.async_show_form(
|
||||
step_id="user",
|
||||
data_schema=vol.Schema({vol.Required(CONF_HOST): str}),
|
||||
errors=errors or {},
|
||||
)
|
||||
@@ -2,19 +2,19 @@
|
||||
|
||||
DOMAIN = "directv"
|
||||
|
||||
# Attributes
|
||||
ATTR_IDENTIFIERS = "identifiers"
|
||||
ATTR_MANUFACTURER = "manufacturer"
|
||||
ATTR_MEDIA_CURRENTLY_RECORDING = "media_currently_recording"
|
||||
ATTR_MEDIA_RATING = "media_rating"
|
||||
ATTR_MEDIA_RECORDED = "media_recorded"
|
||||
ATTR_MEDIA_START_TIME = "media_start_time"
|
||||
ATTR_MODEL = "model"
|
||||
ATTR_SOFTWARE_VERSION = "sw_version"
|
||||
ATTR_VIA_DEVICE = "via_device"
|
||||
|
||||
DATA_CLIENT = "client"
|
||||
DATA_LOCATIONS = "locations"
|
||||
DATA_VERSION_INFO = "version_info"
|
||||
CONF_RECEIVER_ID = "receiver_id"
|
||||
|
||||
DEFAULT_DEVICE = "0"
|
||||
DEFAULT_MANUFACTURER = "DirecTV"
|
||||
DEFAULT_NAME = "DirecTV Receiver"
|
||||
DEFAULT_PORT = 8080
|
||||
|
||||
MODEL_HOST = "DirecTV Host"
|
||||
MODEL_CLIENT = "DirecTV Client"
|
||||
@@ -2,9 +2,10 @@
|
||||
"domain": "directv",
|
||||
"name": "DirecTV",
|
||||
"documentation": "https://www.home-assistant.io/integrations/directv",
|
||||
"requirements": ["directpy==0.7"],
|
||||
"requirements": ["directv==0.2.0"],
|
||||
"dependencies": [],
|
||||
"codeowners": ["@ctalkington"],
|
||||
"quality_scale": "gold",
|
||||
"config_flow": true,
|
||||
"ssdp": [
|
||||
{
|
||||
|
||||
@@ -1,12 +1,10 @@
|
||||
"""Support for the DirecTV receivers."""
|
||||
import logging
|
||||
from typing import Callable, Dict, List, Optional
|
||||
from typing import Callable, List
|
||||
|
||||
from DirectPy import DIRECTV
|
||||
from requests.exceptions import RequestException
|
||||
import voluptuous as vol
|
||||
from directv import DIRECTV
|
||||
|
||||
from homeassistant.components.media_player import PLATFORM_SCHEMA, MediaPlayerDevice
|
||||
from homeassistant.components.media_player import MediaPlayerDevice
|
||||
from homeassistant.components.media_player.const import (
|
||||
MEDIA_TYPE_CHANNEL,
|
||||
MEDIA_TYPE_MOVIE,
|
||||
@@ -21,34 +19,17 @@ from homeassistant.components.media_player.const import (
|
||||
SUPPORT_TURN_ON,
|
||||
)
|
||||
from homeassistant.config_entries import ConfigEntry
|
||||
from homeassistant.const import (
|
||||
CONF_DEVICE,
|
||||
CONF_HOST,
|
||||
CONF_NAME,
|
||||
CONF_PORT,
|
||||
STATE_OFF,
|
||||
STATE_PAUSED,
|
||||
STATE_PLAYING,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.const import STATE_OFF, STATE_PAUSED, STATE_PLAYING
|
||||
from homeassistant.helpers.typing import HomeAssistantType
|
||||
from homeassistant.util import dt as dt_util
|
||||
|
||||
from . import DIRECTVEntity
|
||||
from .const import (
|
||||
ATTR_MEDIA_CURRENTLY_RECORDING,
|
||||
ATTR_MEDIA_RATING,
|
||||
ATTR_MEDIA_RECORDED,
|
||||
ATTR_MEDIA_START_TIME,
|
||||
DATA_CLIENT,
|
||||
DATA_LOCATIONS,
|
||||
DATA_VERSION_INFO,
|
||||
DEFAULT_DEVICE,
|
||||
DEFAULT_MANUFACTURER,
|
||||
DEFAULT_NAME,
|
||||
DEFAULT_PORT,
|
||||
DOMAIN,
|
||||
MODEL_CLIENT,
|
||||
MODEL_HOST,
|
||||
)
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
@@ -73,15 +54,6 @@ SUPPORT_DTV_CLIENT = (
|
||||
| SUPPORT_PLAY
|
||||
)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_HOST): cv.string,
|
||||
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
|
||||
vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
|
||||
vol.Optional(CONF_DEVICE, default=DEFAULT_DEVICE): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def async_setup_entry(
|
||||
hass: HomeAssistantType,
|
||||
@@ -89,139 +61,57 @@ async def async_setup_entry(
|
||||
async_add_entities: Callable[[List, bool], None],
|
||||
) -> bool:
|
||||
"""Set up the DirecTV config entry."""
|
||||
locations = hass.data[DOMAIN][entry.entry_id][DATA_LOCATIONS]
|
||||
version_info = hass.data[DOMAIN][entry.entry_id][DATA_VERSION_INFO]
|
||||
dtv = hass.data[DOMAIN][entry.entry_id]
|
||||
entities = []
|
||||
|
||||
for loc in locations["locations"]:
|
||||
if "locationName" not in loc or "clientAddr" not in loc:
|
||||
continue
|
||||
|
||||
if loc["clientAddr"] != "0":
|
||||
dtv = DIRECTV(
|
||||
entry.data[CONF_HOST],
|
||||
DEFAULT_PORT,
|
||||
loc["clientAddr"],
|
||||
determine_state=False,
|
||||
)
|
||||
else:
|
||||
dtv = hass.data[DOMAIN][entry.entry_id][DATA_CLIENT]
|
||||
|
||||
for location in dtv.device.locations:
|
||||
entities.append(
|
||||
DirecTvDevice(
|
||||
str.title(loc["locationName"]), loc["clientAddr"], dtv, version_info,
|
||||
DIRECTVMediaPlayer(
|
||||
dtv=dtv, name=str.title(location.name), address=location.address,
|
||||
)
|
||||
)
|
||||
|
||||
async_add_entities(entities, True)
|
||||
|
||||
|
||||
class DirecTvDevice(MediaPlayerDevice):
|
||||
class DIRECTVMediaPlayer(DIRECTVEntity, MediaPlayerDevice):
|
||||
"""Representation of a DirecTV receiver on the network."""
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
device: str,
|
||||
dtv: DIRECTV,
|
||||
version_info: Optional[Dict] = None,
|
||||
enabled_default: bool = True,
|
||||
):
|
||||
"""Initialize the device."""
|
||||
self.dtv = dtv
|
||||
self._name = name
|
||||
self._unique_id = None
|
||||
self._is_standby = True
|
||||
self._current = None
|
||||
self._last_update = None
|
||||
self._paused = None
|
||||
self._last_position = None
|
||||
self._is_recorded = None
|
||||
self._is_client = device != "0"
|
||||
def __init__(self, *, dtv: DIRECTV, name: str, address: str = "0") -> None:
|
||||
"""Initialize DirecTV media player."""
|
||||
super().__init__(
|
||||
dtv=dtv, name=name, address=address,
|
||||
)
|
||||
|
||||
self._assumed_state = None
|
||||
self._available = False
|
||||
self._enabled_default = enabled_default
|
||||
self._first_error_timestamp = None
|
||||
self._model = None
|
||||
self._receiver_id = None
|
||||
self._software_version = None
|
||||
self._is_recorded = None
|
||||
self._is_standby = True
|
||||
self._last_position = None
|
||||
self._last_update = None
|
||||
self._paused = None
|
||||
self._program = None
|
||||
self._state = None
|
||||
|
||||
if self._is_client:
|
||||
self._model = MODEL_CLIENT
|
||||
self._unique_id = device
|
||||
|
||||
if version_info:
|
||||
self._receiver_id = "".join(version_info["receiverId"].split())
|
||||
|
||||
if not self._is_client:
|
||||
self._unique_id = self._receiver_id
|
||||
self._model = MODEL_HOST
|
||||
self._software_version = version_info["stbSoftwareVersion"]
|
||||
|
||||
def update(self):
|
||||
async def async_update(self):
|
||||
"""Retrieve latest state."""
|
||||
_LOGGER.debug("%s: Updating status", self.entity_id)
|
||||
try:
|
||||
self._available = True
|
||||
self._is_standby = self.dtv.get_standby()
|
||||
if self._is_standby:
|
||||
self._current = None
|
||||
self._is_recorded = None
|
||||
self._paused = None
|
||||
self._assumed_state = False
|
||||
self._last_position = None
|
||||
self._last_update = None
|
||||
else:
|
||||
self._current = self.dtv.get_tuned()
|
||||
if self._current["status"]["code"] == 200:
|
||||
self._first_error_timestamp = None
|
||||
self._is_recorded = self._current.get("uniqueId") is not None
|
||||
self._paused = self._last_position == self._current["offset"]
|
||||
self._assumed_state = self._is_recorded
|
||||
self._last_position = self._current["offset"]
|
||||
self._last_update = (
|
||||
dt_util.utcnow()
|
||||
if not self._paused or self._last_update is None
|
||||
else self._last_update
|
||||
)
|
||||
else:
|
||||
# If an error is received then only set to unavailable if
|
||||
# this started at least 1 minute ago.
|
||||
log_message = f"{self.entity_id}: Invalid status {self._current['status']['code']} received"
|
||||
if self._check_state_available():
|
||||
_LOGGER.debug(log_message)
|
||||
else:
|
||||
_LOGGER.error(log_message)
|
||||
self._state = await self.dtv.state(self._address)
|
||||
self._available = self._state.available
|
||||
self._is_standby = self._state.standby
|
||||
self._program = self._state.program
|
||||
|
||||
except RequestException as exception:
|
||||
_LOGGER.error(
|
||||
"%s: Request error trying to update current status: %s",
|
||||
self.entity_id,
|
||||
exception,
|
||||
)
|
||||
self._check_state_available()
|
||||
|
||||
except Exception as exception:
|
||||
_LOGGER.error(
|
||||
"%s: Exception trying to update current status: %s",
|
||||
self.entity_id,
|
||||
exception,
|
||||
)
|
||||
self._available = False
|
||||
if not self._first_error_timestamp:
|
||||
self._first_error_timestamp = dt_util.utcnow()
|
||||
raise
|
||||
|
||||
def _check_state_available(self):
|
||||
"""Set to unavailable if issue been occurring over 1 minute."""
|
||||
if not self._first_error_timestamp:
|
||||
self._first_error_timestamp = dt_util.utcnow()
|
||||
else:
|
||||
tdelta = dt_util.utcnow() - self._first_error_timestamp
|
||||
if tdelta.total_seconds() >= 60:
|
||||
self._available = False
|
||||
|
||||
return self._available
|
||||
if self._is_standby:
|
||||
self._assumed_state = False
|
||||
self._is_recorded = None
|
||||
self._last_position = None
|
||||
self._last_update = None
|
||||
self._paused = None
|
||||
elif self._program is not None:
|
||||
self._paused = self._last_position == self._program.position
|
||||
self._is_recorded = self._program.recorded
|
||||
self._last_position = self._program.position
|
||||
self._last_update = self._state.at
|
||||
self._assumed_state = self._is_recorded
|
||||
|
||||
@property
|
||||
def device_state_attributes(self):
|
||||
@@ -243,24 +133,10 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def unique_id(self):
|
||||
"""Return a unique ID to use for this media player."""
|
||||
return self._unique_id
|
||||
if self._address == "0":
|
||||
return self.dtv.device.info.receiver_id
|
||||
|
||||
@property
|
||||
def device_info(self):
|
||||
"""Return device specific attributes."""
|
||||
return {
|
||||
"name": self.name,
|
||||
"identifiers": {(DOMAIN, self.unique_id)},
|
||||
"manufacturer": DEFAULT_MANUFACTURER,
|
||||
"model": self._model,
|
||||
"sw_version": self._software_version,
|
||||
"via_device": (DOMAIN, self._receiver_id),
|
||||
}
|
||||
|
||||
@property
|
||||
def entity_registry_enabled_default(self) -> bool:
|
||||
"""Return if the entity should be enabled when first added to the entity registry."""
|
||||
return self._enabled_default
|
||||
return self._address
|
||||
|
||||
# MediaPlayerDevice properties and methods
|
||||
@property
|
||||
@@ -290,29 +166,30 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def media_content_id(self):
|
||||
"""Return the content ID of current playing media."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["programId"]
|
||||
return self._program.program_id
|
||||
|
||||
@property
|
||||
def media_content_type(self):
|
||||
"""Return the content type of current playing media."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
if "episodeTitle" in self._current:
|
||||
return MEDIA_TYPE_TVSHOW
|
||||
known_types = [MEDIA_TYPE_MOVIE, MEDIA_TYPE_TVSHOW]
|
||||
if self._program.program_type in known_types:
|
||||
return self._program.program_type
|
||||
|
||||
return MEDIA_TYPE_MOVIE
|
||||
|
||||
@property
|
||||
def media_duration(self):
|
||||
"""Return the duration of current playing media in seconds."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["duration"]
|
||||
return self._program.duration
|
||||
|
||||
@property
|
||||
def media_position(self):
|
||||
@@ -324,10 +201,7 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
|
||||
@property
|
||||
def media_position_updated_at(self):
|
||||
"""When was the position of the current playing media valid.
|
||||
|
||||
Returns value from homeassistant.util.dt.utcnow().
|
||||
"""
|
||||
"""When was the position of the current playing media valid."""
|
||||
if self._is_standby:
|
||||
return None
|
||||
|
||||
@@ -336,34 +210,34 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def media_title(self):
|
||||
"""Return the title of current playing media."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["title"]
|
||||
return self._program.title
|
||||
|
||||
@property
|
||||
def media_series_title(self):
|
||||
"""Return the title of current episode of TV show."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current.get("episodeTitle")
|
||||
return self._program.episode_title
|
||||
|
||||
@property
|
||||
def media_channel(self):
|
||||
"""Return the channel current playing media."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return f"{self._current['callsign']} ({self._current['major']})"
|
||||
return f"{self._program.channel_name} ({self._program.channel})"
|
||||
|
||||
@property
|
||||
def source(self):
|
||||
"""Name of the current input source."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["major"]
|
||||
return self._program.channel
|
||||
|
||||
@property
|
||||
def supported_features(self):
|
||||
@@ -373,18 +247,18 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def media_currently_recording(self):
|
||||
"""If the media is currently being recorded or not."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["isRecording"]
|
||||
return self._program.recording
|
||||
|
||||
@property
|
||||
def media_rating(self):
|
||||
"""TV Rating of the current playing media."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return self._current["rating"]
|
||||
return self._program.rating
|
||||
|
||||
@property
|
||||
def media_recorded(self):
|
||||
@@ -397,53 +271,53 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
@property
|
||||
def media_start_time(self):
|
||||
"""Start time the program aired."""
|
||||
if self._is_standby:
|
||||
if self._is_standby or self._program is None:
|
||||
return None
|
||||
|
||||
return dt_util.as_local(dt_util.utc_from_timestamp(self._current["startTime"]))
|
||||
return dt_util.as_local(self._program.start_time)
|
||||
|
||||
def turn_on(self):
|
||||
async def async_turn_on(self):
|
||||
"""Turn on the receiver."""
|
||||
if self._is_client:
|
||||
raise NotImplementedError()
|
||||
|
||||
_LOGGER.debug("Turn on %s", self._name)
|
||||
self.dtv.key_press("poweron")
|
||||
await self.dtv.remote("poweron", self._address)
|
||||
|
||||
def turn_off(self):
|
||||
async def async_turn_off(self):
|
||||
"""Turn off the receiver."""
|
||||
if self._is_client:
|
||||
raise NotImplementedError()
|
||||
|
||||
_LOGGER.debug("Turn off %s", self._name)
|
||||
self.dtv.key_press("poweroff")
|
||||
await self.dtv.remote("poweroff", self._address)
|
||||
|
||||
def media_play(self):
|
||||
async def async_media_play(self):
|
||||
"""Send play command."""
|
||||
_LOGGER.debug("Play on %s", self._name)
|
||||
self.dtv.key_press("play")
|
||||
await self.dtv.remote("play", self._address)
|
||||
|
||||
def media_pause(self):
|
||||
async def async_media_pause(self):
|
||||
"""Send pause command."""
|
||||
_LOGGER.debug("Pause on %s", self._name)
|
||||
self.dtv.key_press("pause")
|
||||
await self.dtv.remote("pause", self._address)
|
||||
|
||||
def media_stop(self):
|
||||
async def async_media_stop(self):
|
||||
"""Send stop command."""
|
||||
_LOGGER.debug("Stop on %s", self._name)
|
||||
self.dtv.key_press("stop")
|
||||
await self.dtv.remote("stop", self._address)
|
||||
|
||||
def media_previous_track(self):
|
||||
async def async_media_previous_track(self):
|
||||
"""Send rewind command."""
|
||||
_LOGGER.debug("Rewind on %s", self._name)
|
||||
self.dtv.key_press("rew")
|
||||
await self.dtv.remote("rew", self._address)
|
||||
|
||||
def media_next_track(self):
|
||||
async def async_media_next_track(self):
|
||||
"""Send fast forward command."""
|
||||
_LOGGER.debug("Fast forward on %s", self._name)
|
||||
self.dtv.key_press("ffwd")
|
||||
await self.dtv.remote("ffwd", self._address)
|
||||
|
||||
def play_media(self, media_type, media_id, **kwargs):
|
||||
async def async_play_media(self, media_type, media_id, **kwargs):
|
||||
"""Select input source."""
|
||||
if media_type != MEDIA_TYPE_CHANNEL:
|
||||
_LOGGER.error(
|
||||
@@ -454,4 +328,4 @@ class DirecTvDevice(MediaPlayerDevice):
|
||||
return
|
||||
|
||||
_LOGGER.debug("Changing channel on %s to %s", self._name, media_id)
|
||||
self.dtv.tune_channel(media_id)
|
||||
await self.dtv.tune(media_id, self._address)
|
||||
Reference in new issue
Block a user