Fix huawei_lte current month up/download sensor error on delete (#100506)

Deleting one of them prematurely deleted the last reset item
subscription that is shared between the two.
This commit is contained in:
Ville Skyttä 2023-09-17 19:11:44 +03:00 committed by GitHub
parent 7aa02b8621
commit 2794ab1782
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 12 additions and 8 deletions

View file

@ -143,9 +143,11 @@ class Router:
url: str
data: dict[str, Any] = field(default_factory=dict, init=False)
subscriptions: dict[str, set[str]] = field(
# Values are lists rather than sets, because the same item may be used by more than
# one thing, such as MonthDuration for CurrentMonth{Download,Upload}.
subscriptions: dict[str, list[str]] = field(
default_factory=lambda: defaultdict(
set, ((x, {"initial_scan"}) for x in ALL_KEYS)
list, ((x, ["initial_scan"]) for x in ALL_KEYS)
),
init=False,
)

View file

@ -65,7 +65,9 @@ class HuaweiLteBaseBinarySensor(HuaweiLteBaseEntityWithDevice, BinarySensorEntit
async def async_added_to_hass(self) -> None:
"""Subscribe to needed data on add."""
await super().async_added_to_hass()
self.router.subscriptions[self.key].add(f"{BINARY_SENSOR_DOMAIN}/{self.item}")
self.router.subscriptions[self.key].append(
f"{BINARY_SENSOR_DOMAIN}/{self.item}"
)
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from needed data on remove."""

View file

@ -90,8 +90,8 @@ async def async_setup_entry(
async_add_entities(known_entities, True)
# Tell parent router to poll hosts list to gather new devices
router.subscriptions[KEY_LAN_HOST_INFO].add(_DEVICE_SCAN)
router.subscriptions[KEY_WLAN_HOST_LIST].add(_DEVICE_SCAN)
router.subscriptions[KEY_LAN_HOST_INFO].append(_DEVICE_SCAN)
router.subscriptions[KEY_WLAN_HOST_LIST].append(_DEVICE_SCAN)
async def _async_maybe_add_new_entities(unique_id: str) -> None:
"""Add new entities if the update signal comes from our router."""

View file

@ -724,9 +724,9 @@ class HuaweiLteSensor(HuaweiLteBaseEntityWithDevice, SensorEntity):
async def async_added_to_hass(self) -> None:
"""Subscribe to needed data on add."""
await super().async_added_to_hass()
self.router.subscriptions[self.key].add(f"{SENSOR_DOMAIN}/{self.item}")
self.router.subscriptions[self.key].append(f"{SENSOR_DOMAIN}/{self.item}")
if self.entity_description.last_reset_item:
self.router.subscriptions[self.key].add(
self.router.subscriptions[self.key].append(
f"{SENSOR_DOMAIN}/{self.entity_description.last_reset_item}"
)

View file

@ -69,7 +69,7 @@ class HuaweiLteBaseSwitch(HuaweiLteBaseEntityWithDevice, SwitchEntity):
async def async_added_to_hass(self) -> None:
"""Subscribe to needed data on add."""
await super().async_added_to_hass()
self.router.subscriptions[self.key].add(f"{SWITCH_DOMAIN}/{self.item}")
self.router.subscriptions[self.key].append(f"{SWITCH_DOMAIN}/{self.item}")
async def async_will_remove_from_hass(self) -> None:
"""Unsubscribe from needed data on remove."""