Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add config flow to panasonic_viera component #33829

Merged
merged 47 commits into from
Apr 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
4b2d72f
Updating the panasonic_viera component
Apr 8, 2020
41e007e
Updating .coveragerc
Apr 8, 2020
3444198
Removing testplatform
Apr 8, 2020
743549d
Updating strings.json
Apr 8, 2020
9ed6be1
Commit before rebase
Apr 10, 2020
db1d08f
Commit before rebase
Apr 10, 2020
b0d5c33
Commit before rebase
Apr 10, 2020
646e676
Commit before rebase
Apr 11, 2020
5820e94
Commit before rebase
Apr 11, 2020
529045c
Adding tests and stuff
Apr 11, 2020
0820ba9
Fixing permission issues
Apr 11, 2020
8d0e6e7
Ignoring Pylint warnings
Apr 12, 2020
ccc830e
Fixing one more Pylint warning
Apr 12, 2020
1e9ff71
Refactoring
Apr 12, 2020
4e56da4
Commiting changes - part 1
Apr 13, 2020
a1f24dd
Commiting changes: part 2
Apr 15, 2020
916904c
Turning unknown error logs into exception logs
Apr 16, 2020
8a9336a
Update strings.json
Apr 16, 2020
f56c3dd
Rebasing
Apr 16, 2020
56842af
Updating the panasonic_viera component
Apr 8, 2020
4bdbc46
Removing testplatform
Apr 8, 2020
0d694b7
Updating strings.json
Apr 8, 2020
5ee232c
Commit before rebase
Apr 10, 2020
e4c5636
Commit before rebase
Apr 10, 2020
cb6cf6e
Commit before rebase
Apr 10, 2020
a32c6ef
Commit before rebase
Apr 11, 2020
d5dda1f
Commit before rebase
Apr 11, 2020
a6aad48
Adding tests and stuff
Apr 11, 2020
ff94e9b
Fixing permission issues
Apr 11, 2020
ce79821
Ignoring Pylint warnings
Apr 12, 2020
88458d2
Fixing one more Pylint warning
Apr 12, 2020
974ffcb
Refactoring
Apr 12, 2020
02dc03b
Commiting changes - part 1
Apr 13, 2020
5262fc4
Commiting changes: part 2
Apr 15, 2020
4e3b085
Turning unknown error logs into exception logs
Apr 16, 2020
f97647b
Adding pt-BR translation
Apr 16, 2020
57c93eb
Removing Brazilian Portugues translations
Apr 16, 2020
a10b6ce
Modifying error handling
Apr 16, 2020
b415ae2
Adding SOAPError to except handling
Apr 16, 2020
02bf10c
Updating translation
Apr 16, 2020
e3ce86f
Refactoring async_step_import
Apr 16, 2020
877e366
Fixing indentation
Apr 17, 2020
8a30bfa
Fixing requirements after rebase
Apr 17, 2020
5e41613
Fixing translations
Apr 17, 2020
bdc41a1
Fixing issues after rebase
Apr 17, 2020
a1eb27a
Routing import step to user step
Apr 18, 2020
41e7333
Adding myself as a codeowner
Apr 18, 2020
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,7 @@ omit =
homeassistant/components/osramlightify/light.py
homeassistant/components/otp/sensor.py
homeassistant/components/panasonic_bluray/media_player.py
homeassistant/components/panasonic_viera/__init__.py
homeassistant/components/panasonic_viera/media_player.py
homeassistant/components/pandora/media_player.py
homeassistant/components/pcal9535a/*
Expand Down
1 change: 1 addition & 0 deletions CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ homeassistant/components/openweathermap/* @fabaff
homeassistant/components/opnsense/* @mtreinish
homeassistant/components/orangepi_gpio/* @pascallj
homeassistant/components/oru/* @bvlaicu
homeassistant/components/panasonic_viera/* @joogps
homeassistant/components/panel_custom/* @home-assistant/frontend
homeassistant/components/panel_iframe/* @home-assistant/frontend
homeassistant/components/pcal9535a/* @Shulyaka
Expand Down
31 changes: 31 additions & 0 deletions homeassistant/components/panasonic_viera/.translations/en.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"config": {
"abort": {
"already_configured": "This Panasonic Viera TV is already configured.",
"not_connected": "The remote connection with your Panasonic Viera TV was lost. Check the logs for more information.",
"unknown": "An unknown error occured. Check the logs for more information."
},
"error": {
"invalid_pin_code": "The PIN code you entered was invalid",
"not_connected": "Could not establish a remote connection with your Panasonic Viera TV"
},
"step": {
"pairing": {
"data": {
"pin": "PIN"
},
"description": "Enter the PIN displayed on your TV",
"title": "Pairing"
},
"user": {
"data": {
"host": "IP address",
"name": "Name"
},
"description": "Enter your Panasonic Viera TV's IP address",
"title": "Setup your TV"
}
}
},
"title": "Panasonic Viera"
}
70 changes: 69 additions & 1 deletion homeassistant/components/panasonic_viera/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,69 @@
"""The panasonic_viera component."""
"""The Panasonic Viera integration."""
import asyncio

import voluptuous as vol

from homeassistant.config_entries import SOURCE_IMPORT
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PORT
import homeassistant.helpers.config_validation as cv

from .const import CONF_ON_ACTION, DEFAULT_NAME, DEFAULT_PORT, DOMAIN

CONFIG_SCHEMA = vol.Schema(
{
DOMAIN: vol.All(
cv.ensure_list,
[
vol.Schema(
{
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_ON_ACTION): cv.SCRIPT_SCHEMA,
}
)
],
)
},
extra=vol.ALLOW_EXTRA,
)

PLATFORMS = ["media_player"]


async def async_setup(hass, config):
"""Set up Panasonic Viera from configuration.yaml."""
if DOMAIN not in config:
return True

for conf in config[DOMAIN]:
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN, context={"source": SOURCE_IMPORT}, data=conf
)
)

return True


async def async_setup_entry(hass, config_entry):
"""Set up Panasonic Viera from a config entry."""

for component in PLATFORMS:
hass.async_create_task(
hass.config_entries.async_forward_entry_setup(config_entry, component)
)

return True


async def async_unload_entry(hass, config_entry):
"""Unload a config entry."""
return all(
await asyncio.gather(
*[
hass.config_entries.async_forward_entry_unload(config_entry, component)
for component in PLATFORMS
]
)
)
152 changes: 152 additions & 0 deletions homeassistant/components/panasonic_viera/config_flow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
"""Config flow for Panasonic Viera TV integration."""
from functools import partial
import logging
from urllib.request import URLError

from panasonic_viera import TV_TYPE_ENCRYPTED, RemoteControl, SOAPError
import voluptuous as vol

from homeassistant import config_entries
from homeassistant.const import CONF_HOST, CONF_NAME, CONF_PIN, CONF_PORT

from .const import ( # pylint: disable=unused-import
CONF_APP_ID,
CONF_ENCRYPTION_KEY,
CONF_ON_ACTION,
DEFAULT_NAME,
DEFAULT_PORT,
DOMAIN,
ERROR_INVALID_PIN_CODE,
ERROR_NOT_CONNECTED,
REASON_NOT_CONNECTED,
REASON_UNKNOWN,
)

_LOGGER = logging.getLogger(__name__)


class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
"""Config flow for Panasonic Viera."""

VERSION = 1
CONNECTION_CLASS = config_entries.CONN_CLASS_LOCAL_POLL

def __init__(self):
"""Initialize the Panasonic Viera config flow."""
self._data = {
CONF_HOST: None,
CONF_NAME: None,
CONF_PORT: None,
CONF_ON_ACTION: None,
}

self._remote = None

async def async_step_user(self, user_input=None):
"""Handle the initial step."""
errors = {}

if user_input is not None:
await self.async_load_data(user_input)
try:
self._remote = await self.hass.async_add_executor_job(
partial(RemoteControl, self._data[CONF_HOST], self._data[CONF_PORT])
)
except (TimeoutError, URLError, SOAPError, OSError) as err:
_LOGGER.error("Could not establish remote connection: %s", err)
errors["base"] = ERROR_NOT_CONNECTED
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception("An unknown error occurred: %s", err)
return self.async_abort(reason=REASON_UNKNOWN)

if "base" not in errors:
if self._remote.type == TV_TYPE_ENCRYPTED:
return await self.async_step_pairing()

return self.async_create_entry(
title=self._data[CONF_NAME], data=self._data,
)

return self.async_show_form(
step_id="user",
data_schema=vol.Schema(
{
vol.Required(
CONF_HOST,
default=self._data[CONF_HOST]
if self._data[CONF_HOST] is not None
else "",
): str,
vol.Optional(
CONF_NAME,
default=self._data[CONF_NAME]
if self._data[CONF_NAME] is not None
else DEFAULT_NAME,
): str,
}
),
errors=errors,
)

async def async_step_pairing(self, user_input=None):
"""Handle the pairing step."""
errors = {}

if user_input is not None:
pin = user_input[CONF_PIN]
try:
self._remote.authorize_pin_code(pincode=pin)
except SOAPError as err:
_LOGGER.error("Invalid PIN code: %s", err)
errors["base"] = ERROR_INVALID_PIN_CODE
except (TimeoutError, URLError, OSError) as err:
_LOGGER.error("The remote connection was lost: %s", err)
return self.async_abort(reason=REASON_NOT_CONNECTED)
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception("Unknown error: %s", err)
return self.async_abort(reason=REASON_UNKNOWN)

if "base" not in errors:
encryption_data = {
CONF_APP_ID: self._remote.app_id,
CONF_ENCRYPTION_KEY: self._remote.enc_key,
}

self._data = {**self._data, **encryption_data}

return self.async_create_entry(
title=self._data[CONF_NAME], data=self._data,
)

try:
self._remote.request_pin_code(name="Home Assistant")
except (TimeoutError, URLError, SOAPError, OSError) as err:
_LOGGER.error("The remote connection was lost: %s", err)
return self.async_abort(reason=REASON_NOT_CONNECTED)
except Exception as err: # pylint: disable=broad-except
_LOGGER.exception("Unknown error: %s", err)
return self.async_abort(reason=REASON_UNKNOWN)

return self.async_show_form(
step_id="pairing",
data_schema=vol.Schema({vol.Required(CONF_PIN): str}),
errors=errors,
)

async def async_step_import(self, import_config):
"""Import a config entry from configuration.yaml."""
return await self.async_step_user(user_input=import_config)

async def async_load_data(self, config):
"""Load the data."""
self._data = config

self._data[CONF_PORT] = (
self._data[CONF_PORT] if CONF_PORT in self._data else DEFAULT_PORT
)
self._data[CONF_ON_ACTION] = (
self._data[CONF_ON_ACTION] if CONF_ON_ACTION in self._data else None
)

await self.async_set_unique_id(self._data[CONF_HOST])
self._abort_if_unique_id_configured()
17 changes: 17 additions & 0 deletions homeassistant/components/panasonic_viera/const.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
"""Constants for the Panasonic Viera integration."""
DOMAIN = "panasonic_viera"

DEVICE_MANUFACTURER = "Panasonic"

CONF_ON_ACTION = "turn_on_action"
CONF_APP_ID = "app_id"
CONF_ENCRYPTION_KEY = "encryption_key"

DEFAULT_NAME = "Panasonic Viera TV"
DEFAULT_PORT = 55000

ERROR_NOT_CONNECTED = "not_connected"
ERROR_INVALID_PIN_CODE = "invalid_pin_code"

REASON_NOT_CONNECTED = "not_connected"
REASON_UNKNOWN = "unknown"
7 changes: 4 additions & 3 deletions homeassistant/components/panasonic_viera/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
{
"domain": "panasonic_viera",
"name": "Panasonic Viera TV",
"name": "Panasonic Viera",
"documentation": "https://www.home-assistant.io/integrations/panasonic_viera",
"requirements": ["panasonic_viera==0.3.2", "wakeonlan==1.1.6"],
"codeowners": []
"requirements": ["panasonic_viera==0.3.5"],
"codeowners": ["@joogps"],
"config_flow": true
}
Loading