"""Constants for Google Assistant."""

from homeassistant.components import (
    binary_sensor,
    cover,
    event,
    humidifier,
    media_player,
    sensor,
    switch,
)
from homeassistant.components.alarm_control_panel import (
    DOMAIN as ALARM_CONTROL_PANEL_DOMAIN,
)
from homeassistant.components.binary_sensor import DOMAIN as BINARY_SENSOR_DOMAIN
from homeassistant.components.button import DOMAIN as BUTTON_DOMAIN
from homeassistant.components.camera import DOMAIN as CAMERA_DOMAIN
from homeassistant.components.climate import DOMAIN as CLIMATE_DOMAIN
from homeassistant.components.cover import DOMAIN as COVER_DOMAIN
from homeassistant.components.event import DOMAIN as EVENT_DOMAIN
from homeassistant.components.fan import DOMAIN as FAN_DOMAIN
from homeassistant.components.group import DOMAIN as GROUP_DOMAIN
from homeassistant.components.humidifier import DOMAIN as HUMIDIFIER_DOMAIN
from homeassistant.components.input_boolean import DOMAIN as INPUT_BOOLEAN_DOMAIN
from homeassistant.components.input_button import DOMAIN as INPUT_BUTTON_DOMAIN
from homeassistant.components.input_select import DOMAIN as INPUT_SELECT_DOMAIN
from homeassistant.components.lawn_mower import DOMAIN as LAWN_MOWER_DOMAIN
from homeassistant.components.light import DOMAIN as LIGHT_DOMAIN
from homeassistant.components.lock import DOMAIN as LOCK_DOMAIN
from homeassistant.components.media_player import DOMAIN as MEDIA_PLAYER_DOMAIN
from homeassistant.components.scene import DOMAIN as SCENE_DOMAIN
from homeassistant.components.script import DOMAIN as SCRIPT_DOMAIN
from homeassistant.components.select import DOMAIN as SELECT_DOMAIN
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN
from homeassistant.components.switch import DOMAIN as SWITCH_DOMAIN
from homeassistant.components.vacuum import DOMAIN as VACUUM_DOMAIN
from homeassistant.components.valve import DOMAIN as VALVE_DOMAIN
from homeassistant.components.water_heater import DOMAIN as WATER_HEATER_DOMAIN

DOMAIN = "google_assistant"

GOOGLE_ASSISTANT_API_ENDPOINT = "/api/google_assistant"

CONF_ALIASES = "aliases"
CONF_CLIENT_EMAIL = "client_email"
CONF_ENTITY_CONFIG = "entity_config"
CONF_EXPOSE = "expose"
CONF_EXPOSE_BY_DEFAULT = "expose_by_default"
CONF_EXPOSED_DOMAINS = "exposed_domains"
CONF_PRIVATE_KEY = "private_key"
CONF_PROJECT_ID = "project_id"
CONF_REPORT_STATE = "report_state"
CONF_ROOM_HINT = "room"
CONF_SECURE_DEVICES_PIN = "secure_devices_pin"
CONF_SERVICE_ACCOUNT = "service_account"

DATA_CONFIG = "config"

DEFAULT_EXPOSE_BY_DEFAULT = True
DEFAULT_EXPOSED_DOMAINS = [
    "alarm_control_panel",
    "binary_sensor",
    "climate",
    "cover",
    "event",
    "fan",
    "group",
    "humidifier",
    "input_boolean",
    "input_select",
    "lawn_mower",
    "light",
    "lock",
    "media_player",
    "scene",
    "script",
    "select",
    "sensor",
    "switch",
    "vacuum",
    "valve",
    "water_heater",
]

# https://developers.google.com/assistant/smarthome/guides
PREFIX_TYPES = "action.devices.types."
TYPE_ALARM = f"{PREFIX_TYPES}SECURITYSYSTEM"
TYPE_AWNING = f"{PREFIX_TYPES}AWNING"
TYPE_BLINDS = f"{PREFIX_TYPES}BLINDS"
TYPE_CAMERA = f"{PREFIX_TYPES}CAMERA"
TYPE_CURTAIN = f"{PREFIX_TYPES}CURTAIN"
TYPE_CARBON_MONOXIDE_DETECTOR = f"{PREFIX_TYPES}CARBON_MONOXIDE_DETECTOR"
TYPE_DEHUMIDIFIER = f"{PREFIX_TYPES}DEHUMIDIFIER"
TYPE_DOOR = f"{PREFIX_TYPES}DOOR"
TYPE_DOORBELL = f"{PREFIX_TYPES}DOORBELL"
TYPE_FAN = f"{PREFIX_TYPES}FAN"
TYPE_GARAGE = f"{PREFIX_TYPES}GARAGE"
TYPE_GATE = f"{PREFIX_TYPES}GATE"
TYPE_HUMIDIFIER = f"{PREFIX_TYPES}HUMIDIFIER"
TYPE_LIGHT = f"{PREFIX_TYPES}LIGHT"
TYPE_LOCK = f"{PREFIX_TYPES}LOCK"
TYPE_MOWER = f"{PREFIX_TYPES}MOWER"
TYPE_OUTLET = f"{PREFIX_TYPES}OUTLET"
TYPE_RECEIVER = f"{PREFIX_TYPES}AUDIO_VIDEO_RECEIVER"
TYPE_SCENE = f"{PREFIX_TYPES}SCENE"
TYPE_SENSOR = f"{PREFIX_TYPES}SENSOR"
TYPE_SETTOP = f"{PREFIX_TYPES}SETTOP"
TYPE_SHUTTER = f"{PREFIX_TYPES}SHUTTER"
TYPE_SMOKE_DETECTOR = f"{PREFIX_TYPES}SMOKE_DETECTOR"
TYPE_SPEAKER = f"{PREFIX_TYPES}SPEAKER"
TYPE_SWITCH = f"{PREFIX_TYPES}SWITCH"
TYPE_THERMOSTAT = f"{PREFIX_TYPES}THERMOSTAT"
TYPE_TV = f"{PREFIX_TYPES}TV"
TYPE_WINDOW = f"{PREFIX_TYPES}WINDOW"
TYPE_VACUUM = f"{PREFIX_TYPES}VACUUM"
TYPE_VALVE = f"{PREFIX_TYPES}VALVE"
TYPE_WATERHEATER = f"{PREFIX_TYPES}WATERHEATER"

SERVICE_REQUEST_SYNC = "request_sync"
HOMEGRAPH_URL = "https://homegraph.googleapis.com/"
HOMEGRAPH_SCOPE = "https://www.googleapis.com/auth/homegraph"
HOMEGRAPH_TOKEN_URL = "https://accounts.google.com/o/oauth2/token"
REQUEST_SYNC_BASE_URL = f"{HOMEGRAPH_URL}v1/devices:requestSync"
REPORT_STATE_BASE_URL = f"{HOMEGRAPH_URL}v1/devices:reportStateAndNotification"

# Error codes used for SmartHomeError class
# https://developers.google.com/actions/reference/smarthome/errors-exceptions
ERR_ALREADY_ARMED = "alreadyArmed"
ERR_ALREADY_DISARMED = "alreadyDisarmed"
ERR_ALREADY_STOPPED = "alreadyStopped"
ERR_CHALLENGE_NEEDED = "challengeNeeded"
ERR_CHALLENGE_NOT_SETUP = "challengeFailedNotSetup"
ERR_DEVICE_NOT_FOUND = "deviceNotFound"
ERR_DEVICE_OFFLINE = "deviceOffline"
ERR_FUNCTION_NOT_SUPPORTED = "functionNotSupported"
ERR_NO_AVAILABLE_CHANNEL = "noAvailableChannel"
ERR_NOT_SUPPORTED = "notSupported"
ERR_PIN_INCORRECT = "pinIncorrect"
ERR_PROTOCOL_ERROR = "protocolError"
ERR_TOO_MANY_FAILED_ATTEMPTS = "tooManyFailedAttempts"
ERR_UNKNOWN_ERROR = "unknownError"
ERR_UNSUPPORTED_INPUT = "unsupportedInput"
ERR_USER_CANCELLED = "userCancelled"
ERR_VALUE_OUT_OF_RANGE = "valueOutOfRange"

# Event types
EVENT_COMMAND_RECEIVED = "google_assistant_command"
EVENT_QUERY_RECEIVED = "google_assistant_query"
EVENT_SYNC_RECEIVED = "google_assistant_sync"

DOMAIN_TO_GOOGLE_TYPES = {
    ALARM_CONTROL_PANEL_DOMAIN: TYPE_ALARM,
    BINARY_SENSOR_DOMAIN: TYPE_SENSOR,
    BUTTON_DOMAIN: TYPE_SCENE,
    CAMERA_DOMAIN: TYPE_CAMERA,
    CLIMATE_DOMAIN: TYPE_THERMOSTAT,
    COVER_DOMAIN: TYPE_BLINDS,
    FAN_DOMAIN: TYPE_FAN,
    GROUP_DOMAIN: TYPE_SWITCH,
    HUMIDIFIER_DOMAIN: TYPE_HUMIDIFIER,
    INPUT_BOOLEAN_DOMAIN: TYPE_SWITCH,
    INPUT_BUTTON_DOMAIN: TYPE_SCENE,
    INPUT_SELECT_DOMAIN: TYPE_SENSOR,
    LAWN_MOWER_DOMAIN: TYPE_MOWER,
    LIGHT_DOMAIN: TYPE_LIGHT,
    LOCK_DOMAIN: TYPE_LOCK,
    MEDIA_PLAYER_DOMAIN: TYPE_SETTOP,
    SCENE_DOMAIN: TYPE_SCENE,
    SCRIPT_DOMAIN: TYPE_SCENE,
    SELECT_DOMAIN: TYPE_SENSOR,
    SENSOR_DOMAIN: TYPE_SENSOR,
    SWITCH_DOMAIN: TYPE_SWITCH,
    VACUUM_DOMAIN: TYPE_VACUUM,
    VALVE_DOMAIN: TYPE_VALVE,
    WATER_HEATER_DOMAIN: TYPE_WATERHEATER,
}

DEVICE_CLASS_TO_GOOGLE_TYPES = {
    (BINARY_SENSOR_DOMAIN, binary_sensor.BinarySensorDeviceClass.DOOR): TYPE_DOOR,
    (BINARY_SENSOR_DOMAIN, binary_sensor.BinarySensorDeviceClass.LOCK): TYPE_SENSOR,
    (BINARY_SENSOR_DOMAIN, binary_sensor.BinarySensorDeviceClass.OPENING): TYPE_SENSOR,
    (BINARY_SENSOR_DOMAIN, binary_sensor.BinarySensorDeviceClass.WINDOW): TYPE_WINDOW,
    (
        BINARY_SENSOR_DOMAIN,
        binary_sensor.BinarySensorDeviceClass.GARAGE_DOOR,
    ): TYPE_GARAGE,
    (
        BINARY_SENSOR_DOMAIN,
        binary_sensor.BinarySensorDeviceClass.SMOKE,
    ): TYPE_SMOKE_DETECTOR,
    (
        BINARY_SENSOR_DOMAIN,
        binary_sensor.BinarySensorDeviceClass.CO,
    ): TYPE_CARBON_MONOXIDE_DETECTOR,
    (COVER_DOMAIN, cover.CoverDeviceClass.AWNING): TYPE_AWNING,
    (COVER_DOMAIN, cover.CoverDeviceClass.CURTAIN): TYPE_CURTAIN,
    (COVER_DOMAIN, cover.CoverDeviceClass.DOOR): TYPE_DOOR,
    (COVER_DOMAIN, cover.CoverDeviceClass.GARAGE): TYPE_GARAGE,
    (COVER_DOMAIN, cover.CoverDeviceClass.GATE): TYPE_GATE,
    (COVER_DOMAIN, cover.CoverDeviceClass.SHUTTER): TYPE_SHUTTER,
    (COVER_DOMAIN, cover.CoverDeviceClass.WINDOW): TYPE_WINDOW,
    (EVENT_DOMAIN, event.EventDeviceClass.DOORBELL): TYPE_DOORBELL,
    (
        HUMIDIFIER_DOMAIN,
        humidifier.HumidifierDeviceClass.DEHUMIDIFIER,
    ): TYPE_DEHUMIDIFIER,
    (HUMIDIFIER_DOMAIN, humidifier.HumidifierDeviceClass.HUMIDIFIER): TYPE_HUMIDIFIER,
    (MEDIA_PLAYER_DOMAIN, media_player.MediaPlayerDeviceClass.RECEIVER): TYPE_RECEIVER,
    (MEDIA_PLAYER_DOMAIN, media_player.MediaPlayerDeviceClass.SPEAKER): TYPE_SPEAKER,
    (MEDIA_PLAYER_DOMAIN, media_player.MediaPlayerDeviceClass.TV): TYPE_TV,
    (MEDIA_PLAYER_DOMAIN, media_player.MediaPlayerDeviceClass.PROJECTOR): TYPE_TV,
    (SENSOR_DOMAIN, sensor.SensorDeviceClass.AQI): TYPE_SENSOR,
    (SENSOR_DOMAIN, sensor.SensorDeviceClass.HUMIDITY): TYPE_SENSOR,
    (SENSOR_DOMAIN, sensor.SensorDeviceClass.TEMPERATURE): TYPE_SENSOR,
    (SWITCH_DOMAIN, switch.SwitchDeviceClass.OUTLET): TYPE_OUTLET,
    (SWITCH_DOMAIN, switch.SwitchDeviceClass.SWITCH): TYPE_SWITCH,
}

CHALLENGE_ACK_NEEDED = "ackNeeded"
CHALLENGE_FAILED_PIN_NEEDED = "challengeFailedPinNeeded"
CHALLENGE_PIN_NEEDED = "pinNeeded"

STORE_AGENT_USER_IDS = "agent_user_ids"
STORE_GOOGLE_LOCAL_WEBHOOK_ID = "local_webhook_id"

SOURCE_CLOUD = "cloud"
SOURCE_LOCAL = "local"

NOT_EXPOSE_LOCAL = {TYPE_ALARM, TYPE_LOCK, TYPE_THERMOSTAT}

FAN_SPEEDS = {
    "5/5": ["High", "Max", "Fast", "5"],
    "4/5": ["Medium High", "4"],
    "3/5": ["Medium", "3"],
    "2/5": ["Medium Low", "2"],
    "1/5": ["Low", "Min", "Slow", "1"],
    "4/4": ["High", "Max", "Fast", "4"],
    "3/4": ["Medium High", "3"],
    "2/4": ["Medium Low", "2"],
    "1/4": ["Low", "Min", "Slow", "1"],
    "3/3": ["High", "Max", "Fast", "3"],
    "2/3": ["Medium", "2"],
    "1/3": ["Low", "Min", "Slow", "1"],
    "2/2": ["High", "Max", "Fast", "2"],
    "1/2": ["Low", "Min", "Slow", "1"],
    "1/1": ["Normal", "1"],
}
