Skip to content

Commit ce6bfde

Browse files
authored
Improve typing of floor registry events (home-assistant#157624)
1 parent f00a944 commit ce6bfde

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

homeassistant/helpers/area_registry.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ def _removed_from_registry_filter(
516516
@callback
517517
def _handle_floor_registry_update(event: fr.EventFloorRegistryUpdated) -> None:
518518
"""Update areas that are associated with a floor that has been removed."""
519-
floor_id = event.data.get("floor_id")
520-
if floor_id is None:
521-
return
519+
if TYPE_CHECKING:
520+
assert event.data["action"] == "remove"
521+
floor_id = event.data["floor_id"]
522522
for area in self.areas.get_areas_for_floor(floor_id):
523523
self.async_update(area.id, floor_id=None)
524524

homeassistant/helpers/floor_registry.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,23 @@ class FloorRegistryStoreData(TypedDict):
5151
floors: list[_FloorStoreData]
5252

5353

54-
class EventFloorRegistryUpdatedData(TypedDict):
54+
class _EventFloorRegistryUpdatedData_Create_Remove_Update(TypedDict):
5555
"""Event data for when the floor registry is updated."""
5656

57-
action: Literal["create", "remove", "update", "reorder"]
58-
floor_id: str | None
57+
action: Literal["create", "remove", "update"]
58+
floor_id: str
59+
60+
61+
class _EventFloorRegistryUpdatedData_Reorder(TypedDict):
62+
"""Event data for when the floor registry is updated."""
5963

64+
action: Literal["reorder"]
65+
66+
67+
type EventFloorRegistryUpdatedData = (
68+
_EventFloorRegistryUpdatedData_Create_Remove_Update
69+
| _EventFloorRegistryUpdatedData_Reorder
70+
)
6071

6172
type EventFloorRegistryUpdated = Event[EventFloorRegistryUpdatedData]
6273

@@ -200,7 +211,9 @@ def async_create(
200211

201212
self.hass.bus.async_fire_internal(
202213
EVENT_FLOOR_REGISTRY_UPDATED,
203-
EventFloorRegistryUpdatedData(action="create", floor_id=floor_id),
214+
_EventFloorRegistryUpdatedData_Create_Remove_Update(
215+
action="create", floor_id=floor_id
216+
),
204217
)
205218
return floor
206219

@@ -211,7 +224,7 @@ def async_delete(self, floor_id: str) -> None:
211224
del self.floors[floor_id]
212225
self.hass.bus.async_fire_internal(
213226
EVENT_FLOOR_REGISTRY_UPDATED,
214-
EventFloorRegistryUpdatedData(
227+
_EventFloorRegistryUpdatedData_Create_Remove_Update(
215228
action="remove",
216229
floor_id=floor_id,
217230
),
@@ -253,7 +266,7 @@ def async_update(
253266
self.async_schedule_save()
254267
self.hass.bus.async_fire_internal(
255268
EVENT_FLOOR_REGISTRY_UPDATED,
256-
EventFloorRegistryUpdatedData(
269+
_EventFloorRegistryUpdatedData_Create_Remove_Update(
257270
action="update",
258271
floor_id=floor_id,
259272
),
@@ -280,7 +293,7 @@ def async_reorder(self, floor_ids: list[str]) -> None:
280293
self.async_schedule_save()
281294
self.hass.bus.async_fire_internal(
282295
EVENT_FLOOR_REGISTRY_UPDATED,
283-
EventFloorRegistryUpdatedData(action="reorder", floor_id=None),
296+
_EventFloorRegistryUpdatedData_Reorder(action="reorder"),
284297
)
285298

286299
async def async_load(self) -> None:

0 commit comments

Comments
 (0)