|
56 | 56 | ) |
57 | 57 | from homeassistant.exceptions import TemplateError |
58 | 58 | from homeassistant.helpers import ( |
59 | | - area_registry as ar, |
60 | | - device_registry as dr, |
61 | 59 | entity_registry as er, |
62 | 60 | issue_registry as ir, |
63 | 61 | location as loc_helper, |
|
78 | 76 | template_context_manager, |
79 | 77 | template_cv, |
80 | 78 | ) |
81 | | -from .helpers import raise_no_default, resolve_area_id |
| 79 | +from .helpers import raise_no_default |
82 | 80 | from .render_info import RenderInfo, render_info_cv |
83 | 81 |
|
84 | 82 | if TYPE_CHECKING: |
@@ -1244,103 +1242,6 @@ def issue(hass: HomeAssistant, domain: str, issue_id: str) -> dict[str, Any] | N |
1244 | 1242 | return None |
1245 | 1243 |
|
1246 | 1244 |
|
1247 | | -def areas(hass: HomeAssistant) -> Iterable[str | None]: |
1248 | | - """Return all areas.""" |
1249 | | - return list(ar.async_get(hass).areas) |
1250 | | - |
1251 | | - |
1252 | | -def area_id(hass: HomeAssistant, lookup_value: str) -> str | None: |
1253 | | - """Get the area ID from an area name, alias, device id, or entity id.""" |
1254 | | - return resolve_area_id(hass, lookup_value) |
1255 | | - |
1256 | | - |
1257 | | -def _get_area_name(area_reg: ar.AreaRegistry, valid_area_id: str) -> str: |
1258 | | - """Get area name from valid area ID.""" |
1259 | | - area = area_reg.async_get_area(valid_area_id) |
1260 | | - assert area |
1261 | | - return area.name |
1262 | | - |
1263 | | - |
1264 | | -def area_name(hass: HomeAssistant, lookup_value: str) -> str | None: |
1265 | | - """Get the area name from an area id, device id, or entity id.""" |
1266 | | - area_reg = ar.async_get(hass) |
1267 | | - if area := area_reg.async_get_area(lookup_value): |
1268 | | - return area.name |
1269 | | - |
1270 | | - dev_reg = dr.async_get(hass) |
1271 | | - ent_reg = er.async_get(hass) |
1272 | | - # Import here, not at top-level to avoid circular import |
1273 | | - from homeassistant.helpers import config_validation as cv # noqa: PLC0415 |
1274 | | - |
1275 | | - try: |
1276 | | - cv.entity_id(lookup_value) |
1277 | | - except vol.Invalid: |
1278 | | - pass |
1279 | | - else: |
1280 | | - if entity := ent_reg.async_get(lookup_value): |
1281 | | - # If entity has an area ID, get the area name for that |
1282 | | - if entity.area_id: |
1283 | | - return _get_area_name(area_reg, entity.area_id) |
1284 | | - # If entity has a device ID and the device exists with an area ID, get the |
1285 | | - # area name for that |
1286 | | - if ( |
1287 | | - entity.device_id |
1288 | | - and (device := dev_reg.async_get(entity.device_id)) |
1289 | | - and device.area_id |
1290 | | - ): |
1291 | | - return _get_area_name(area_reg, device.area_id) |
1292 | | - |
1293 | | - if (device := dev_reg.async_get(lookup_value)) and device.area_id: |
1294 | | - return _get_area_name(area_reg, device.area_id) |
1295 | | - |
1296 | | - return None |
1297 | | - |
1298 | | - |
1299 | | -def area_entities(hass: HomeAssistant, area_id_or_name: str) -> Iterable[str]: |
1300 | | - """Return entities for a given area ID or name.""" |
1301 | | - _area_id: str | None |
1302 | | - # if area_name returns a value, we know the input was an ID, otherwise we |
1303 | | - # assume it's a name, and if it's neither, we return early |
1304 | | - if area_name(hass, area_id_or_name) is None: |
1305 | | - _area_id = area_id(hass, area_id_or_name) |
1306 | | - else: |
1307 | | - _area_id = area_id_or_name |
1308 | | - if _area_id is None: |
1309 | | - return [] |
1310 | | - ent_reg = er.async_get(hass) |
1311 | | - entity_ids = [ |
1312 | | - entry.entity_id for entry in er.async_entries_for_area(ent_reg, _area_id) |
1313 | | - ] |
1314 | | - dev_reg = dr.async_get(hass) |
1315 | | - # We also need to add entities tied to a device in the area that don't themselves |
1316 | | - # have an area specified since they inherit the area from the device. |
1317 | | - entity_ids.extend( |
1318 | | - [ |
1319 | | - entity.entity_id |
1320 | | - for device in dr.async_entries_for_area(dev_reg, _area_id) |
1321 | | - for entity in er.async_entries_for_device(ent_reg, device.id) |
1322 | | - if entity.area_id is None |
1323 | | - ] |
1324 | | - ) |
1325 | | - return entity_ids |
1326 | | - |
1327 | | - |
1328 | | -def area_devices(hass: HomeAssistant, area_id_or_name: str) -> Iterable[str]: |
1329 | | - """Return device IDs for a given area ID or name.""" |
1330 | | - _area_id: str | None |
1331 | | - # if area_name returns a value, we know the input was an ID, otherwise we |
1332 | | - # assume it's a name, and if it's neither, we return early |
1333 | | - if area_name(hass, area_id_or_name) is not None: |
1334 | | - _area_id = area_id_or_name |
1335 | | - else: |
1336 | | - _area_id = area_id(hass, area_id_or_name) |
1337 | | - if _area_id is None: |
1338 | | - return [] |
1339 | | - dev_reg = dr.async_get(hass) |
1340 | | - entries = dr.async_entries_for_area(dev_reg, _area_id) |
1341 | | - return [entry.id for entry in entries] |
1342 | | - |
1343 | | - |
1344 | 1245 | def closest(hass: HomeAssistant, *args: Any) -> State | None: |
1345 | 1246 | """Find closest entity. |
1346 | 1247 |
|
@@ -2182,6 +2083,7 @@ def __init__( |
2182 | 2083 | ] = weakref.WeakValueDictionary() |
2183 | 2084 | self.add_extension("jinja2.ext.loopcontrols") |
2184 | 2085 | self.add_extension("jinja2.ext.do") |
| 2086 | + self.add_extension("homeassistant.helpers.template.extensions.AreaExtension") |
2185 | 2087 | self.add_extension("homeassistant.helpers.template.extensions.Base64Extension") |
2186 | 2088 | self.add_extension( |
2187 | 2089 | "homeassistant.helpers.template.extensions.CollectionExtension" |
@@ -2276,22 +2178,6 @@ def wrapper(_: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R: |
2276 | 2178 |
|
2277 | 2179 | return jinja_context(wrapper) |
2278 | 2180 |
|
2279 | | - # Area extensions |
2280 | | - |
2281 | | - self.globals["areas"] = hassfunction(areas) |
2282 | | - |
2283 | | - self.globals["area_id"] = hassfunction(area_id) |
2284 | | - self.filters["area_id"] = self.globals["area_id"] |
2285 | | - |
2286 | | - self.globals["area_name"] = hassfunction(area_name) |
2287 | | - self.filters["area_name"] = self.globals["area_name"] |
2288 | | - |
2289 | | - self.globals["area_entities"] = hassfunction(area_entities) |
2290 | | - self.filters["area_entities"] = self.globals["area_entities"] |
2291 | | - |
2292 | | - self.globals["area_devices"] = hassfunction(area_devices) |
2293 | | - self.filters["area_devices"] = self.globals["area_devices"] |
2294 | | - |
2295 | 2181 | # Integration extensions |
2296 | 2182 |
|
2297 | 2183 | self.globals["integration_entities"] = hassfunction(integration_entities) |
|
0 commit comments