2020-04-14 23:04:06 +00:00
|
|
|
"""Adds config flow for Bravia TV integration."""
|
2021-07-03 13:37:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from contextlib import suppress
|
2020-04-14 23:04:06 +00:00
|
|
|
import ipaddress
|
|
|
|
import re
|
2021-07-03 13:37:54 +00:00
|
|
|
from typing import Any
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
from bravia_tv import BraviaRC
|
2020-05-25 20:05:52 +00:00
|
|
|
from bravia_tv.braviarc import NoIPControl
|
2020-04-14 23:04:06 +00:00
|
|
|
import voluptuous as vol
|
|
|
|
|
|
|
|
from homeassistant import config_entries, exceptions
|
2021-07-03 13:37:54 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2020-04-14 23:04:06 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_MAC, CONF_PIN
|
|
|
|
from homeassistant.core import callback
|
2021-07-03 13:37:54 +00:00
|
|
|
from homeassistant.data_entry_flow import FlowResult
|
2020-04-14 23:04:06 +00:00
|
|
|
import homeassistant.helpers.config_validation as cv
|
|
|
|
|
2021-03-02 08:02:04 +00:00
|
|
|
from .const import (
|
2020-04-14 23:04:06 +00:00
|
|
|
ATTR_CID,
|
|
|
|
ATTR_MAC,
|
|
|
|
ATTR_MODEL,
|
|
|
|
CLIENTID_PREFIX,
|
|
|
|
CONF_IGNORED_SOURCES,
|
|
|
|
DOMAIN,
|
|
|
|
NICKNAME,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
def host_valid(host: str) -> bool:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Return True if hostname or IP address is valid."""
|
2021-07-03 13:37:54 +00:00
|
|
|
with suppress(ValueError):
|
2021-04-29 14:09:59 +00:00
|
|
|
if ipaddress.ip_address(host).version in [4, 6]:
|
2020-04-14 23:04:06 +00:00
|
|
|
return True
|
2021-07-03 13:37:54 +00:00
|
|
|
disallowed = re.compile(r"[^a-zA-Z\d\-]")
|
|
|
|
return all(x and not disallowed.search(x) for x in host.split("."))
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
|
|
|
|
class BraviaTVConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
|
|
|
"""Handle a config flow for BraviaTV integration."""
|
|
|
|
|
|
|
|
VERSION = 1
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
def __init__(self) -> None:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Initialize."""
|
2021-07-03 13:37:54 +00:00
|
|
|
self.braviarc: BraviaRC | None = None
|
|
|
|
self.host: str | None = None
|
|
|
|
self.title = ""
|
|
|
|
self.mac: str | None = None
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def init_device(self, pin: str) -> None:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Initialize Bravia TV device."""
|
2021-07-03 13:37:54 +00:00
|
|
|
assert self.braviarc is not None
|
2020-04-14 23:04:06 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
2020-05-25 20:05:52 +00:00
|
|
|
self.braviarc.connect, pin, CLIENTID_PREFIX, NICKNAME
|
2020-04-14 23:04:06 +00:00
|
|
|
)
|
|
|
|
|
2020-07-05 10:02:45 +00:00
|
|
|
connected = await self.hass.async_add_executor_job(self.braviarc.is_connected)
|
|
|
|
if not connected:
|
2020-04-14 23:04:06 +00:00
|
|
|
raise CannotConnect()
|
|
|
|
|
2020-04-21 14:46:12 +00:00
|
|
|
system_info = await self.hass.async_add_executor_job(
|
|
|
|
self.braviarc.get_system_info
|
|
|
|
)
|
|
|
|
if not system_info:
|
2020-04-14 23:04:06 +00:00
|
|
|
raise ModelNotSupported()
|
|
|
|
|
|
|
|
await self.async_set_unique_id(system_info[ATTR_CID].lower())
|
|
|
|
self._abort_if_unique_id_configured()
|
|
|
|
|
|
|
|
self.title = system_info[ATTR_MODEL]
|
|
|
|
self.mac = system_info[ATTR_MAC]
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
@callback
|
2021-07-03 13:37:54 +00:00
|
|
|
def async_get_options_flow(config_entry: ConfigEntry) -> BraviaTVOptionsFlowHandler:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Bravia TV options callback."""
|
|
|
|
return BraviaTVOptionsFlowHandler(config_entry)
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Handle the initial step."""
|
2021-07-03 13:37:54 +00:00
|
|
|
errors: dict[str, str] = {}
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
if host_valid(user_input[CONF_HOST]):
|
|
|
|
self.host = user_input[CONF_HOST]
|
|
|
|
self.braviarc = BraviaRC(self.host)
|
|
|
|
|
|
|
|
return await self.async_step_authorize()
|
|
|
|
|
|
|
|
errors[CONF_HOST] = "invalid_host"
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_HOST, default=""): str}),
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_step_authorize(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Get PIN from the Bravia TV device."""
|
2021-07-03 13:37:54 +00:00
|
|
|
errors: dict[str, str] = {}
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
if user_input is not None:
|
|
|
|
try:
|
|
|
|
await self.init_device(user_input[CONF_PIN])
|
|
|
|
except CannotConnect:
|
|
|
|
errors["base"] = "cannot_connect"
|
|
|
|
except ModelNotSupported:
|
|
|
|
errors["base"] = "unsupported_model"
|
|
|
|
else:
|
|
|
|
user_input[CONF_HOST] = self.host
|
|
|
|
user_input[CONF_MAC] = self.mac
|
|
|
|
return self.async_create_entry(title=self.title, data=user_input)
|
|
|
|
# Connecting with th PIN "0000" to start the pairing process on the TV.
|
2020-05-25 20:05:52 +00:00
|
|
|
try:
|
2021-07-03 13:37:54 +00:00
|
|
|
assert self.braviarc is not None
|
2020-05-25 20:05:52 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
|
|
|
self.braviarc.connect, "0000", CLIENTID_PREFIX, NICKNAME
|
|
|
|
)
|
|
|
|
except NoIPControl:
|
|
|
|
return self.async_abort(reason="no_ip_control")
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="authorize",
|
|
|
|
data_schema=vol.Schema({vol.Required(CONF_PIN, default=""): str}),
|
|
|
|
errors=errors,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class BraviaTVOptionsFlowHandler(config_entries.OptionsFlow):
|
|
|
|
"""Config flow options for Bravia TV."""
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
def __init__(self, config_entry: ConfigEntry) -> None:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Initialize Bravia TV options flow."""
|
|
|
|
self.config_entry = config_entry
|
|
|
|
self.pin = config_entry.data[CONF_PIN]
|
|
|
|
self.ignored_sources = config_entry.options.get(CONF_IGNORED_SOURCES)
|
2021-07-03 13:37:54 +00:00
|
|
|
self.source_list: dict[str, str] = {}
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_step_init(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Manage the options."""
|
2021-06-21 04:39:14 +00:00
|
|
|
coordinator = self.hass.data[DOMAIN][self.config_entry.entry_id]
|
2021-07-03 13:37:54 +00:00
|
|
|
braviarc = coordinator.braviarc
|
|
|
|
connected = await self.hass.async_add_executor_job(braviarc.is_connected)
|
2020-07-05 10:02:45 +00:00
|
|
|
if not connected:
|
2020-04-14 23:04:06 +00:00
|
|
|
await self.hass.async_add_executor_job(
|
2021-07-03 13:37:54 +00:00
|
|
|
braviarc.connect, self.pin, CLIENTID_PREFIX, NICKNAME
|
2020-04-14 23:04:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
content_mapping = await self.hass.async_add_executor_job(
|
2021-07-03 13:37:54 +00:00
|
|
|
braviarc.load_source_list
|
2020-04-14 23:04:06 +00:00
|
|
|
)
|
2021-08-21 08:41:23 +00:00
|
|
|
self.source_list = {item: item for item in content_mapping}
|
2020-04-14 23:04:06 +00:00
|
|
|
return await self.async_step_user()
|
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
async def async_step_user(
|
|
|
|
self, user_input: dict[str, Any] | None = None
|
|
|
|
) -> FlowResult:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Handle a flow initialized by the user."""
|
|
|
|
if user_input is not None:
|
|
|
|
return self.async_create_entry(title="", data=user_input)
|
|
|
|
|
|
|
|
return self.async_show_form(
|
|
|
|
step_id="user",
|
|
|
|
data_schema=vol.Schema(
|
|
|
|
{
|
|
|
|
vol.Optional(
|
|
|
|
CONF_IGNORED_SOURCES, default=self.ignored_sources
|
|
|
|
): cv.multi_select(self.source_list)
|
|
|
|
}
|
|
|
|
),
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class CannotConnect(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate we cannot connect."""
|
|
|
|
|
|
|
|
|
|
|
|
class ModelNotSupported(exceptions.HomeAssistantError):
|
|
|
|
"""Error to indicate not supported model."""
|