iCloud Binding Communication Error

I am on openHAB 3.4 release version

277 │ Active │  80 │ 3.4.0   │ openHAB Add-ons :: Bundles :: iCloud Binding

and have no issue with 2FA.

Do you have two bridges active?

No, just one.

Well, sometimes more as one bridge is needed, with 3.3 snapshot I got 2 bridges with 15 devices working .

I deleted all things, cleaned cache and started OpenHAB again but my problem still remains when I try to activate the master account of the family share. So I still need some help please.

Do I understand correctly, that even adding one account does not work, because the 2-FA code is rejected?
Thats strange.
The binding stores some authentication info in the jsondb, normally that should not harm, if if detects, that the stored information (e.g. security token) is invalid. But to be safe, could you please delete the corresponding data. It is stored in the jsondb directory and should be named somehting like:
icloud%3Aaccount…json

Please also delete the corresponding backups in the backup/ subdir

Yes

Searching by

sudo find / -name *icloud*json*

I found these files in following directories:

/srv/openhab-userdata/jsondb/
/srv/openhab-userdata/jsondb/backup/
/var/lib/openhab/jsondb/
/var/lib/openhab/jsondb/backup/

Should I delete the files in all these directories?

I’m not sure why they are at two different locations. Maybe there is symlink involved. Personally I would delete them.

/srv is used to export data via samba.

I deleted all files in
/var/lib/openhab/jsondb/
/var/lib/openhab/jsondb/backup/
Afterwards the files were gone in the other directories too.

But unfortunately this wasn’t the solution. My problem still remains.

As a side effect my cpu threads went down from 640 to 570.

Did you

  1. Stop openhab
  2. Delete the files
  3. Start openhab

?

Yes - this procedure

Could you post a DEBUG log here? How many digits has the code you receive from Apple?

What I did is:
Disable thing
Enable it
Allow Apple-ID registration
Enter the 6 digit 2FA code
Save changes

This is the debug


2023-01-10 19:46:22.931 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'icloud:account:a39aeaeff1' changed from UNINITIALIZED (DISABLED) to INITIALIZING
==> /var/log/openhab/openhab.log <==
2023-01-10 19:46:22.937 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initializing ...
2023-01-10 19:46:22.939 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initialized.
==> /var/log/openhab/events.log <==
2023-01-10 19:46:22.940 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'icloud:account:a39aeaeff1' changed from INITIALIZING to UNKNOWN
==> /var/log/openhab/openhab.log <==
2023-01-10 19:46:22.941 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud authentication (AuthState=INITIAL, Thing=icloud:account:a39aeaeff1)...
2023-01-10 19:46:22.944 [DEBUG] [inding.icloud.internal.ICloudService] - Checking session token validity
2023-01-10 19:46:22.948 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://setup.icloud.com/setup/ws/1/validate.
2023-01-10 19:46:23.222 [DEBUG] [inding.icloud.internal.ICloudService] - Token is not valid. Attemping new login.
org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://setup.icloud.com/setup/ws/1/validate failed with 421.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.validateToken(ICloudService.java:190) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.authenticate(ICloudService.java:94) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.checkLogin(ICloudAccountBridgeHandler.java:341) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:167) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$2(ICloudAccountBridgeHandler.java:130) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
2023-01-10 19:46:23.225 [DEBUG] [inding.icloud.internal.ICloudService] - Authenticating as XXXXXXXXXXXXXX...
2023-01-10 19:46:23.227 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/signin?isRememberMeEnabled=true.
2023-01-10 19:46:24.484 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://setup.icloud.com/setup/ws/1/accountLogin.
2023-01-10 19:46:25.564 [WARN ] [l.handler.ICloudAccountBridgeHandler] - iCloud authentication requires 2-FA code. Please provide code configuration for thing 'icloud:account:a39aeaeff1'.
==> /var/log/openhab/events.log <==
2023-01-10 19:46:25.566 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'icloud:account:a39aeaeff1' changed from UNKNOWN to OFFLINE (CONFIGURATION_ERROR): Please provide 2-FA code in thing configuration.
19:46:25.564 [WARN ] [l.handler.ICloudAccountBridgeHandler] - iCloud authentication requires 2-FA code. Please provide code configuration for thing 'icloud:account:a39aeaeff1'.
==> /var/log/openhab/openhab.log <==
2023-01-10 19:46:42.778 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initializing ...
2023-01-10 19:46:42.781 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Starting iCloud 2-FA authentication  AuthState=WAIT_FOR_CODE, Thing=icloud:account:a39aeaeff1)...
2023-01-10 19:46:42.781 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - iCloud bridge handler initialized.
==> /var/log/openhab/events.log <==
2023-01-10 19:46:42.782 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'icloud:account:a39aeaeff1' changed from OFFLINE (CONFIGURATION_ERROR): Please provide 2-FA code in thing configuration. to UNKNOWN
==> /var/log/openhab/openhab.log <==
2023-01-10 19:46:42.784 [DEBUG] [l.handler.ICloudAccountBridgeHandler] - Code is given in thing configuration 'icloud:account:a39aeaeff1'. Trying to validate code...
2023-01-10 19:46:42.787 [DEBUG] [inding.icloud.internal.ICloudSession] - iCloud request POST https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode.
2023-01-10 19:46:43.253 [DEBUG] [inding.icloud.internal.ICloudService] - Code verification failed.
org.openhab.binding.icloud.internal.ICloudApiResponseException: Request https://idmsa.apple.com/appleauth/auth/verify/trusteddevice/securitycode failed with 412.
	at org.openhab.binding.icloud.internal.ICloudSession.request(ICloudSession.java:148) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudSession.post(ICloudSession.java:98) ~[?:?]
	at org.openhab.binding.icloud.internal.ICloudService.validate2faCode(ICloudService.java:246) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.handle2FAAuthentication(ICloudAccountBridgeHandler.java:244) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.callApiWithRetryAndExceptionHandling(ICloudAccountBridgeHandler.java:170) ~[?:?]
	at org.openhab.binding.icloud.internal.handler.ICloudAccountBridgeHandler.lambda$2(ICloudAccountBridgeHandler.java:130) ~[?:?]
	at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
	at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:304) [?:?]
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128) [?:?]
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628) [?:?]
	at java.lang.Thread.run(Thread.java:829) [?:?]
2023-01-10 19:46:43.256 [WARN ] [l.handler.ICloudAccountBridgeHandler] - ICloud token invalid.
==> /var/log/openhab/events.log <==
2023-01-10 19:46:43.258 [INFO ] [ab.event.ThingStatusInfoChangedEvent] - Thing 'icloud:account:a39aeaeff1' changed from UNKNOWN to OFFLINE (CONFIGURATION_ERROR): Invalid 2-FA-code.

For me it looks like there is something different with your iCloud account, with is not supported by the current implementation. I did a few tests with my account. When providing a wrong auth code, I receive HTTP 400. You receive HTTP 421.
The problem is, that I have currently now idea how to support here, since I cannot reproduce this with my iCloud account. Another problem is that there is no documentation available for the iCloud API. The implementation in openhab is based upon an python project for the iCloud API: GitHub - picklepete/pyicloud: A Python + iCloud wrapper to access iPhone and Calendar data.
If possible you could try to login to your account using the pycloud project examples. If this works I could help to analyze what is different there.

This test could help you with that:

import os
import click
import datetime
import time
import sys
from pyicloud import PyiCloudService


print("Setup Time Zone")
time.strftime("%X %x %Z")
os.environ["TZ"] = "America/New_York"


print("Py iCloud Services")
api = PyiCloudService("XXX", "*****")

if api.requires_2fa:
    print("Two-factor authentication required.")
    code = input("Enter the code you received of one of your approved devices: ")
    result = api.validate_2fa_code(code)
    print("Code validation result: %s" % result)

    if not result:
        print("Failed to verify security code")

    if not api.is_trusted_session:
        print("Session is not trusted. Requesting trust...")
        result = api.trust_session()
        print("Session trust result %s" % result)

        if not result:
            print("Failed to request trust. You will likely be prompted for the code again in the coming weeks")
#
# Location
#
print("Location")
print(api.iphone.location())

sys.exit(1)

Many thanks for your support Simon!!
Maybe a dump question but where do I have to store the code part?

The code has nothing to with openhab. You have to download the linked python project from github and create a python file with the mentioned code. This is not a typical enduser task but development related.

Found same error in apple forum. Maybe the hints there help? https://discussions.apple.com/thread/251380063

Is there already a release available which is supported on OH 4.0.x ?

As it was included in 3.4 release, it is also available in 4.0 SNAPSHOTS.