From 99cdc199de8fa28801fe3c8c2601512995a577b8 Mon Sep 17 00:00:00 2001 From: Erik Montnemery Date: Wed, 24 Aug 2022 08:21:11 +0200 Subject: [PATCH] Add blog post about core.async_get_hass() (#1443) --- blog/2022-08-24-globally_accessible_hass.md | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 blog/2022-08-24-globally_accessible_hass.md diff --git a/blog/2022-08-24-globally_accessible_hass.md b/blog/2022-08-24-globally_accessible_hass.md new file mode 100644 index 00000000..dcb193b5 --- /dev/null +++ b/blog/2022-08-24-globally_accessible_hass.md @@ -0,0 +1,20 @@ +--- +author: Erik Montnemery +authorURL: https://github.com/emontnemery +title: "Globally available HomeAssistant object (hass)" +--- + +It's now possible to get a reference to the `HomeAssistant` instance by calling `core.async_get_hass()`. + +Although this means it's no longer strictly necessary to pass `hass` around, the recommendation is still to only use `core.async_get_hass` where it's very cumbersome or downright impossible to pass `hass` to the code which needs it. +An example where this can be useful is voluptuous validators, which previously couldn't access `hass` because voluptuous has no way of passing user data to validators. + +```python +@callback +def async_get_hass() -> HomeAssistant: + """Return the HomeAssistant instance. + Raises LookupError if no HomeAssistant instance is available. + This should be used where it's very cumbersome or downright impossible to pass + hass to the code which needs it. + """ +```