Is there a way to retrieve the current device twin state on reprovisioning with the IoT Hub Device SDK in python?
Your question is not accuracy. There is not other ways to retrieve the current device twin state, only the REST API Service - Get Twin
and its related Python API iothub_twin_method.get_twin
.
The device twin included in device state data on reprovisioning is only depended on the choice of Reprovisioning policies
, as below.
Re-provision and migrate data: This policy is the default for new enrollment entries. This policy takes action when devices associated with the enrollment entry submit a new request (1). Depending on the enrollment entry configuration, the device may be reassigned to another IoT hub. If the device is changing IoT hubs, the device registration with the initial IoT hub will be removed. The updated device state information from that initial IoT hub will be migrated over to the new IoT hub (2). During migration, the device's status will be reported as Assigning.
Re-provision and reset to initial config: This policy takes action when devices associated with the enrollment entry submit a new provisioning request (1). Depending on the enrollment entry configuration, the device may be reassigned to another IoT hub. If the device is changing IoT hubs, the device registration with the initial IoT hub will be removed. The initial configuration data that the provisioning service instance received when the device was provisioned is provided to the new IoT hub (2). During migration, the device's status will be reported as Assigning.
This policy is often used for a factory reset without changing IoT hubs.
Never re-provision: The device is never reassigned to a different hub. This policy is provided for managing backwards compatibility.
Using Azure IoT SDK for Python, the reprovisioning policy is set by the parameters update_hub_assignment
and migrate_device_data
of ReprovisionPolicy
object, please see the explaination below.
The behavior of the service when a device is re-provisioned to an IoT hub.
All required parameters must be populated in order to send to Azure.
:param update_hub_assignment: Required. When set to true (default), the
Device Provisioning Service will evaluate the device's IoT Hub assignment
and update it if necessary for any provisioning requests beyond the first
from a given device. If set to false, the device will stay assigned to its
current IoT hub. Default value: True .
:type update_hub_assignment: bool
:param migrate_device_data: Required. When set to true (default), the
Device Provisioning Service will migrate the device's data (twin, device
capabilities, and device ID) from one IoT hub to another during an IoT hub
assignment update. If set to false, the Device Provisioning Service will
reset the device's data to the initial desired configuration stored in the
corresponding enrollment list. Default value: True .
:type migrate_device_data: bool
And the ReprovisionPolicy
object is as a property initialized in the EnrollmentGroup
or IndividualEnrollment
object.
So please try to change the combination of parameters update_hub_assignment
and migrate_device_data
of ReprovisionPolicy
to realize your needs. Otherwise, your workaround to cache the historial data is the only solution.