Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def _group_add_mock(ep, status=ZCLStatus.SUCCESS, no_groups_cluster=False):
async def mock_req(*args, **kwargs):
return [status, mock.sentinel.group_id]
if not no_groups_cluster:
ep.add_input_cluster(4)
ep.request = mock.MagicMock(side_effect=mock_req)
ep.device.application.groups = mock.MagicMock(spec_set=group.Groups)
return ep
bar: t.uint8_t = t.StructField(requires=lambda s: s.status == Status.SUCCESS)
baz1: t.uint8_t
async def test_ota_handle_image_block(ota_cluster):
ota_cluster.image_block_response = mock.CoroutineMock()
await _ota_image_block(ota_cluster, has_image=True, correct_version=True)
assert ota_cluster.image_block_response.call_count == 1
assert (
ota_cluster.image_block_response.call_args[0][0]
== zcl.foundation.Status.SUCCESS
)
assert (
ota_cluster.image_block_response.call_args[0][1]
== mock.sentinel.manufacturer_id
)
assert ota_cluster.image_block_response.call_args[0][2] == mock.sentinel.image_type
assert (
ota_cluster.image_block_response.call_args[0][3] == mock.sentinel.image_version
)
assert ota_cluster.image_block_response.call_args[0][4] == IMAGE_OFFSET
assert ota_cluster.image_block_response.call_args[0][5] == mock.sentinel.data
ota_cluster.image_block_response.reset_mock()
await _ota_image_block(ota_cluster, has_image=True, correct_version=False)
assert ota_cluster.image_block_response.call_count == 1
assert (
if expect_reply and self.node_desc.is_end_device in (True, None):
self.debug("Extending timeout for 0x%02x request", sequence)
timeout = APS_REPLY_TIMEOUT_EXTENDED
with self._pending.new(sequence) as req:
result, msg = await self._application.request(
self,
profile,
cluster,
src_ep,
dst_ep,
sequence,
data,
expect_reply=expect_reply,
use_ieee=use_ieee,
)
if result != foundation.Status.SUCCESS:
self.debug(
(
"Delivery error for seq # 0x%02x, on endpoint id %s "
"cluster 0x%04x: %s"
),
sequence,
dst_ep,
cluster,
msg,
)
raise zigpy.exceptions.DeliveryError(
"[0x{:04x}:{}:0x{:04x}]: Message send failure".format(
self.nwk, dst_ep, cluster
)
)
# If application.request raises an exception, we won't get here, so
def serialize(self):
failed = [record for record in self if record.status != Status.SUCCESS]
if failed:
return b"".join(
[WriteAttributesStatusRecord(i).serialize() for i in failed]
)
return Status.SUCCESS.serialize()
if img is not None:
should_update = img.should_update(
manufacturer_id, image_type, current_file_version, hardware_version
)
self.debug(
"OTA image version: %s, size: %s. Update needed: %s",
img.version,
img.header.image_size,
should_update,
)
if should_update:
self.info(
"Updating: %s %s", self.endpoint.manufacturer, self.endpoint.model
)
await self.query_next_image_response(
foundation.Status.SUCCESS,
img.key.manufacturer_id,
img.key.image_type,
img.version,
img.header.image_size,
tsn=tsn,
)
return
else:
self.debug("No OTA image is available")
await self.query_next_image_response(
foundation.Status.NO_IMAGE_AVAILABLE, tsn=tsn
)
def serialize(self):
failed = [record for record in self if record.status != Status.SUCCESS]
if failed:
return b"".join(
[WriteAttributesStatusRecord(i).serialize() for i in failed]
)
return Status.SUCCESS.serialize()
def read_attributes_rsp(self, attributes, manufacturer=None, *, tsn=None):
args = []
for attrid, value in attributes.items():
if isinstance(attrid, str):
attrid = self.attridx[attrid]
a = foundation.ReadAttributeRecord(
attrid, foundation.Status.UNSUPPORTED_ATTRIBUTE, foundation.TypeValue()
)
args.append(a)
if value is None:
continue
try:
a.status = foundation.Status.SUCCESS
python_type = self.attributes[attrid][1]
a.value.type = foundation.DATA_TYPES.pytype_to_datatype_id(python_type)
a.value.value = python_type(value)
except ValueError as e:
a.status = foundation.Status.UNSUPPORTED_ATTRIBUTE
self.error(str(e))
return self._read_attributes_rsp(args, manufacturer=manufacturer, tsn=tsn)
async def add_to_group(self, grp_id: int, name: str = None):
try:
res = await self.groups.add(grp_id, name)
except AttributeError:
self.debug("Cannot add 0x%04x group, no groups cluster", grp_id)
return ZCLStatus.FAILURE
if res[0] not in (ZCLStatus.SUCCESS, ZCLStatus.DUPLICATE_EXISTS):
self.debug("Couldn't add to 0x%04x group: %s", grp_id, res[0])
return res[0]
group = self.device.application.groups.add_group(grp_id, name)
group.add_member(self)
return res[0]
async def wrapper(*args, **kwds):
from zigpy.zcl.foundation import Status
from zigpy.exceptions import DeliveryError
try:
result = await command(*args, **kwds)
_LOGGER.debug("%s: executed command: %s %s %s %s",
listener.unique_id,
command.__name__,
"{}: {}".format("with args", args),
"{}: {}".format("with kwargs", kwds),
"{}: {}".format("and result", result))
if isinstance(result, bool):
return result
return result[1] is Status.SUCCESS
except DeliveryError:
_LOGGER.debug("%s: command failed: %s", listener.unique_id,
command.__name__)
return False
return wrapper