| 1 |
"""Client interface for Home Assistant to cloud.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
from abc import ABC, abstractmethod |
| 6 |
from asyncio import AbstractEventLoop |
| 7 |
from pathlib import Path |
| 8 |
from typing import TYPE_CHECKING, Any, Literal |
| 9 |
|
| 10 |
from aiohttp import ClientSession |
| 11 |
from aiohttp.web import AppRunner |
| 12 |
|
| 13 |
from .iot import HandlerError |
| 14 |
|
| 15 |
if TYPE_CHECKING: |
| 16 |
from . import Cloud |
| 17 |
|
| 18 |
|
| 19 |
class RemoteActivationNotAllowed(HandlerError): |
| 20 |
"""Raised when it's not allowed to remotely activate remote UI.""" |
| 21 |
|
| 22 |
def __init__(self) -> None: |
| 23 |
"""Initialize Error Message.""" |
| 24 |
super().__init__("remote_activation_not_allowed") |
| 25 |
|
| 26 |
|
| 27 |
class CloudClient(ABC): |
| 28 |
"""Interface class for Home Assistant.""" |
| 29 |
|
| 30 |
cloud: Cloud |
| 31 |
|
| 32 |
@property |
| 33 |
@abstractmethod |
| 34 |
def base_path(self) -> Path: |
| 35 |
"""Return path to base dir.""" |
| 36 |
|
| 37 |
@property |
| 38 |
@abstractmethod |
| 39 |
def loop(self) -> AbstractEventLoop: |
| 40 |
"""Return client loop.""" |
| 41 |
|
| 42 |
@property |
| 43 |
@abstractmethod |
| 44 |
def websession(self) -> ClientSession: |
| 45 |
"""Return client session for aiohttp.""" |
| 46 |
|
| 47 |
@property |
| 48 |
@abstractmethod |
| 49 |
def client_name(self) -> str: |
| 50 |
"""Return name of the client, this will be used as the user-agent.""" |
| 51 |
|
| 52 |
@property |
| 53 |
@abstractmethod |
| 54 |
def aiohttp_runner(self) -> AppRunner | None: |
| 55 |
"""Return client webinterface aiohttp application.""" |
| 56 |
|
| 57 |
@property |
| 58 |
@abstractmethod |
| 59 |
def cloudhooks(self) -> dict[str, dict[str, str | bool]]: |
| 60 |
"""Return list of cloudhooks.""" |
| 61 |
|
| 62 |
@property |
| 63 |
@abstractmethod |
| 64 |
def remote_autostart(self) -> bool: |
| 65 |
"""Return true if we want start a remote connection.""" |
| 66 |
|
| 67 |
@abstractmethod |
| 68 |
async def cloud_connected(self) -> None: |
| 69 |
"""Cloud connected.""" |
| 70 |
|
| 71 |
@abstractmethod |
| 72 |
async def cloud_disconnected(self) -> None: |
| 73 |
"""Cloud disconnected.""" |
| 74 |
|
| 75 |
@abstractmethod |
| 76 |
async def cloud_started(self) -> None: |
| 77 |
"""Cloud started with an active subscription.""" |
| 78 |
|
| 79 |
@abstractmethod |
| 80 |
async def cloud_stopped(self) -> None: |
| 81 |
"""Cloud stopped.""" |
| 82 |
|
| 83 |
@abstractmethod |
| 84 |
async def logout_cleanups(self) -> None: |
| 85 |
"""Cleanup before logout.""" |
| 86 |
|
| 87 |
@abstractmethod |
| 88 |
async def async_cloud_connect_update(self, connect: bool) -> None: |
| 89 |
"""Process cloud remote message to client. |
| 90 |
|
| 91 |
If it's not allowed to remotely enable remote control, the implementation |
| 92 |
should raise RemoteActivationNotAllowed |
| 93 |
""" |
| 94 |
|
| 95 |
@abstractmethod |
| 96 |
async def async_cloud_connection_info( |
| 97 |
self, |
| 98 |
payload: dict[str, Any], |
| 99 |
) -> dict[str, Any]: |
| 100 |
"""Process cloud connection info message to client.""" |
| 101 |
|
| 102 |
@abstractmethod |
| 103 |
async def async_alexa_message(self, payload: dict[str, Any]) -> dict[str, Any]: |
| 104 |
"""Process cloud alexa message to client.""" |
| 105 |
|
| 106 |
@abstractmethod |
| 107 |
async def async_system_message(self, payload: dict[str, Any]) -> None: |
| 108 |
"""Process cloud system message to client.""" |
| 109 |
|
| 110 |
@abstractmethod |
| 111 |
async def async_google_message(self, payload: dict[str, Any]) -> dict[str, Any]: |
| 112 |
"""Process cloud google message to client.""" |
| 113 |
|
| 114 |
@abstractmethod |
| 115 |
async def async_webhook_message(self, payload: dict[str, Any]) -> dict[str, Any]: |
| 116 |
"""Process cloud webhook message to client.""" |
| 117 |
|
| 118 |
@abstractmethod |
| 119 |
async def async_cloudhooks_update( |
| 120 |
self, |
| 121 |
data: dict[str, dict[str, str | bool]], |
| 122 |
) -> None: |
| 123 |
"""Update local list of cloudhooks.""" |
| 124 |
|
| 125 |
@abstractmethod |
| 126 |
def dispatcher_message(self, identifier: str, data: Any = None) -> None: |
| 127 |
"""Send data to dispatcher.""" |
| 128 |
|
| 129 |
@abstractmethod |
| 130 |
def user_message(self, identifier: str, title: str, message: str) -> None: |
| 131 |
"""Create a message for user to UI.""" |
| 132 |
|
| 133 |
@abstractmethod |
| 134 |
async def async_create_repair_issue( |
| 135 |
self, |
| 136 |
identifier: str, |
| 137 |
translation_key: str, |
| 138 |
*, |
| 139 |
placeholders: dict[str, str] | None = None, |
| 140 |
severity: Literal["error", "warning"] = "warning", |
| 141 |
) -> None: |
| 142 |
"""Create a repair issue.""" |
| 143 |
|
| 144 |
@abstractmethod |
| 145 |
async def async_delete_repair_issue(self, identifier: str) -> None: |
| 146 |
"""Delete a repair issue.""" |