2022-08-10 11:11:49 +00:00
|
|
|
"""The Bravia TV integration."""
|
2024-03-08 13:51:32 +00:00
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
|
|
|
from typing import Final
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2022-08-10 11:11:49 +00:00
|
|
|
from aiohttp import CookieJar
|
2023-01-05 10:34:07 +00:00
|
|
|
from pybravia import BraviaClient
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2021-07-03 13:37:54 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2022-10-05 08:24:52 +00:00
|
|
|
from homeassistant.const import CONF_HOST, CONF_MAC, Platform
|
2021-07-03 13:37:54 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2022-08-10 11:11:49 +00:00
|
|
|
from homeassistant.helpers.aiohttp_client import async_create_clientsession
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2022-08-10 11:11:49 +00:00
|
|
|
from .coordinator import BraviaTVCoordinator
|
2021-06-17 10:33:44 +00:00
|
|
|
|
2024-05-04 13:50:33 +00:00
|
|
|
BraviaTVConfigEntry = ConfigEntry[BraviaTVCoordinator]
|
|
|
|
|
2022-09-23 13:39:24 +00:00
|
|
|
PLATFORMS: Final[list[Platform]] = [
|
|
|
|
Platform.BUTTON,
|
|
|
|
Platform.MEDIA_PLAYER,
|
|
|
|
Platform.REMOTE,
|
|
|
|
]
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
|
2024-05-04 13:50:33 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, config_entry: BraviaTVConfigEntry
|
|
|
|
) -> bool:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Set up a config entry."""
|
|
|
|
host = config_entry.data[CONF_HOST]
|
|
|
|
mac = config_entry.data[CONF_MAC]
|
|
|
|
|
2022-08-10 11:11:49 +00:00
|
|
|
session = async_create_clientsession(
|
|
|
|
hass, cookie_jar=CookieJar(unsafe=True, quote_cookie=False)
|
|
|
|
)
|
2023-01-05 10:34:07 +00:00
|
|
|
client = BraviaClient(host, mac, session=session)
|
2022-09-23 13:03:43 +00:00
|
|
|
coordinator = BraviaTVCoordinator(
|
|
|
|
hass=hass,
|
|
|
|
client=client,
|
2022-10-05 08:24:52 +00:00
|
|
|
config=config_entry.data,
|
2022-09-23 13:03:43 +00:00
|
|
|
)
|
2021-06-21 04:39:14 +00:00
|
|
|
config_entry.async_on_unload(config_entry.add_update_listener(update_listener))
|
2020-04-29 14:27:45 +00:00
|
|
|
|
2021-06-17 10:33:44 +00:00
|
|
|
await coordinator.async_config_entry_first_refresh()
|
|
|
|
|
2024-05-04 13:50:33 +00:00
|
|
|
config_entry.runtime_data = coordinator
|
2020-04-14 23:04:06 +00:00
|
|
|
|
2022-07-09 15:27:42 +00:00
|
|
|
await hass.config_entries.async_forward_entry_setups(config_entry, PLATFORMS)
|
2020-04-14 23:04:06 +00:00
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2024-05-04 13:50:33 +00:00
|
|
|
async def async_unload_entry(
|
|
|
|
hass: HomeAssistant, config_entry: BraviaTVConfigEntry
|
|
|
|
) -> bool:
|
2020-04-14 23:04:06 +00:00
|
|
|
"""Unload a config entry."""
|
2024-05-04 13:50:33 +00:00
|
|
|
return await hass.config_entries.async_unload_platforms(config_entry, PLATFORMS)
|
2020-04-29 14:27:45 +00:00
|
|
|
|
|
|
|
|
2024-05-04 13:50:33 +00:00
|
|
|
async def update_listener(
|
|
|
|
hass: HomeAssistant, config_entry: BraviaTVConfigEntry
|
|
|
|
) -> None:
|
2020-04-29 14:27:45 +00:00
|
|
|
"""Handle options update."""
|
|
|
|
await hass.config_entries.async_reload(config_entry.entry_id)
|