azure-iot-sdk-python: Not able to get Twin Patch Handler to work

Context

  • OS and version used: Windows 10
  • Python version: 3.8.6
  • pip version: 21.0.1
  • list of installed packages: N/A (will post if required anyways)
  • cloned repo: N/A

Description of the issue

When trying to work with the module_client.on_twin_desired_properties_patch_received I am expecting my method to be executed when the Twin for my module gets updated and my print call being executed.

⚠ I am utilizing the Simulator to test this! I am not 100% sure this is supported but could not find documentation stating that this is not supported.

The way I test Twin updates:

  1. Go to Portal
  2. Open Device + Module
  3. Edit the Module Identity Twin + Save

OR

  1. Go to VSCode IoTHub
  2. Right Click module -> Edit Module Twin
  3. Edit Twin + Click Update Module Twin in the left top

Code sample exhibiting the issue

import time
import os
import sys
import asyncio
from six.moves import input
import threading
from azure.iot.device.aio import IoTHubModuleClient
from azure.iot.device import Message
import random

async def main():
  try:
    print("IoT Hub Client for Python")

    # The client object is used to interact with your Azure IoT hub.
    module_client = IoTHubModuleClient.create_from_edge_environment()

    # connect the client.
    await module_client.connect()

    async def main_loop(module_client):
      while True:
        print("Waiting for commands")
        await asyncio.sleep(1)

    def twin_patch_handler(patch):
      print("the data in the desired properties patch was: {}".format(patch))
    
    module_client.on_twin_desired_properties_patch_received = twin_patch_handler

    print("The sample is now waiting for messages. ")
    await main_loop(module_client)

    # Finally, disconnect
    await module_client.disconnect()

  except Exception as e:
    print("Unexpected error %s " % e)
    raise

if __name__ == "__main__":
  asyncio.run(main())

Thanks for helping in advance! Let me know if anything else is required from my side.

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 17 (10 by maintainers)

Most upvoted comments

We have been able to reproduce the issue and are now looking for solutions.