From 85aca86838451c1d359350726624a2f869c643b6 Mon Sep 17 00:00:00 2001 From: Maarten Blokker Date: Thu, 9 Sep 2021 21:36:37 +0200 Subject: [PATCH] [surepetcate] fixes #11234 Check for null before updating pet location channels Signed-off-by: Maarten Blokker --- .../handler/SurePetcarePetHandler.java | 29 ++++++++++--------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java index ec1fa5b67f01c..94c82326daf31 100644 --- a/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java +++ b/bundles/org.openhab.binding.surepetcare/src/main/java/org/openhab/binding/surepetcare/internal/handler/SurePetcarePetHandler.java @@ -164,6 +164,21 @@ protected void updateThing() { if (loc.since != null) { updateState(PET_CHANNEL_LOCATION_CHANGED, new DateTimeType(loc.since)); } + + if (loc.deviceId != null) { + SurePetcareDevice device = petcareAPI.getDevice(loc.deviceId.toString()); + if (device != null) { + updateState(PET_CHANNEL_LOCATION_CHANGED_THROUGH, new StringType(device.name)); + } + } else if (loc.userId != null) { + SurePetcareHousehold household = petcareAPI.getHousehold(pet.householdId.toString()); + if (household != null) { + Long userId = loc.userId; + household.users.stream().map(user -> user.user).filter(user -> userId.equals(user.userId)) + .forEach(user -> updateState(PET_CHANNEL_LOCATION_CHANGED_THROUGH, + new StringType(user.userName))); + } + } } updateState(PET_CHANNEL_DATE_OF_BIRTH, pet.dateOfBirth == null ? UnDefType.UNDEF : new DateTimeType(pet.dateOfBirth.atStartOfDay(ZoneId.systemDefault()))); @@ -175,20 +190,6 @@ protected void updateThing() { updateState(PET_CHANNEL_TAG_IDENTIFIER, new StringType(tag.tag)); } } - if (pet.status.activity.deviceId != null) { - SurePetcareDevice device = petcareAPI.getDevice(pet.status.activity.deviceId.toString()); - if (device != null) { - updateState(PET_CHANNEL_LOCATION_CHANGED_THROUGH, new StringType(device.name)); - } - } else if (pet.status.activity.userId != null) { - SurePetcareHousehold household = petcareAPI.getHousehold(pet.householdId.toString()); - if (household != null) { - Long userId = pet.status.activity.userId; - household.users.stream().map(user -> user.user).filter(user -> userId.equals(user.userId)) - .forEach(user -> updateState(PET_CHANNEL_LOCATION_CHANGED_THROUGH, - new StringType(user.userName))); - } - } SurePetcarePetFeeding feeding = pet.status.feeding; if (feeding != null) { SurePetcareDevice device = petcareAPI.getDevice(feeding.deviceId.toString());