From a57d7717a88d7a53ff8c8e45fabc29bd3e2721e5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Hjelseth=20H=C3=B8yer?= Date: Sun, 19 Sep 2021 08:38:43 +0200 Subject: [PATCH] Improve Surepetcare set_pet_location service (#56401) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Surepetcare, improve set_pet_location Signed-off-by: Daniel Hjelseth Høyer * Surepetcare, improve set_pet_location Signed-off-by: Daniel Hjelseth Høyer * Surepetcare, improve set_pet_location Signed-off-by: Daniel Hjelseth Høyer * Surepetcare, improve set_pet_location Signed-off-by: Daniel Hjelseth Høyer * Surepetcare, improve set_pet_location Signed-off-by: Daniel Hjelseth Høyer --- .../components/surepetcare/__init__.py | 21 +++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/homeassistant/components/surepetcare/__init__.py b/homeassistant/components/surepetcare/__init__.py index 1efcaa6b5c1..ece42d0f410 100644 --- a/homeassistant/components/surepetcare/__init__.py +++ b/homeassistant/components/surepetcare/__init__.py @@ -131,7 +131,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool: set_pet_location_schema = vol.Schema( { - vol.Optional(ATTR_PET_NAME): vol.In(coordinator.get_pets().values()), + vol.Required(ATTR_PET_NAME): vol.In(coordinator.get_pets().keys()), vol.Required(ATTR_LOCATION): vol.In( [ Location.INSIDE.name.title(), @@ -198,23 +198,18 @@ class SurePetcareDataCoordinator(DataUpdateCoordinator): await self.lock_states[state](flap_id) await self.async_request_refresh() - def get_pets(self) -> dict[int, str]: + def get_pets(self) -> dict[str, int]: """Get pets.""" - names = {} + pets = {} for surepy_entity in self.data.values(): if surepy_entity.type == EntityType.PET and surepy_entity.name: - names[surepy_entity.id] = surepy_entity.name - return names + pets[surepy_entity.name] = surepy_entity.id + return pets async def handle_set_pet_location(self, call: ServiceCall) -> None: """Call when setting the pet location.""" pet_name = call.data[ATTR_PET_NAME] location = call.data[ATTR_LOCATION] - for device_id, device_name in self.get_pets().items(): - if pet_name == device_name: - await self.surepy.sac.set_pet_location( - device_id, Location[location.upper()] - ) - await self.async_request_refresh() - return - _LOGGER.error("Unknown pet %s", pet_name) + device_id = self.get_pets()[pet_name] + await self.surepy.sac.set_pet_location(device_id, Location[location.upper()]) + await self.async_request_refresh()