def main()

in src/modules/sitewise/lib/patch_sitewise_content.py [0:0]


def main():
    args = parse_args()
    region = args.region
    profile = args.profile
    endpoint_url = args.endpoint_url
    workspace_id = args.workspace_id

    session = boto3.session.Session(profile_name=profile)

    iottwinmaker_client = session.client(service_name='iottwinmaker', endpoint_url=endpoint_url, region_name=region)
    sitewise_client = session.client('iotsitewise', region)

    sitewiseImporter = SiteWiseTelemetryImporter(args.region, asset_model_prefix=workspace_id)
    models = [x for x in sitewiseImporter.get_models(workspace_id) if 'WaterTank' in x['name']]
    print(models)
    assert len(models) == 1
    model_name = models[0]['name']
    model_id = models[0]['assetModelId']
    assets = sitewiseImporter.get_assets_by_model_id(model_id)
    assert len(assets) == 1

    asset_id = assets[0]['assetId']

    try:
        update_entity = iottwinmaker_client.update_entity(
            componentUpdates={
                "WaterTankVolume": {
                    "updateType": "CREATE",
                    "componentTypeId": "com.amazon.iotsitewise.connector",
                    "propertyUpdates": {
                        "sitewiseAssetId": {
                            "updateType": "UPDATE",
                            "value": {
                                "stringValue": asset_id
                            }
                        },
                        "sitewiseAssetModelId": {
                            "updateType": "UPDATE",
                            "value": {
                                "stringValue": model_id
                            }
                        }
                    }
                }
            },
            entityId = 'WaterTank_ab5e8bc0-5c8f-44d8-b0a9-bef9c8d2cfab',
            workspaceId = workspace_id)
        print(update_entity)

        state = update_entity['state']
        while state == 'UPDATING':
            entity_description = iottwinmaker_client.get_entity(entityId='WaterTank_ab5e8bc0-5c8f-44d8-b0a9-bef9c8d2cfab', workspaceId=workspace_id)
            state = entity_description['status']['state']
            print(f'  waiting for update to complete...current entity state: {state}')
            time.sleep(1)
    except Exception as e:
        if 'Component WaterTankVolume in entity WaterTank_ab5e8bc0-5c8f-44d8-b0a9-bef9c8d2cfab in workspace' in str(e):
            print("entity updated")
        else:
            raise e