Home Assistant Unofficial Reference 2024.12.1
homeassistant.core.HomeAssistant Class Reference

Public Member Functions

None __init__ (self, str config_dir)
 
Self __new__ (cls, str config_dir)
 
str __repr__ (self)
 
CALLBACK_TYPE async_add_shutdown_job (self, HassJob[..., Coroutine[Any, Any, Any]] hassjob, *Any args)
 
CALLBACK_TYPE async_add_shutdown_job (self, HassJob[..., Coroutine[Any, Any, Any]|Any] hassjob, *Any args)
 
CALLBACK_TYPE async_add_shutdown_job (self, HassJob[..., Coroutine[Any, Any, Any]|Any] hassjob, *Any args)
 
None async_block_till_done (self, bool wait_background_tasks=False)
 
int async_run (self, *bool attach_signals=True)
 
None async_start (self)
 
None async_stop (self, int exit_code=0, *bool force=False)
 
None block_till_done (self, bool wait_background_tasks=False)
 
None create_task (self, Coroutine[Any, Any, Any] target, str|None name=None)
 
bool is_running (self)
 
bool is_stopping (self)
 
None set_state (self, CoreState state)
 
int start (self)
 
None stop (self)
 
None verify_event_loop_thread (self, str what)
 

Public Attributes

 bus
 
 components
 
 config
 
 data
 
 exit_code
 
 helpers
 
 import_executor
 
 loop
 
 loop_thread_id
 
 services
 
 state
 
 states
 

Static Public Attributes

 ConfigEntries
 
 HomeAssistantHTTP
 

Private Member Functions

set[asyncio.Future[Any]] _active_tasks (self)
 
None _async_log_running_tasks (self, str stage)
 
None _await_and_log_pending (self, Collection[asyncio.Future[Any]] pending)
 
None _cancel_cancellable_timers (self)
 
asyncio.Future[_R]|None _R (self, Coroutine[Any, Any, _R] target, *Any args)
 
asyncio.Future[_R]|None _R (self, Coroutine[Any, Any, _R] target, *Any args, bool eager_start=False)
 
asyncio.Task[_R] _R (self, Coroutine[Any, Any, _R] target, str name, bool eager_start=True)
 
asyncio.Task[_R] _R (self, Coroutine[Any, Any, _R] target, str|None name=None, bool eager_start=True)
 
asyncio.Task[_R] _R (self, Coroutine[Any, Any, _R] target, str|None name=None, bool eager_start=True)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]] hassjob, *Any args, bool eager_start=False, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool eager_start=False, bool background=False)
 
asyncio.Future[_R]|None _R (self, HassJob[..., Coroutine[Any, Any, _R]|_R] hassjob, *Any args, bool eager_start=False, bool background=False)
 
asyncio.Future[_T] _T (self, Callable[[*_Ts], _T] target, **_Ts args)
 
asyncio.Future[_T] _T (self, Callable[[*_Ts], _T] target, **_Ts args)
 
None _Ts (self, Callable[[*_Ts], Any]|Coroutine[Any, Any, Any] target, **_Ts args)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]] target, **_Ts args)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]] target, **_Ts args, bool eager_start=False)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]|_R] target, **_Ts args)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]|_R] target, **_Ts args, bool eager_start=False)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]|_R]|Coroutine[Any, Any, _R] target, **_Ts args)
 
asyncio.Future[_R]|None _Ts (self, Callable[[*_Ts], Coroutine[Any, Any, _R]|_R]|Coroutine[Any, Any, _R] target, **_Ts args, bool eager_start=False)
 

Private Attributes

 _stop_future
 
 _stopped
 
 _tasks
 

Detailed Description

Root object of the Home Assistant home automation.

Definition at line 418 of file core.py.

Constructor & Destructor Documentation

◆ __init__()

None homeassistant.core.HomeAssistant.__init__ (   self,
str  config_dir 
)
Initialize new Home Assistant object.

Definition at line 435 of file core.py.

Member Function Documentation

◆ __new__()

Self homeassistant.core.HomeAssistant.__new__ (   cls,
str  config_dir 
)
Set the _hass thread local data.

Definition at line 425 of file core.py.

◆ __repr__()

str homeassistant.core.HomeAssistant.__repr__ (   self)
Return the representation.

Definition at line 431 of file core.py.

◆ _active_tasks()

set[asyncio.Future[Any]] homeassistant.core.HomeAssistant._active_tasks (   self)
private
Return all active tasks.

This property is used in bootstrap to log all active tasks
so we can identify what is blocking startup.

This property is marked as private to avoid accidental use
as it is not guaranteed to be present in future versions.

Definition at line 477 of file core.py.

◆ _async_log_running_tasks()

None homeassistant.core.HomeAssistant._async_log_running_tasks (   self,
str  stage 
)
private
Log all running tasks.

Definition at line 1250 of file core.py.

◆ _await_and_log_pending()

None homeassistant.core.HomeAssistant._await_and_log_pending (   self,
Collection[asyncio.Future[Any]]   pending 
)
private
Await and log tasks that take a long time.

Definition at line 1041 of file core.py.

◆ _cancel_cancellable_timers()

None homeassistant.core.HomeAssistant._cancel_cancellable_timers (   self)
private
Cancel timer handles marked as cancellable.

Definition at line 1239 of file core.py.

◆ _R() [1/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
Coroutine[Any, Any, _R]  target,
*Any   args 
)
private

Definition at line 968 of file core.py.

◆ _R() [2/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
Coroutine[Any, Any, _R]  target,
*Any  args,
bool   eager_start = False 
)
private

Definition at line 631 of file core.py.

◆ _R() [3/14]

asyncio.Task[_R] homeassistant.core.HomeAssistant._R (   self,
Coroutine[Any, Any, _R]  target,
str  name,
bool   eager_start = True 
)
private
Create a task from within the event loop.

This type of task is for background tasks that usually run for
the lifetime of Home Assistant or an integration's setup.

A background task is different from a normal task:

  - Will not block startup
  - Will be automatically cancelled on shutdown
  - Calls to async_block_till_done will not wait for completion

If you are using this in your integration, use the create task
methods on the config entry instead.

This method must be run in the event loop.

Definition at line 853 of file core.py.

◆ _R() [4/14]

asyncio.Task[_R] homeassistant.core.HomeAssistant._R (   self,
Coroutine[Any, Any, _R]  target,
str | None   name = None,
bool   eager_start = True 
)
private
Create a task from within the event loop.

This method must be run in the event loop. If you are using this in your
integration, use the create task methods on the config entry instead.

target: target to call.

Definition at line 802 of file core.py.

◆ _R() [5/14]

asyncio.Task[_R] homeassistant.core.HomeAssistant._R (   self,
Coroutine[Any, Any, _R]  target,
str | None   name = None,
bool   eager_start = True 
)
private
Create a task from within the event loop, internal use only.

This method is intended to only be used by core internally
and should not be considered a stable API. We will make
breaking changes to this function in the future and it
should not be used in integrations.

This method must be run in the event loop. If you are using this in your
integration, use the create task methods on the config entry instead.

target: target to call.

Definition at line 822 of file core.py.

◆ _R() [6/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R]]  hassjob,
*Any  args,
bool   background = False 
)
private

Definition at line 727 of file core.py.

◆ _R() [7/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R]]  hassjob,
*Any  args,
bool   background = False 
)
private

Definition at line 910 of file core.py.

◆ _R() [8/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R]]  hassjob,
*Any  args,
bool   eager_start = False,
bool   background = False 
)
private

Definition at line 677 of file core.py.

◆ _R() [9/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   background = False 
)
private

Definition at line 736 of file core.py.

◆ _R() [10/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   background = False 
)
private
Add a HassJob from within the event loop.

If eager_start is True, coroutine functions will be scheduled eagerly.
If background is True, the task will created as a background task.

This method must be run in the event loop.
hassjob: HassJob to call.
args: parameters for method to call.

Definition at line 744 of file core.py.

◆ _R() [11/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   background = False 
)
private

Definition at line 919 of file core.py.

◆ _R() [12/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   background = False 
)
private
Run a HassJob from within the event loop.

This method must be run in the event loop.

If background is True, the task will created as a background task.

hassjob: HassJob
args: parameters for method to call.

Definition at line 927 of file core.py.

◆ _R() [13/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   eager_start = False,
bool   background = False 
)
private

Definition at line 687 of file core.py.

◆ _R() [14/14]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._R (   self,
HassJob[..., Coroutine[Any, Any, _R] | _R]  hassjob,
*Any  args,
bool   eager_start = False,
bool   background = False 
)
private
Add a HassJob from within the event loop.

If eager_start is True, coroutine functions will be scheduled eagerly.
If background is True, the task will created as a background task.

This method must be run in the event loop.
hassjob: HassJob to call.
args: parameters for method to call.

Definition at line 696 of file core.py.

◆ _T() [1/2]

asyncio.Future[_T] homeassistant.core.HomeAssistant._T (   self,
Callable[[*_Ts], _T]  target,
**_Ts   args 
)
private
Add an executor job from within the event loop.

Definition at line 885 of file core.py.

◆ _T() [2/2]

asyncio.Future[_T] homeassistant.core.HomeAssistant._T (   self,
Callable[[*_Ts], _T]  target,
**_Ts   args 
)
private
Add an import executor job from within the event loop.

The future returned from this method must be awaited in the event loop.

Definition at line 899 of file core.py.

◆ _Ts() [1/7]

None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Any] | Coroutine[Any, Any, Any]  target,
**_Ts   args 
)
private
Add a job to be executed by the event loop or by an executor.

If the job is either a coroutine or decorated with @callback, it will be
run by the event loop, if not it will be run by an executor.

target: target to call.
args: parameters for method to call.

Definition at line 589 of file core.py.

◆ _Ts() [2/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R]]  target,
**_Ts   args 
)
private

Definition at line 956 of file core.py.

◆ _Ts() [3/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R]]  target,
**_Ts  args,
bool   eager_start = False 
)
private

Definition at line 613 of file core.py.

◆ _Ts() [4/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R] | _R target,
**_Ts   args 
)
private

Definition at line 962 of file core.py.

◆ _Ts() [5/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R] | _R target,
**_Ts  args,
bool   eager_start = False 
)
private

Definition at line 622 of file core.py.

◆ _Ts() [6/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R] | _R] | Coroutine[Any, Any, _R target,
**_Ts  args 
)
private
Run a job from within the event loop.

This method must be run in the event loop.

target: target to call.
args: parameters for method to call.

Definition at line 973 of file core.py.

◆ _Ts() [7/7]

asyncio.Future[_R] | None homeassistant.core.HomeAssistant._Ts (   self,
Callable[[*_Ts], Coroutine[Any, Any, _R] | _R] | Coroutine[Any, Any, _R target,
**_Ts  args,
bool   eager_start = False 
)
private
Add a job to be executed by the event loop or by an executor.

If the job is either a coroutine or decorated with @callback, it will be
run by the event loop, if not it will be run by an executor.

This method must be run in the event loop.

target: target to call.
args: parameters for method to call.

Definition at line 639 of file core.py.

◆ async_add_shutdown_job() [1/3]

CALLBACK_TYPE homeassistant.core.HomeAssistant.async_add_shutdown_job (   self,
HassJob[..., Coroutine[Any, Any, Any]]  hassjob,
*Any   args 
)

Definition at line 1056 of file core.py.

◆ async_add_shutdown_job() [2/3]

CALLBACK_TYPE homeassistant.core.HomeAssistant.async_add_shutdown_job (   self,
HassJob[..., Coroutine[Any, Any, Any] | Any]  hassjob,
*Any   args 
)

Definition at line 1062 of file core.py.

◆ async_add_shutdown_job() [3/3]

CALLBACK_TYPE homeassistant.core.HomeAssistant.async_add_shutdown_job (   self,
HassJob[..., Coroutine[Any, Any, Any] | Any]  hassjob,
*Any   args 
)
Add a HassJob which will be executed on shutdown.

This method must be run in the event loop.

hassjob: HassJob
args: parameters for method to call.

Returns function to remove the job.

Definition at line 1067 of file core.py.

◆ async_block_till_done()

None homeassistant.core.HomeAssistant.async_block_till_done (   self,
bool   wait_background_tasks = False 
)
Block until all pending work is done.

Definition at line 1009 of file core.py.

◆ async_run()

int homeassistant.core.HomeAssistant.async_run (   self,
*bool   attach_signals = True 
)
Home Assistant main entry point.

Start Home Assistant and block until stopped.

This method is a coroutine.

Definition at line 521 of file core.py.

◆ async_start()

None homeassistant.core.HomeAssistant.async_start (   self)
Finalize startup from inside the event loop.

This method is a coroutine.

Definition at line 544 of file core.py.

◆ async_stop()

None homeassistant.core.HomeAssistant.async_stop (   self,
int   exit_code = 0,
*bool   force = False 
)
Stop Home Assistant and shuts down all threads.

The "force" flag commands async_stop to proceed regardless of
Home Assistant's current state. You should not set this flag
unless you're testing.

This method is a coroutine.

Definition at line 1098 of file core.py.

◆ block_till_done()

None homeassistant.core.HomeAssistant.block_till_done (   self,
bool   wait_background_tasks = False 
)
Block until all pending work is done.

Definition at line 1002 of file core.py.

◆ create_task()

None homeassistant.core.HomeAssistant.create_task (   self,
Coroutine[Any, Any, Any]  target,
str | None   name = None 
)
Add task to the executor pool.

target: target to call.

Definition at line 788 of file core.py.

◆ is_running()

bool homeassistant.core.HomeAssistant.is_running (   self)
Return if Home Assistant is running.

Definition at line 489 of file core.py.

◆ is_stopping()

bool homeassistant.core.HomeAssistant.is_stopping (   self)
Return if Home Assistant is stopping.

Definition at line 494 of file core.py.

◆ set_state()

None homeassistant.core.HomeAssistant.set_state (   self,
CoreState  state 
)
Set the current state.

Definition at line 498 of file core.py.

◆ start()

int homeassistant.core.HomeAssistant.start (   self)
Start Home Assistant.

Note: This function is only used for testing.
For regular use, use "await hass.run()".

Definition at line 504 of file core.py.

◆ stop()

None homeassistant.core.HomeAssistant.stop (   self)
Stop Home Assistant and shuts down all threads.

Definition at line 1088 of file core.py.

◆ verify_event_loop_thread()

None homeassistant.core.HomeAssistant.verify_event_loop_thread (   self,
str  what 
)
Report and raise if we are not running in the event loop thread.

Definition at line 468 of file core.py.

Member Data Documentation

◆ _stop_future

homeassistant.core.HomeAssistant._stop_future
private

Definition at line 1094 of file core.py.

◆ _stopped

homeassistant.core.HomeAssistant._stopped
private

Definition at line 532 of file core.py.

◆ _tasks

homeassistant.core.HomeAssistant._tasks
private

Definition at line 1147 of file core.py.

◆ bus

homeassistant.core.HomeAssistant.bus

Definition at line 448 of file core.py.

◆ components

homeassistant.core.HomeAssistant.components

Definition at line 453 of file core.py.

◆ config

homeassistant.core.HomeAssistant.config

Definition at line 451 of file core.py.

◆ ConfigEntries

homeassistant.core.HomeAssistant.ConfigEntries
static

Definition at line 423 of file core.py.

◆ data

homeassistant.core.HomeAssistant.data

Definition at line 444 of file core.py.

◆ exit_code

homeassistant.core.HomeAssistant.exit_code

Definition at line 1156 of file core.py.

◆ helpers

homeassistant.core.HomeAssistant.helpers

Definition at line 454 of file core.py.

◆ HomeAssistantHTTP

homeassistant.core.HomeAssistant.HomeAssistantHTTP
static

Definition at line 422 of file core.py.

◆ import_executor

homeassistant.core.HomeAssistant.import_executor

Definition at line 463 of file core.py.

◆ loop

homeassistant.core.HomeAssistant.loop

Definition at line 445 of file core.py.

◆ loop_thread_id

homeassistant.core.HomeAssistant.loop_thread_id

Definition at line 466 of file core.py.

◆ services

homeassistant.core.HomeAssistant.services

Definition at line 449 of file core.py.

◆ state

homeassistant.core.HomeAssistant.state

Definition at line 500 of file core.py.

◆ states

homeassistant.core.HomeAssistant.states

Definition at line 450 of file core.py.


The documentation for this class was generated from the following file: