async def validate_http(hass: core.HomeAssistant, data): """Validate the user input allows us to connect over HTTP.""" host = data[CONF_HOST] port = data[CONF_PORT] username = data.get(CONF_USERNAME) password = data.get(CONF_PASSWORD) ssl = data.get(CONF_SSL) session = async_get_clientsession(hass) _LOGGER.debug("Connecting to %s:%s over HTTP", host, port) khc = get_kodi_connection(host, port, None, username, password, ssl, session=session) kodi = Kodi(khc) try: await kodi.ping() except CannotConnectError as error: raise CannotConnect from error except InvalidAuthError as error: raise InvalidAuth from error
async def validate_ws(hass: core.HomeAssistant, data): """Validate the user input allows us to connect over WS.""" ws_port = data.get(CONF_WS_PORT) if not ws_port: return host = data[CONF_HOST] port = data[CONF_PORT] username = data.get(CONF_USERNAME) password = data.get(CONF_PASSWORD) ssl = data.get(CONF_SSL) session = async_get_clientsession(hass) _LOGGER.debug("Connecting to %s:%s over WebSocket", host, ws_port) kwc = get_kodi_connection(host, port, ws_port, username, password, ssl, session=session) try: await kwc.connect() if not kwc.connected: _LOGGER.warning("Cannot connect to %s:%s over WebSocket", host, ws_port) raise WSCannotConnect() kodi = Kodi(kwc) await kodi.ping() except CannotConnectError as error: raise WSCannotConnect from error
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Kodi from a config entry.""" conn = get_kodi_connection( entry.data[CONF_HOST], entry.data[CONF_PORT], entry.data[CONF_WS_PORT], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], entry.data[CONF_SSL], session=async_get_clientsession(hass), ) try: await conn.connect() kodi = Kodi(conn) await kodi.ping() raw_version = (await kodi.get_application_properties(["version"]))["version"] except CannotConnectError as error: raise ConfigEntryNotReady from error except InvalidAuthError as error: _LOGGER.error( "Login to %s failed: [%s]", entry.data[CONF_HOST], error, ) return False async def _close(event): await conn.close() remove_stop_listener = hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) version = f"{raw_version['major']}.{raw_version['minor']}" hass.data[DOMAIN][entry.entry_id] = { DATA_CONNECTION: conn, DATA_KODI: kodi, DATA_REMOVE_LISTENER: remove_stop_listener, DATA_VERSION: version, } for component in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, component)) return True
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): """Set up Kodi from a config entry.""" conn = get_kodi_connection( entry.data[CONF_HOST], entry.data[CONF_PORT], entry.data[CONF_WS_PORT], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], entry.data[CONF_SSL], session=async_get_clientsession(hass), ) kodi = Kodi(conn) try: await conn.connect() except CannotConnectError: pass except InvalidAuthError as error: _LOGGER.error( "Login to %s failed: [%s]", entry.data[CONF_HOST], error, ) return False async def _close(event): await conn.close() remove_stop_listener = hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STOP, _close) hass.data[DOMAIN][entry.entry_id] = { DATA_CONNECTION: conn, DATA_KODI: kodi, DATA_REMOVE_LISTENER: remove_stop_listener, } for platform in PLATFORMS: hass.async_create_task( hass.config_entries.async_forward_entry_setup(entry, platform)) return True
async def async_setup_entry(opp: OpenPeerPower, entry: ConfigEntry): """Set up Kodi from a config entry.""" conn = get_kodi_connection( entry.data[CONF_HOST], entry.data[CONF_PORT], entry.data[CONF_WS_PORT], entry.data[CONF_USERNAME], entry.data[CONF_PASSWORD], entry.data[CONF_SSL], session=async_get_clientsession(opp), ) kodi = Kodi(conn) try: await conn.connect() except CannotConnectError: pass except InvalidAuthError as error: _LOGGER.error( "Login to %s failed: [%s]", entry.data[CONF_HOST], error, ) return False async def _close(event): await conn.close() remove_stop_listener = opp.bus.async_listen_once(EVENT_OPENPEERPOWER_STOP, _close) opp.data.setdefault(DOMAIN, {}) opp.data[DOMAIN][entry.entry_id] = { DATA_CONNECTION: conn, DATA_KODI: kodi, DATA_REMOVE_LISTENER: remove_stop_listener, } opp.config_entries.async_setup_platforms(entry, PLATFORMS) return True
_LOGGER.debug("Connecting to %s:%s over WebSocket", host, ws_port) kwc = get_kodi_connection(host, port, ws_port, username, password, ssl, session=session) try: await kwc.connect() if not kwc.connected: _LOGGER.warning("Cannot connect to %s:%s over WebSocket", host, ws_port) raise WSCannotConnect() kodi = Kodi(kwc) await kodi.ping() except CannotConnectError as error: raise WSCannotConnect from error class KodiConfigFlow(config_entries.ConfigFlow, domain=DOMAIN): """Handle a config flow for Kodi.""" VERSION = 1 def __init__(self): """Initialize flow.""" self._host: str | None = None self._port: int | None = DEFAULT_PORT self._ws_port: int | None = DEFAULT_WS_PORT