Prevent double ZHA channel initialization (#36554)

* Preven double channel initialization.

* Use a setter for setting ZHA device availability.
This commit is contained in:
Alexei Chetroi 2020-06-08 08:54:52 -04:00 committed by GitHub
parent 8ed1b1782e
commit 1bdbe90d2a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 30 additions and 26 deletions

View File

@ -263,9 +263,14 @@ class ZHADevice(LogMixin):
@property
def available(self):
"""Return True if sensor is available."""
"""Return True if device is available."""
return self._available
@available.setter
def available(self, new_availability: bool) -> None:
"""Set device availability."""
self._available = new_availability
@property
def zigbee_signature(self) -> Dict[str, Any]:
"""Get zigbee signature for this device."""
@ -274,10 +279,6 @@ class ZHADevice(LogMixin):
ATTR_ENDPOINTS: self._channels.zigbee_signature,
}
def set_available(self, available):
"""Set availability from restore and prevent signals."""
self._available = available
@classmethod
def new(
cls,

View File

@ -485,16 +485,20 @@ class ZHAGateway:
self, sender, profile, cluster, src_ep, dst_ep, message
):
"""Handle tasks when a device becomes available."""
self.async_update_device(sender)
self.async_update_device(sender, available=True)
@callback
def async_update_device(self, sender: zigpy_dev.Device, available: bool = True):
def async_update_device(
self, sender: zigpy_dev.Device, available: bool = True
) -> None:
"""Update device that has just become available."""
if sender.ieee in self.devices:
device = self.devices[sender.ieee]
# avoid a race condition during new joins
if device.status is DeviceStatus.INITIALIZED:
device.update_available(available)
else:
device.available = available
async def async_update_device_storage(self):
"""Update the devices in the store."""

View File

@ -7,8 +7,7 @@ def apply_application_controller_patch(zha_gateway):
def handle_message(sender, profile, cluster, src_ep, dst_ep, message):
"""Handle message from a device."""
if (
not sender.initializing
and sender.ieee in zha_gateway.devices
sender.ieee in zha_gateway.devices
and not zha_gateway.devices[sender.ieee].available
):
zha_gateway.async_device_became_available(

View File

@ -211,7 +211,7 @@ class ZhaEntity(BaseZhaEntity, RestoreEntity):
if not self.zha_device.is_mains_powered:
# mains powered devices will get real time state
self.async_restore_last_state(last_state)
self._zha_device.set_available(True)
self._zha_device.available = True
async def async_update(self) -> None:
"""Retrieve latest state."""

View File

@ -42,7 +42,7 @@ async def device_switch(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_SWITCH_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -65,7 +65,7 @@ async def device_groupable(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -266,7 +266,7 @@ async def test_list_groupable_devices(zha_client, device_groupable):
# Make sure there are no groupable devices when the device is unavailable
# Make device unavailable
device_groupable.set_available(False)
device_groupable.available = False
await zha_client.send_json({ID: 11, TYPE: "zha/devices/groupable"})

View File

@ -65,7 +65,7 @@ async def coordinator(hass, zigpy_device_mock, zha_device_joined):
node_descriptor=b"\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -83,7 +83,7 @@ async def device_fan_1(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -105,7 +105,7 @@ async def device_fan_2(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE2,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device

View File

@ -58,7 +58,7 @@ async def coordinator(hass, zigpy_device_mock, zha_device_joined):
node_descriptor=b"\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -82,7 +82,7 @@ async def device_light_1(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -106,7 +106,7 @@ async def device_light_2(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE2,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device

View File

@ -88,7 +88,7 @@ async def coordinator(hass, zigpy_device_mock, zha_device_joined):
node_descriptor=b"\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -114,7 +114,7 @@ async def device_light_1(hass, zigpy_device_mock, zha_device_joined):
nwk=0xB79D,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -140,7 +140,7 @@ async def device_light_2(hass, zigpy_device_mock, zha_device_joined):
nwk=0xC79E,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -166,7 +166,7 @@ async def device_light_3(hass, zigpy_device_mock, zha_device_joined):
nwk=0xB89F,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device

View File

@ -56,7 +56,7 @@ async def coordinator(hass, zigpy_device_mock, zha_device_joined):
node_descriptor=b"\xf8\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff",
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -75,7 +75,7 @@ async def device_switch_1(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device
@ -94,7 +94,7 @@ async def device_switch_2(hass, zigpy_device_mock, zha_device_joined):
ieee=IEEE_GROUPABLE_DEVICE2,
)
zha_device = await zha_device_joined(zigpy_device)
zha_device.set_available(True)
zha_device.available = True
return zha_device