5959 area_registry as ar ,
6060 device_registry as dr ,
6161 entity_registry as er ,
62- floor_registry as fr ,
6362 issue_registry as ir ,
6463 location as loc_helper ,
6564)
7978 template_context_manager ,
8079 template_cv ,
8180)
82- from .helpers import raise_no_default
81+ from .helpers import raise_no_default , resolve_area_id
8382from .render_info import RenderInfo , render_info_cv
8483
8584if TYPE_CHECKING :
@@ -1318,112 +1317,14 @@ def issue(hass: HomeAssistant, domain: str, issue_id: str) -> dict[str, Any] | N
13181317 return None
13191318
13201319
1321- def floors (hass : HomeAssistant ) -> Iterable [str | None ]:
1322- """Return all floors."""
1323- floor_registry = fr .async_get (hass )
1324- return [floor .floor_id for floor in floor_registry .async_list_floors ()]
1325-
1326-
1327- def floor_id (hass : HomeAssistant , lookup_value : Any ) -> str | None :
1328- """Get the floor ID from a floor or area name, alias, device id, or entity id."""
1329- floor_registry = fr .async_get (hass )
1330- lookup_str = str (lookup_value )
1331- if floor := floor_registry .async_get_floor_by_name (lookup_str ):
1332- return floor .floor_id
1333- floors_list = floor_registry .async_get_floors_by_alias (lookup_str )
1334- if floors_list :
1335- return floors_list [0 ].floor_id
1336-
1337- if aid := area_id (hass , lookup_value ):
1338- area_reg = ar .async_get (hass )
1339- if area := area_reg .async_get_area (aid ):
1340- return area .floor_id
1341-
1342- return None
1343-
1344-
1345- def floor_name (hass : HomeAssistant , lookup_value : str ) -> str | None :
1346- """Get the floor name from a floor id."""
1347- floor_registry = fr .async_get (hass )
1348- if floor := floor_registry .async_get_floor (lookup_value ):
1349- return floor .name
1350-
1351- if aid := area_id (hass , lookup_value ):
1352- area_reg = ar .async_get (hass )
1353- if (
1354- (area := area_reg .async_get_area (aid ))
1355- and area .floor_id
1356- and (floor := floor_registry .async_get_floor (area .floor_id ))
1357- ):
1358- return floor .name
1359-
1360- return None
1361-
1362-
1363- def floor_areas (hass : HomeAssistant , floor_id_or_name : str ) -> Iterable [str ]:
1364- """Return area IDs for a given floor ID or name."""
1365- _floor_id : str | None
1366- # If floor_name returns a value, we know the input was an ID, otherwise we
1367- # assume it's a name, and if it's neither, we return early
1368- if floor_name (hass , floor_id_or_name ) is not None :
1369- _floor_id = floor_id_or_name
1370- else :
1371- _floor_id = floor_id (hass , floor_id_or_name )
1372- if _floor_id is None :
1373- return []
1374-
1375- area_reg = ar .async_get (hass )
1376- entries = ar .async_entries_for_floor (area_reg , _floor_id )
1377- return [entry .id for entry in entries if entry .id ]
1378-
1379-
1380- def floor_entities (hass : HomeAssistant , floor_id_or_name : str ) -> Iterable [str ]:
1381- """Return entity_ids for a given floor ID or name."""
1382- return [
1383- entity_id
1384- for area_id in floor_areas (hass , floor_id_or_name )
1385- for entity_id in area_entities (hass , area_id )
1386- ]
1387-
1388-
13891320def areas (hass : HomeAssistant ) -> Iterable [str | None ]:
13901321 """Return all areas."""
13911322 return list (ar .async_get (hass ).areas )
13921323
13931324
13941325def area_id (hass : HomeAssistant , lookup_value : str ) -> str | None :
13951326 """Get the area ID from an area name, alias, device id, or entity id."""
1396- area_reg = ar .async_get (hass )
1397- lookup_str = str (lookup_value )
1398- if area := area_reg .async_get_area_by_name (lookup_str ):
1399- return area .id
1400- areas_list = area_reg .async_get_areas_by_alias (lookup_str )
1401- if areas_list :
1402- return areas_list [0 ].id
1403-
1404- ent_reg = er .async_get (hass )
1405- dev_reg = dr .async_get (hass )
1406- # Import here, not at top-level to avoid circular import
1407- from homeassistant .helpers import config_validation as cv # noqa: PLC0415
1408-
1409- try :
1410- cv .entity_id (lookup_value )
1411- except vol .Invalid :
1412- pass
1413- else :
1414- if entity := ent_reg .async_get (lookup_value ):
1415- # If entity has an area ID, return that
1416- if entity .area_id :
1417- return entity .area_id
1418- # If entity has a device ID, return the area ID for the device
1419- if entity .device_id and (device := dev_reg .async_get (entity .device_id )):
1420- return device .area_id
1421-
1422- # Check if this could be a device ID
1423- if device := dev_reg .async_get (lookup_value ):
1424- return device .area_id
1425-
1426- return None
1327+ return resolve_area_id (hass , lookup_value )
14271328
14281329
14291330def _get_area_name (area_reg : ar .AreaRegistry , valid_area_id : str ) -> str :
@@ -2359,6 +2260,7 @@ def __init__(
23592260 "homeassistant.helpers.template.extensions.CollectionExtension"
23602261 )
23612262 self .add_extension ("homeassistant.helpers.template.extensions.CryptoExtension" )
2263+ self .add_extension ("homeassistant.helpers.template.extensions.FloorExtension" )
23622264 self .add_extension ("homeassistant.helpers.template.extensions.LabelExtension" )
23632265 self .add_extension ("homeassistant.helpers.template.extensions.MathExtension" )
23642266 self .add_extension ("homeassistant.helpers.template.extensions.RegexExtension" )
@@ -2462,23 +2364,6 @@ def wrapper(_: Any, *args: _P.args, **kwargs: _P.kwargs) -> _R:
24622364 self .globals ["area_devices" ] = hassfunction (area_devices )
24632365 self .filters ["area_devices" ] = self .globals ["area_devices" ]
24642366
2465- # Floor extensions
2466-
2467- self .globals ["floors" ] = hassfunction (floors )
2468- self .filters ["floors" ] = self .globals ["floors" ]
2469-
2470- self .globals ["floor_id" ] = hassfunction (floor_id )
2471- self .filters ["floor_id" ] = self .globals ["floor_id" ]
2472-
2473- self .globals ["floor_name" ] = hassfunction (floor_name )
2474- self .filters ["floor_name" ] = self .globals ["floor_name" ]
2475-
2476- self .globals ["floor_areas" ] = hassfunction (floor_areas )
2477- self .filters ["floor_areas" ] = self .globals ["floor_areas" ]
2478-
2479- self .globals ["floor_entities" ] = hassfunction (floor_entities )
2480- self .filters ["floor_entities" ] = self .globals ["floor_entities" ]
2481-
24822367 # Integration extensions
24832368
24842369 self .globals ["integration_entities" ] = hassfunction (integration_entities )
@@ -2534,8 +2419,6 @@ def warn_unsupported(*args: Any, **kwargs: Any) -> NoReturn:
25342419 "device_id" ,
25352420 "distance" ,
25362421 "expand" ,
2537- "floor_id" ,
2538- "floor_name" ,
25392422 "has_value" ,
25402423 "is_device_attr" ,
25412424 "is_hidden_entity" ,
@@ -2557,8 +2440,6 @@ def warn_unsupported(*args: Any, **kwargs: Any) -> NoReturn:
25572440 "closest" ,
25582441 "device_id" ,
25592442 "expand" ,
2560- "floor_id" ,
2561- "floor_name" ,
25622443 "has_value" ,
25632444 ]
25642445 hass_tests = [
0 commit comments