2019-09-11 18:34:10 +00:00
""" Home Assistant Cast integration for Cast. """
2021-03-17 22:43:55 +00:00
from __future__ import annotations
2019-09-11 18:34:10 +00:00
from pychromecast . controllers . homeassistant import HomeAssistantController
2019-12-08 16:30:57 +00:00
import voluptuous as vol
2019-09-11 18:34:10 +00:00
from homeassistant import auth , config_entries , core
from homeassistant . const import ATTR_ENTITY_ID
2022-02-03 21:43:23 +00:00
from homeassistant . exceptions import HomeAssistantError
2019-09-11 18:34:10 +00:00
from homeassistant . helpers import config_validation as cv , dispatcher
2022-02-03 21:43:23 +00:00
from homeassistant . helpers . network import NoURLAvailableError , get_url
2022-05-17 17:56:57 +00:00
from homeassistant . helpers . service import async_register_admin_service
2019-09-11 18:34:10 +00:00
from . const import DOMAIN , SIGNAL_HASS_CAST_SHOW_VIEW
SERVICE_SHOW_VIEW = " show_lovelace_view "
ATTR_VIEW_PATH = " view_path "
2020-03-26 23:23:46 +00:00
ATTR_URL_PATH = " dashboard_path "
2022-02-03 21:43:23 +00:00
NO_URL_AVAILABLE_ERROR = " Home Assistant Cast requires your instance to be reachable via HTTPS. Enable Home Assistant Cloud or set up an external URL with valid SSL certificates "
2019-09-11 18:34:10 +00:00
async def async_setup_ha_cast (
hass : core . HomeAssistant , entry : config_entries . ConfigEntry
) :
""" Set up Home Assistant Cast. """
2021-03-17 22:43:55 +00:00
user_id : str | None = entry . data . get ( " user_id " )
user : auth . models . User | None = None
2019-09-11 18:34:10 +00:00
if user_id is not None :
user = await hass . auth . async_get_user ( user_id )
if user is None :
user = await hass . auth . async_create_system_user (
2022-01-10 16:10:46 +00:00
" Home Assistant Cast " , group_ids = [ auth . const . GROUP_ID_ADMIN ]
2019-09-11 18:34:10 +00:00
)
hass . config_entries . async_update_entry (
entry , data = { * * entry . data , " user_id " : user . id }
)
if user . refresh_tokens :
refresh_token : auth . models . RefreshToken = list ( user . refresh_tokens . values ( ) ) [ 0 ]
else :
refresh_token = await hass . auth . async_create_refresh_token ( user )
2021-12-30 20:12:40 +00:00
async def handle_show_view ( call : core . ServiceCall ) - > None :
2019-09-11 18:34:10 +00:00
""" Handle a Show View service call. """
2022-02-03 21:43:23 +00:00
try :
hass_url = get_url ( hass , require_ssl = True , prefer_external = True )
except NoURLAvailableError as err :
raise HomeAssistantError ( NO_URL_AVAILABLE_ERROR ) from err
2019-09-17 07:23:31 +00:00
2019-09-11 18:34:10 +00:00
controller = HomeAssistantController (
# If you are developing Home Assistant Cast, uncomment and set to your dev app id.
# app_id="5FE44367",
2019-09-17 07:23:31 +00:00
hass_url = hass_url ,
2019-09-11 18:34:10 +00:00
client_id = None ,
refresh_token = refresh_token . token ,
)
dispatcher . async_dispatcher_send (
hass ,
SIGNAL_HASS_CAST_SHOW_VIEW ,
controller ,
call . data [ ATTR_ENTITY_ID ] ,
call . data [ ATTR_VIEW_PATH ] ,
2020-03-26 23:23:46 +00:00
call . data . get ( ATTR_URL_PATH ) ,
2019-09-11 18:34:10 +00:00
)
2022-05-17 17:56:57 +00:00
async_register_admin_service (
hass ,
2019-09-11 18:34:10 +00:00
DOMAIN ,
SERVICE_SHOW_VIEW ,
handle_show_view ,
2020-03-26 23:23:46 +00:00
vol . Schema (
{
ATTR_ENTITY_ID : cv . entity_id ,
ATTR_VIEW_PATH : str ,
vol . Optional ( ATTR_URL_PATH ) : str ,
}
) ,
2019-09-11 18:34:10 +00:00
)
2020-12-16 10:00:22 +00:00
async def async_remove_user (
hass : core . HomeAssistant , entry : config_entries . ConfigEntry
) :
""" Remove Home Assistant Cast user. """
2021-03-17 22:43:55 +00:00
user_id : str | None = entry . data . get ( " user_id " )
2020-12-16 10:00:22 +00:00
2021-10-31 17:45:27 +00:00
if user_id is not None and ( user := await hass . auth . async_get_user ( user_id ) ) :
await hass . auth . async_remove_user ( user )