Add device info to Panasonic Viera (#41028)

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
João GabrielandMartin Hjelmare authored and GitHub committed 2020-10-13 13:14:59 +02:00
1 parent 72289b8171
commit d1041efedf
6 files changed
+250 -10

No files matched your search

@@ -14,7 +14,9 @@ import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.script import Script
from .const import (
ATTR_DEVICE_INFO,
ATTR_REMOTE,
ATTR_UDN,
CONF_APP_ID,
CONF_ENCRYPTION_KEY,
CONF_ON_ACTION,
@@ -86,6 +88,22 @@ async def async_setup_entry(hass, config_entry):
panasonic_viera_data[config_entry.entry_id] = {ATTR_REMOTE: remote}
# Add device_info to older config entries
if ATTR_DEVICE_INFO not in config or config[ATTR_DEVICE_INFO] is None:
device_info = await remote.async_get_device_info()
unique_id = config_entry.unique_id
if device_info is None:
_LOGGER.error(
"Couldn't gather device info. Please restart Home Assistant with your TV turned on and connected to your network."
)
else:
unique_id = device_info[ATTR_UDN]
hass.config_entries.async_update_entry(
config_entry,
unique_id=unique_id,
data={**config, ATTR_DEVICE_INFO: device_info},
)
for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
@@ -223,6 +241,12 @@ class Remote:
_LOGGER.debug("Play media: %s (%s)", media_id, media_type)
await self._handle_errors(self._control.open_webpage, media_id)
async def async_get_device_info(self):
"""Return device info."""
if self._control is None:
return None
return await self._handle_errors(self._control.get_device_info)
async def _handle_errors(self, func, *args):
"""Handle errors from func, set available and reconnect if needed."""
try:
@@ -10,6 +10,9 @@ from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PIN, CONF_PORT
from .const import ( # pylint: disable=unused-import
ATTR_DEVICE_INFO,
ATTR_FRIENDLY_NAME,
ATTR_UDN,
CONF_APP_ID,
CONF_ENCRYPTION_KEY,
CONF_ON_ACTION,
@@ -35,6 +38,7 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
CONF_NAME: None,
CONF_PORT: None,
CONF_ON_ACTION: None,
ATTR_DEVICE_INFO: None,
}
self._remote = None
@@ -49,6 +53,10 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
self._remote = await self.hass.async_add_executor_job(
partial(RemoteControl, self._data[CONF_HOST], self._data[CONF_PORT])
)
self._data[ATTR_DEVICE_INFO] = await self.hass.async_add_executor_job(
self._remote.get_device_info
)
except (TimeoutError, URLError, SOAPError, OSError) as err:
_LOGGER.error("Could not establish remote connection: %s", err)
errors["base"] = "cannot_connect"
@@ -57,6 +65,14 @@ class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
return self.async_abort(reason="unknown")
if "base" not in errors:
await self.async_set_unique_id(self._data[ATTR_DEVICE_INFO][ATTR_UDN])
self._abort_if_unique_id_configured()
if self._data[CONF_NAME] == DEFAULT_NAME:
self._data[CONF_NAME] = self._data[ATTR_DEVICE_INFO][
ATTR_FRIENDLY_NAME
].replace("_", " ")
if self._remote.type == TV_TYPE_ENCRYPTED:
return await self.async_step_pairing()
@@ -12,4 +12,10 @@ DEFAULT_PORT = 55000
ATTR_REMOTE = "remote"
ATTR_DEVICE_INFO = "device_info"
ATTR_FRIENDLY_NAME = "friendlyName"
ATTR_MANUFACTURER = "manufacturer"
ATTR_MODEL_NUMBER = "modelNumber"
ATTR_UDN = "UDN"
ERROR_INVALID_PIN_CODE = "invalid_pin_code"
@@ -20,7 +20,14 @@ from homeassistant.components.media_player.const import (
)
from homeassistant.const import CONF_NAME
from .const import ATTR_REMOTE, DOMAIN
from .const import (
ATTR_DEVICE_INFO,
ATTR_MANUFACTURER,
ATTR_MODEL_NUMBER,
ATTR_REMOTE,
ATTR_UDN,
DOMAIN,
)
SUPPORT_VIERATV = (
SUPPORT_PAUSE
@@ -46,24 +53,39 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
remote = hass.data[DOMAIN][config_entry.entry_id][ATTR_REMOTE]
name = config[CONF_NAME]
device_info = config[ATTR_DEVICE_INFO]
tv_device = PanasonicVieraTVEntity(remote, name)
tv_device = PanasonicVieraTVEntity(remote, name, device_info)
async_add_entities([tv_device])
class PanasonicVieraTVEntity(MediaPlayerEntity):
"""Representation of a Panasonic Viera TV."""
def __init__(self, remote, name, uuid=None):
def __init__(self, remote, name, device_info):
"""Initialize the entity."""
self._remote = remote
self._name = name
self._uuid = uuid
self._device_info = device_info
@property
def unique_id(self):
def unique_id(self) -> str:
"""Return the unique ID of the device."""
return self._uuid
if self._device_info is not None:
return self._device_info[ATTR_UDN]
return None
@property
def device_info(self):
"""Return device specific attributes."""
if self._device_info is None:
return None
return {
"name": self._name,
"identifiers": {(DOMAIN, self._device_info[ATTR_UDN])},
"manufacturer": self._device_info[ATTR_MANUFACTURER],
"model": self._device_info[ATTR_MODEL_NUMBER],
}
@property
def name(self):