@@ -505,14 +505,16 @@ def _domains_from_yaml_config(yaml_configuration: dict[str, Any]) -> set[str]:
505505DEFAULT_ENTITY_ANALYTICS_CONFIG = EntityAnalyticsModifications ()
506506
507507
508- async def async_devices_payload (hass : HomeAssistant ) -> dict :
508+ async def async_devices_payload (hass : HomeAssistant ) -> dict : # noqa: C901
509509 """Return detailed information about entities and devices."""
510510 dev_reg = dr .async_get (hass )
511511 ent_reg = er .async_get (hass )
512512
513513 integration_inputs : dict [str , tuple [list [str ], list [str ]]] = {}
514514 integration_configs : dict [str , AnalyticsModifications ] = {}
515515
516+ removed_devices : set [str ] = set ()
517+
516518 # Get device list
517519 for device_entry in dev_reg .devices .values ():
518520 if not device_entry .primary_config_entry :
@@ -525,6 +527,10 @@ async def async_devices_payload(hass: HomeAssistant) -> dict:
525527 if config_entry is None :
526528 continue
527529
530+ if device_entry .entry_type is dr .DeviceEntryType .SERVICE :
531+ removed_devices .add (device_entry .id )
532+ continue
533+
528534 integration_domain = config_entry .domain
529535
530536 integration_input = integration_inputs .setdefault (integration_domain , ([], []))
@@ -614,11 +620,12 @@ async def async_devices_payload(hass: HomeAssistant) -> dict:
614620 device_config = integration_config .devices .get (device_id , device_config )
615621
616622 if device_config .remove :
623+ removed_devices .add (device_id )
617624 continue
618625
619626 device_entry = dev_reg .devices [device_id ]
620627
621- device_id_mapping [device_entry . id ] = (integration_domain , len (devices_info ))
628+ device_id_mapping [device_id ] = (integration_domain , len (devices_info ))
622629
623630 devices_info .append (
624631 {
@@ -669,7 +676,7 @@ async def async_devices_payload(hass: HomeAssistant) -> dict:
669676
670677 entity_entry = ent_reg .entities [entity_id ]
671678
672- entity_state = hass .states .get (entity_entry . entity_id )
679+ entity_state = hass .states .get (entity_id )
673680
674681 entity_info = {
675682 # LIMITATION: `assumed_state` can be overridden by users;
@@ -690,15 +697,19 @@ async def async_devices_payload(hass: HomeAssistant) -> dict:
690697 "unit_of_measurement" : entity_entry .unit_of_measurement ,
691698 }
692699
693- if (
694- ((device_id_ := entity_entry .device_id ) is not None )
695- and ((new_device_id := device_id_mapping .get (device_id_ )) is not None )
696- and (new_device_id [0 ] == integration_domain )
697- ):
698- device_info = devices_info [new_device_id [1 ]]
699- device_info ["entities" ].append (entity_info )
700- else :
701- entities_info .append (entity_info )
700+ if (device_id_ := entity_entry .device_id ) is not None :
701+ if device_id_ in removed_devices :
702+ # The device was removed, so we remove the entity too
703+ continue
704+
705+ if (
706+ new_device_id := device_id_mapping .get (device_id_ )
707+ ) is not None and (new_device_id [0 ] == integration_domain ):
708+ device_info = devices_info [new_device_id [1 ]]
709+ device_info ["entities" ].append (entity_info )
710+ continue
711+
712+ entities_info .append (entity_info )
702713
703714 return {
704715 "version" : "home-assistant:1" ,
0 commit comments