Panasonic Viera TV component error

Hi all,

I am trying to update the Panasonic Viera Component to work with panasonic_viera=0.4.3 because 0.4.2 does not have support for encrypted TV. But if I run the code inside HomeAssistant I get the following error:

de gen. 13 18:36:29 raspberrypi hass[26388]: 2020-01-13 18:36:29 ERROR (MainThread) [homeassistant.components.websocket_api.http.connection.1668440368] HTTP Error 500: Internal Server Error
de gen. 13 18:36:29 raspberrypi hass[26388]: Traceback (most recent call last):
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/websocket_api/commands.py", line 133, in handle_call_service
de gen. 13 18:36:29 raspberrypi hass[26388]:     connection.context(msg),
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1235, in async_call
de gen. 13 18:36:29 raspberrypi hass[26388]:     await asyncio.shield(self._execute_service(handler, service_call))
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/core.py", line 1260, in _execute_service
de gen. 13 18:36:29 raspberrypi hass[26388]:     await handler.func(service_call)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/entity_component.py", line 205, in handle_service
de gen. 13 18:36:29 raspberrypi hass[26388]:     self._platforms.values(), func, call, service_name, required_features
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 336, in entity_service_call
de gen. 13 18:36:29 raspberrypi hass[26388]:     future.result()  # pop exception if have
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/helpers/service.py", line 358, in _handle_service_platform_call
de gen. 13 18:36:29 raspberrypi hass[26388]:     await getattr(entity, func)(**data)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/concurrent/futures/thread.py", line 57, in run
de gen. 13 18:36:29 raspberrypi hass[26388]:     result = self.fn(*self.args, **self.kwargs)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/homeassistant/components/panasonic_viera/media_player.py", line 190, in turn_off
de gen. 13 18:36:29 raspberrypi hass[26388]:     self._state = STATE_OFF
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/panasonic_viera/__init__.py", line 554, in turn_off
de gen. 13 18:36:29 raspberrypi hass[26388]:     self.send_key(Keys.power)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/panasonic_viera/__init__.py", line 538, in send_key
de gen. 13 18:36:29 raspberrypi hass[26388]:     'X_SendKey', params)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/panasonic_viera/__init__.py", line 242, in soap_request
de gen. 13 18:36:29 raspberrypi hass[26388]:     raise e # Pass to the next handler
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/srv/homeassistant/lib/python3.7/site-packages/panasonic_viera/__init__.py", line 238, in soap_request
de gen. 13 18:36:29 raspberrypi hass[26388]:     res = urlopen(req, timeout=5).read()
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 222, in urlopen
de gen. 13 18:36:29 raspberrypi hass[26388]:     return opener.open(url, data, timeout)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 531, in open
de gen. 13 18:36:29 raspberrypi hass[26388]:     response = meth(req, response)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 641, in http_response
de gen. 13 18:36:29 raspberrypi hass[26388]:     'http', request, response, code, msg, hdrs)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 569, in error
de gen. 13 18:36:29 raspberrypi hass[26388]:     return self._call_chain(*args)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 503, in _call_chain
de gen. 13 18:36:29 raspberrypi hass[26388]:     result = func(*args)
de gen. 13 18:36:29 raspberrypi hass[26388]:   File "/usr/lib/python3.7/urllib/request.py", line 649, in http_error_default
de gen. 13 18:36:29 raspberrypi hass[26388]:     raise HTTPError(req.full_url, code, msg, hdrs, fp)
de gen. 13 18:36:29 raspberrypi hass[26388]: urllib.error.HTTPError: HTTP Error 500: Internal Server Error

but if I run the code the component is running outside homeassistant:

import panasonic_viera
rc = panasonic_viera.RemoteControl("<HOST>", app_id="<APP_ID>", encryption_key="<ENCRYPTION_KEY>")
# We can now start communicating with our TV
rc.turn_off()

everything works as expected and TV turns off.

Any knowledge why inside home assistant I get the error? It works only with the first command I run. After that any command raised the error mentioned above

PD: component is edited as expected and loads app_id and encryption_key:

PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
    {
        vol.Required(CONF_HOST): cv.string,
        vol.Optional(CONF_MAC): cv.string,
        vol.Optional(
            CONF_BROADCAST_ADDRESS, default=DEFAULT_BROADCAST_ADDRESS
        ): cv.string,
        vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
        vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
        vol.Optional(CONF_APP_POWER, default=DEFAULT_APP_POWER): cv.boolean,
        vol.Optional(CONF_APP_ID): cv.string,
        vol.Optional(CONF_ENCRYPTION_KEY): cv.string
    }
)


def setup_platform(hass, config, add_entities, discovery_info=None):
    """Set up the Panasonic Viera TV platform."""
    mac = config.get(CONF_MAC)
    broadcast = config.get(CONF_BROADCAST_ADDRESS)
    name = config.get(CONF_NAME)
    port = config.get(CONF_PORT)
    app_power = config.get(CONF_APP_POWER)
    app_id = config.get(CONF_APP_ID, None)
    encryption_key = config.get(CONF_ENCRYPTION_KEY, None)

    if discovery_info:
        _LOGGER.debug("%s", discovery_info)
        name = discovery_info.get("name")
        host = discovery_info.get("host")
        port = discovery_info.get("port")
        udn = discovery_info.get("udn")
        if udn and udn.startswith("uuid:"):
            uuid = udn[len("uuid:") :]
        else:
            uuid = None
        remote = RemoteControl(host, port)
        add_entities([PanasonicVieraTVDevice(mac, name, remote, host, app_power, uuid)])
        return True

    host = config.get(CONF_HOST)
    remote = RemoteControl(host, port, app_id=app_id, encryption_key=encryption_key)

    add_entities(
        [PanasonicVieraTVDevice(mac, name, remote, host, broadcast, app_power)]
    )
    return True

Thanks,
ttecles

@ttecles did you ever ge this to work?

Not inside home assistant

I played around with this update. I’m keen to get this working with my Panasonic TV.

Have installed as custom component under HA 0.107.6. Generated app_id and key and added to my configurations.yaml.

I’ve found that the first command will work and all subsequent commands fail with the HTTP 500 error, until HA is restarted. So, if my TV is off I can turn it on from HA (using Media Player card in Lovelace) but then turning off from HA fails and all further commands fail. If I re-start HA and my TV is on I can turn it off from HA but trying to turn it on again fails.

Same happened to me, but I did not have time to run home assistant with a debugger to find out the problem…

same problem here, anyone got a fix?

they solved the problem in version 109.4. But there is an issue with the state