import asyncio
import threading
from . import migration as migration, statistics as statistics
from .const import DB_WORKER_PREFIX as DB_WORKER_PREFIX, DOMAIN as DOMAIN, KEEPALIVE_TIME as KEEPALIVE_TIME, MARIADB_PYMYSQL_URL_PREFIX as MARIADB_PYMYSQL_URL_PREFIX, MARIADB_URL_PREFIX as MARIADB_URL_PREFIX, MAX_QUEUE_BACKLOG as MAX_QUEUE_BACKLOG, MYSQLDB_PYMYSQL_URL_PREFIX as MYSQLDB_PYMYSQL_URL_PREFIX, MYSQLDB_URL_PREFIX as MYSQLDB_URL_PREFIX, SQLITE_URL_PREFIX as SQLITE_URL_PREFIX, SupportedDialect as SupportedDialect
from .db_schema import Base as Base, EventData as EventData, Events as Events, SCHEMA_VERSION as SCHEMA_VERSION, StateAttributes as StateAttributes, States as States, Statistics as Statistics, StatisticsRuns as StatisticsRuns, StatisticsShortTerm as StatisticsShortTerm
from .executor import DBInterruptibleThreadPoolExecutor as DBInterruptibleThreadPoolExecutor
from .models import StatisticData as StatisticData, StatisticMetaData as StatisticMetaData, UnsupportedDialect as UnsupportedDialect, process_timestamp as process_timestamp
from .pool import MutexPool as MutexPool, POOL_SIZE as POOL_SIZE, RecorderPool as RecorderPool
from .queries import find_shared_attributes_id as find_shared_attributes_id, find_shared_data_id as find_shared_data_id
from .run_history import RunHistory as RunHistory
from .tasks import AdjustStatisticsTask as AdjustStatisticsTask, ChangeStatisticsUnitTask as ChangeStatisticsUnitTask, ClearStatisticsTask as ClearStatisticsTask, CommitTask as CommitTask, DatabaseLockTask as DatabaseLockTask, EventTask as EventTask, ImportStatisticsTask as ImportStatisticsTask, KeepAliveTask as KeepAliveTask, PerodicCleanupTask as PerodicCleanupTask, PurgeTask as PurgeTask, RecorderTask as RecorderTask, StatisticsTask as StatisticsTask, StopTask as StopTask, SynchronizeTask as SynchronizeTask, UpdateStatisticsMetadataTask as UpdateStatisticsMetadataTask, WaitTask as WaitTask
from .util import build_mysqldb_conv as build_mysqldb_conv, dburl_to_path as dburl_to_path, end_incomplete_runs as end_incomplete_runs, is_second_sunday as is_second_sunday, move_away_broken_database as move_away_broken_database, session_scope as session_scope, setup_connection_for_dialect as setup_connection_for_dialect, validate_or_move_away_sqlite_database as validate_or_move_away_sqlite_database, write_lock_db_sqlite as write_lock_db_sqlite
from _typeshed import Incomplete
from awesomeversion import AwesomeVersion as AwesomeVersion
from collections.abc import Callable as Callable, Iterable
from datetime import datetime
from homeassistant.components import persistent_notification as persistent_notification
from homeassistant.const import ATTR_ENTITY_ID as ATTR_ENTITY_ID, EVENT_HOMEASSISTANT_FINAL_WRITE as EVENT_HOMEASSISTANT_FINAL_WRITE, EVENT_HOMEASSISTANT_STOP as EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED as EVENT_STATE_CHANGED, MATCH_ALL as MATCH_ALL
from homeassistant.core import CALLBACK_TYPE as CALLBACK_TYPE, Event as Event, HomeAssistant as HomeAssistant, callback as callback
from homeassistant.helpers.event import async_track_time_change as async_track_time_change, async_track_time_interval as async_track_time_interval, async_track_utc_time_change as async_track_utc_time_change
from homeassistant.helpers.json import JSON_ENCODE_EXCEPTIONS as JSON_ENCODE_EXCEPTIONS
from homeassistant.helpers.start import async_at_started as async_at_started
from homeassistant.helpers.typing import UNDEFINED as UNDEFINED, UndefinedType as UndefinedType
from sqlalchemy.engine import Engine as Engine
from sqlalchemy.orm.session import Session as Session
from typing import Any, TypeVar

_LOGGER: Incomplete
T = TypeVar('T')
DEFAULT_URL: str
EXPIRE_AFTER_COMMITS: int
STATE_ATTRIBUTES_ID_CACHE_SIZE: int
EVENT_DATA_ID_CACHE_SIZE: int
SHUTDOWN_TASK: Incomplete
COMMIT_TASK: Incomplete
KEEP_ALIVE_TASK: Incomplete
WAIT_TASK: Incomplete
DB_LOCK_TIMEOUT: int
DB_LOCK_QUEUE_CHECK_TIMEOUT: int
INVALIDATED_ERR: str
CONNECTIVITY_ERR: str
MAX_DB_EXECUTOR_WORKERS: Incomplete

class Recorder(threading.Thread):
    stop_requested: bool
    hass: Incomplete
    auto_purge: Incomplete
    auto_repack: Incomplete
    keep_days: Incomplete
    _hass_started: Incomplete
    commit_interval: Incomplete
    _queue: Incomplete
    db_url: Incomplete
    db_max_retries: Incomplete
    db_retry_wait: Incomplete
    engine_version: Incomplete
    async_db_connected: Incomplete
    async_db_ready: Incomplete
    async_recorder_ready: Incomplete
    _queue_watch: Incomplete
    engine: Incomplete
    run_history: Incomplete
    entity_filter: Incomplete
    exclude_t: Incomplete
    schema_version: int
    _commits_without_expire: int
    _old_states: Incomplete
    _state_attributes_ids: Incomplete
    _event_data_ids: Incomplete
    _pending_state_attributes: Incomplete
    _pending_event_data: Incomplete
    _pending_expunge: Incomplete
    event_session: Incomplete
    _get_session: Incomplete
    _completed_first_database_setup: Incomplete
    async_migration_event: Incomplete
    migration_in_progress: bool
    migration_is_live: bool
    _database_lock_task: Incomplete
    _db_executor: Incomplete
    _exclude_attributes_by_domain: Incomplete
    _event_listener: Incomplete
    _queue_watcher: Incomplete
    _keep_alive_listener: Incomplete
    _commit_listener: Incomplete
    _periodic_listener: Incomplete
    _nightly_listener: Incomplete
    enabled: bool
    def __init__(self, hass: HomeAssistant, auto_purge: bool, auto_repack: bool, keep_days: int, commit_interval: int, uri: str, db_max_retries: int, db_retry_wait: int, entity_filter: Callable[[str], bool], exclude_t: list[str], exclude_attributes_by_domain: dict[str, set[str]]) -> None: ...
    @property
    def backlog(self) -> int: ...
    @property
    def dialect_name(self) -> Union[SupportedDialect, None]: ...
    @property
    def _using_file_sqlite(self) -> bool: ...
    @property
    def recording(self) -> bool: ...
    def get_session(self) -> Session: ...
    def queue_task(self, task: RecorderTask) -> None: ...
    def set_enable(self, enable: bool) -> None: ...
    def async_start_executor(self) -> None: ...
    def _shutdown_pool(self) -> None: ...
    def async_initialize(self) -> None: ...
    def _async_keep_alive(self, now: datetime) -> None: ...
    def _async_commit(self, now: datetime) -> None: ...
    def async_add_executor_job(self, target: Callable[..., T], *args: Any) -> asyncio.Future[T]: ...
    def _stop_executor(self) -> None: ...
    def _async_check_queue(self, *_: Any) -> None: ...
    def _async_stop_queue_watcher_and_event_listener(self) -> None: ...
    def _async_stop_listeners(self) -> None: ...
    def _async_event_filter(self, event: Event) -> bool: ...
    def _async_empty_queue(self, event: Event) -> None: ...
    async def _async_shutdown(self, event: Event) -> None: ...
    def _async_hass_started(self, hass: HomeAssistant) -> None: ...
    def async_register(self) -> None: ...
    def async_connection_failed(self) -> None: ...
    def async_connection_success(self) -> None: ...
    def async_set_db_ready(self) -> None: ...
    def _async_set_recorder_ready_migration_done(self) -> None: ...
    def async_nightly_tasks(self, now: datetime) -> None: ...
    def async_periodic_statistics(self, now: datetime) -> None: ...
    def async_adjust_statistics(self, statistic_id: str, start_time: datetime, sum_adjustment: float, adjustment_unit: str) -> None: ...
    def async_clear_statistics(self, statistic_ids: list[str]) -> None: ...
    def async_update_statistics_metadata(self, statistic_id: str, *, new_statistic_id: Union[str, UndefinedType] = ..., new_unit_of_measurement: Union[str, None, UndefinedType] = ...) -> None: ...
    def async_change_statistics_unit(self, statistic_id: str, *, new_unit_of_measurement: str, old_unit_of_measurement: str) -> None: ...
    def async_import_statistics(self, metadata: StatisticMetaData, stats: Iterable[StatisticData], table: type[Union[Statistics, StatisticsShortTerm]]) -> None: ...
    def _async_setup_periodic_tasks(self) -> None: ...
    async def _async_wait_for_started(self) -> Union[object, None]: ...
    def _wait_startup_or_shutdown(self) -> Union[object, None]: ...
    def run(self) -> None: ...
    def _run_event_loop(self) -> None: ...
    def _process_one_task_or_recover(self, task: RecorderTask) -> None: ...
    def _setup_recorder(self) -> bool: ...
    def _async_migration_started(self) -> None: ...
    def _migrate_schema_and_setup_run(self, schema_status: migration.SchemaValidationStatus) -> bool: ...
    def _lock_database(self, task: DatabaseLockTask) -> None: ...
    def _process_one_event(self, event: Event) -> None: ...
    def _find_shared_attr_in_db(self, attr_hash: int, shared_attrs: str) -> Union[int, None]: ...
    def _find_shared_data_in_db(self, data_hash: int, shared_data: str) -> Union[int, None]: ...
    def _process_non_state_changed_event_into_session(self, event: Event) -> None: ...
    def _process_state_changed_event_into_session(self, event: Event) -> None: ...
    def _handle_database_error(self, err: Exception) -> bool: ...
    def _event_session_has_pending_writes(self) -> bool: ...
    def _commit_event_session_or_retry(self) -> None: ...
    def _commit_event_session(self) -> None: ...
    def _handle_sqlite_corruption(self) -> None: ...
    def _close_event_session(self) -> None: ...
    def _reopen_event_session(self) -> None: ...
    def _open_event_session(self) -> None: ...
    def _post_schema_migration(self, old_version: int, new_version: int) -> None: ...
    def _send_keep_alive(self) -> None: ...
    def event_listener(self, event: Event) -> None: ...
    async def async_block_till_done(self) -> None: ...
    def block_till_done(self) -> None: ...
    async def lock_database(self) -> bool: ...
    def unlock_database(self) -> bool: ...
    def _setup_connection(self) -> None: ...
    def _close_connection(self) -> None: ...
    def _setup_run(self) -> None: ...
    def _schedule_compile_missing_statistics(self, session: Session) -> None: ...
    def _end_session(self) -> None: ...
    def _shutdown(self) -> None: ...
