App
App base classes and configuration for building Home Assistant automations.
This module provides clean access to the app framework for creating both async and sync applications with typed configuration.
App
Bases: Generic[AppConfigT], Resource
Base class for applications in the Hassette framework.
This class provides a structure for applications, allowing them to be initialized and managed
within the Hassette ecosystem. Lifecycle will generally be managed for you via the service status events,
which send an event to the Bus and set the status attribute, based on the app's lifecycle.
Source code in src/hassette/app/app.py
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 | |
role: ResourceRole = ResourceRole.APP
class-attribute
Role of the resource, e.g. 'App', 'Service', etc.
app_manifest: AppManifest
class-attribute
Manifest for the app itself, not used by app instances.
app_config_cls: type[AppConfig]
class-attribute
Config class to use for instances of the created app. Configuration from hassette.toml or other sources will be validated by this class.
logger: logging.Logger
instance-attribute
Logger for the instance.
api: Api = cast('Api', self.add_child(api_factory or Api))
instance-attribute
API instance for interacting with Home Assistant.
scheduler: Scheduler = self.add_child(Scheduler)
instance-attribute
Scheduler instance for scheduled tasks owned by this app.
bus: Bus = self.add_child(Bus, priority=0)
instance-attribute
Event bus instance for event handlers owned by this app.
states: StateManager = self.add_child(StateManager)
instance-attribute
States manager instance for accessing Home Assistant states.
app_config: AppConfigT = app_config
instance-attribute
Configuration for this app instance.
index: int = index
instance-attribute
Index of this app instance, used for unique naming.
unique_name: str
property
Unique name for the app instance, used for logging and ownership of resources.
config_log_level: LOG_LEVEL_TYPE
property
Return the log level from the config for this resource.
app_key: str
property
Key for this app in the hassette.toml configuration.
instance_name: str
property
Name for the instance of the app. Used for logging and ownership of resources.
now() -> ZonedDateTime
Return the current date and time.
Source code in src/hassette/app/app.py
130 131 132 | |
cleanup(timeout: int | None = None) -> None
async
Cleanup resources owned by the instance.
This method is called during shutdown to cancel tasks and close caches. Child cleanup (Bus, Scheduler, etc.) is handled by _finalize_shutdown() propagation, not by this method.
Source code in src/hassette/app/app.py
134 135 136 137 138 139 140 141 142 143 | |
AppSync
Bases: App[AppConfigT]
Synchronous adapter for App.
Source code in src/hassette/app/app.py
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | |
before_shutdown() -> None
async
Optional: stop accepting new work, signal loops to wind down, etc.
Source code in src/hassette/app/app.py
149 150 151 152 | |
on_shutdown() -> None
async
Primary hook: release your own stuff (sockets, queues, temp files…).
Source code in src/hassette/app/app.py
154 155 156 157 | |
after_shutdown() -> None
async
Optional: last-chance actions after on_shutdown, before cleanup/STOPPED.
Source code in src/hassette/app/app.py
159 160 161 162 | |
before_initialize() -> None
async
Optional: prepare to accept new work, allocate sockets, queues, temp files, etc.
Source code in src/hassette/app/app.py
164 165 166 167 | |
on_initialize() -> None
async
Primary hook: perform your own initialization (sockets, queues, temp files…).
Source code in src/hassette/app/app.py
169 170 171 172 | |
after_initialize() -> None
async
Optional: finalize initialization, signal readiness, etc.
Source code in src/hassette/app/app.py
174 175 176 177 | |
before_shutdown_sync() -> None
Optional: stop accepting new work, signal loops to wind down, etc.
Source code in src/hassette/app/app.py
179 180 181 | |
on_shutdown_sync() -> None
Primary hook: release your own stuff (sockets, queues, temp files…).
Source code in src/hassette/app/app.py
183 184 185 | |
after_shutdown_sync() -> None
Optional: last-chance actions after on_shutdown, before cleanup/STOPPED.
Source code in src/hassette/app/app.py
187 188 189 | |
before_initialize_sync() -> None
Optional: prepare to accept new work, allocate sockets, queues, temp files, etc.
Source code in src/hassette/app/app.py
191 192 193 | |
on_initialize_sync() -> None
Primary hook: perform your own initialization (sockets, queues, temp files…).
Source code in src/hassette/app/app.py
195 196 197 | |
after_initialize_sync() -> None
Optional: finalize initialization, signal readiness, etc.
Source code in src/hassette/app/app.py
199 200 201 | |
initialize_sync() -> None
Use on_initialize_sync instead.
Source code in src/hassette/app/app.py
203 204 205 206 | |
shutdown_sync() -> None
Use on_shutdown_sync instead.
Source code in src/hassette/app/app.py
208 209 210 211 | |
AppConfig
Bases: BaseSettings
Base configuration class for applications in the Hassette framework.
This default class allows all extras, so arbitrary additional configuration data can be passed without needing to define a custom subclass, at the cost of type safety.
Fields can be set on subclasses and extra can be overridden by assigning a new value to model_config.
Source code in src/hassette/app/app_config.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | |
instance_name: str = ''
class-attribute
instance-attribute
Name for the instance of the app.
log_level: LOG_LEVEL_TYPE = Field(default_factory=log_level_default_factory)
class-attribute
instance-attribute
Log level for the app instance. Defaults to INFO if not provided.
app_key: str = ''
class-attribute
instance-attribute
Configuration-level app key. Reserved: 'hassette' and 'hassette.*' prefixes are rejected.
only_app(app_cls: type[AppT]) -> type[AppT]
Decorator to mark an app class as the only one to run. If more than one app is marked with this decorator, an exception will be raised during initialization.
This is useful for development and testing, where you may want to run only a specific app without modifying configuration files.
Source code in src/hassette/app/app.py
28 29 30 31 32 33 34 35 36 | |