diff --git a/homeassistant/components/surepetcare/__init__.py b/homeassistant/components/surepetcare/__init__.py index 9aac8f11941..bd951ab2641 100644 --- a/homeassistant/components/surepetcare/__init__.py +++ b/homeassistant/components/surepetcare/__init__.py @@ -3,10 +3,11 @@ import logging from typing import Any, Dict, List from surepy import ( + MESTART_RESOURCE, SurePetcare, SurePetcareAuthenticationError, SurePetcareError, - SureProductID, + SurepyProduct, ) import voluptuous as vol @@ -81,7 +82,7 @@ async def async_setup(hass, config) -> bool: async_get_clientsession(hass), api_timeout=SURE_API_TIMEOUT, ) - await surepy.get_data() + except SurePetcareAuthenticationError: _LOGGER.error("Unable to connect to surepetcare.io: Wrong credentials!") return False @@ -91,14 +92,14 @@ async def async_setup(hass, config) -> bool: # add feeders things = [ - {CONF_ID: feeder, CONF_TYPE: SureProductID.FEEDER} + {CONF_ID: feeder, CONF_TYPE: SurepyProduct.FEEDER} for feeder in conf[CONF_FEEDERS] ] # add flaps (don't differentiate between CAT and PET for now) things.extend( [ - {CONF_ID: flap, CONF_TYPE: SureProductID.PET_FLAP} + {CONF_ID: flap, CONF_TYPE: SurepyProduct.PET_FLAP} for flap in conf[CONF_FLAPS] ] ) @@ -109,20 +110,20 @@ async def async_setup(hass, config) -> bool: device_data = await surepy.device(device[CONF_ID]) if ( CONF_PARENT in device_data - and device_data[CONF_PARENT][CONF_PRODUCT_ID] == SureProductID.HUB + and device_data[CONF_PARENT][CONF_PRODUCT_ID] == SurepyProduct.HUB and device_data[CONF_PARENT][CONF_ID] not in hub_ids ): things.append( { CONF_ID: device_data[CONF_PARENT][CONF_ID], - CONF_TYPE: SureProductID.HUB, + CONF_TYPE: SurepyProduct.HUB, } ) hub_ids.add(device_data[CONF_PARENT][CONF_ID]) # add pets things.extend( - [{CONF_ID: pet, CONF_TYPE: SureProductID.PET} for pet in conf[CONF_PETS]] + [{CONF_ID: pet, CONF_TYPE: SurepyProduct.PET} for pet in conf[CONF_PETS]] ) _LOGGER.debug("Devices and Pets to setup: %s", things) @@ -158,8 +159,11 @@ class SurePetcareAPI: async def async_update(self, arg: Any = None) -> None: """Refresh Sure Petcare data.""" - await self.surepy.get_data() - + # Fetch all data from SurePet API, refreshing the surepy cache + # TODO: get surepy upstream to add a method to clear the cache explicitly pylint: disable=fixme + await self.surepy._get_resource( # pylint: disable=protected-access + resource=MESTART_RESOURCE + ) for thing in self.ids: sure_id = thing[CONF_ID] sure_type = thing[CONF_TYPE] @@ -168,13 +172,13 @@ class SurePetcareAPI: type_state = self.states.setdefault(sure_type, {}) if sure_type in [ - SureProductID.CAT_FLAP, - SureProductID.PET_FLAP, - SureProductID.FEEDER, - SureProductID.HUB, + SurepyProduct.CAT_FLAP, + SurepyProduct.PET_FLAP, + SurepyProduct.FEEDER, + SurepyProduct.HUB, ]: type_state[sure_id] = await self.surepy.device(sure_id) - elif sure_type == SureProductID.PET: + elif sure_type == SurepyProduct.PET: type_state[sure_id] = await self.surepy.pet(sure_id) except SurePetcareError as error: diff --git a/homeassistant/components/surepetcare/binary_sensor.py b/homeassistant/components/surepetcare/binary_sensor.py index 0cb96731058..2a624b580ac 100644 --- a/homeassistant/components/surepetcare/binary_sensor.py +++ b/homeassistant/components/surepetcare/binary_sensor.py @@ -3,7 +3,7 @@ from datetime import datetime import logging from typing import Any, Dict, Optional -from surepy import SureLocationID, SureProductID +from surepy import SureLocationID, SurepyProduct from homeassistant.components.binary_sensor import ( DEVICE_CLASS_CONNECTIVITY, @@ -37,15 +37,15 @@ async def async_setup_platform( # connectivity if sure_type in [ - SureProductID.CAT_FLAP, - SureProductID.PET_FLAP, - SureProductID.FEEDER, + SurepyProduct.CAT_FLAP, + SurepyProduct.PET_FLAP, + SurepyProduct.FEEDER, ]: entities.append(DeviceConnectivity(sure_id, sure_type, spc)) - if sure_type == SureProductID.PET: + if sure_type == SurepyProduct.PET: entity = Pet(sure_id, spc) - elif sure_type == SureProductID.HUB: + elif sure_type == SurepyProduct.HUB: entity = Hub(sure_id, spc) else: continue @@ -63,7 +63,7 @@ class SurePetcareBinarySensor(BinarySensorEntity): _id: int, spc: SurePetcareAPI, device_class: str, - sure_type: SureProductID, + sure_type: SurepyProduct, ): """Initialize a Sure Petcare binary sensor.""" self._id = _id @@ -138,7 +138,7 @@ class Hub(SurePetcareBinarySensor): def __init__(self, _id: int, spc: SurePetcareAPI) -> None: """Initialize a Sure Petcare Hub.""" - super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, SureProductID.HUB) + super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, SurepyProduct.HUB) @property def available(self) -> bool: @@ -168,7 +168,7 @@ class Pet(SurePetcareBinarySensor): def __init__(self, _id: int, spc: SurePetcareAPI) -> None: """Initialize a Sure Petcare Pet.""" - super().__init__(_id, spc, DEVICE_CLASS_PRESENCE, SureProductID.PET) + super().__init__(_id, spc, DEVICE_CLASS_PRESENCE, SurepyProduct.PET) @property def is_on(self) -> bool: @@ -205,7 +205,7 @@ class DeviceConnectivity(SurePetcareBinarySensor): def __init__( self, _id: int, - sure_type: SureProductID, + sure_type: SurepyProduct, spc: SurePetcareAPI, ) -> None: """Initialize a Sure Petcare Device.""" diff --git a/homeassistant/components/surepetcare/manifest.json b/homeassistant/components/surepetcare/manifest.json index 2fbe4fe245f..99b52a68c8d 100644 --- a/homeassistant/components/surepetcare/manifest.json +++ b/homeassistant/components/surepetcare/manifest.json @@ -3,5 +3,5 @@ "name": "Sure Petcare", "documentation": "https://www.home-assistant.io/integrations/surepetcare", "codeowners": ["@benleb"], - "requirements": ["surepy==0.2.6"] + "requirements": ["surepy==0.4.0"] } diff --git a/homeassistant/components/surepetcare/sensor.py b/homeassistant/components/surepetcare/sensor.py index d3fcb41dbf4..e2d3d070867 100644 --- a/homeassistant/components/surepetcare/sensor.py +++ b/homeassistant/components/surepetcare/sensor.py @@ -2,7 +2,7 @@ import logging from typing import Any, Dict, Optional -from surepy import SureLockStateID, SureProductID +from surepy import SureLockStateID, SurepyProduct from homeassistant.const import ( ATTR_VOLTAGE, @@ -40,13 +40,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= sure_type = entity[CONF_TYPE] if sure_type in [ - SureProductID.CAT_FLAP, - SureProductID.PET_FLAP, - SureProductID.FEEDER, + SurepyProduct.CAT_FLAP, + SurepyProduct.PET_FLAP, + SurepyProduct.FEEDER, ]: entities.append(SureBattery(entity[CONF_ID], sure_type, spc)) - if sure_type in [SureProductID.CAT_FLAP, SureProductID.PET_FLAP]: + if sure_type in [SurepyProduct.CAT_FLAP, SurepyProduct.PET_FLAP]: entities.append(Flap(entity[CONF_ID], sure_type, spc)) async_add_entities(entities, True) @@ -55,7 +55,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info= class SurePetcareSensor(Entity): """A binary sensor implementation for Sure Petcare Entities.""" - def __init__(self, _id: int, sure_type: SureProductID, spc: SurePetcareAPI): + def __init__(self, _id: int, sure_type: SurepyProduct, spc: SurePetcareAPI): """Initialize a Sure Petcare sensor.""" self._id = _id diff --git a/requirements_all.txt b/requirements_all.txt index 4bef886080d..b7d0691b5c4 100644 --- a/requirements_all.txt +++ b/requirements_all.txt @@ -2127,7 +2127,7 @@ sucks==0.9.4 sunwatcher==0.2.1 # homeassistant.components.surepetcare -surepy==0.2.6 +surepy==0.4.0 # homeassistant.components.swiss_hydrological_data swisshydrodata==0.0.3 diff --git a/requirements_test_all.txt b/requirements_test_all.txt index 6585308074a..ceec361fa7f 100644 --- a/requirements_test_all.txt +++ b/requirements_test_all.txt @@ -1049,7 +1049,7 @@ stringcase==1.2.0 sunwatcher==0.2.1 # homeassistant.components.surepetcare -surepy==0.2.6 +surepy==0.4.0 # homeassistant.components.synology_dsm synologydsm-api==1.0.1 diff --git a/tests/components/surepetcare/conftest.py b/tests/components/surepetcare/conftest.py index 9bc06a84267..44e2a722406 100644 --- a/tests/components/surepetcare/conftest.py +++ b/tests/components/surepetcare/conftest.py @@ -18,6 +18,5 @@ async def surepetcare(hass): async_get_clientsession(hass), api_timeout=1, ) - instance.get_data = AsyncMock(return_value=None) - + instance._get_resource = AsyncMock(return_value=None) yield mock_surepetcare diff --git a/tests/components/surepetcare/test_binary_sensor.py b/tests/components/surepetcare/test_binary_sensor.py index 9478ca7a1d4..ad723f707f5 100644 --- a/tests/components/surepetcare/test_binary_sensor.py +++ b/tests/components/surepetcare/test_binary_sensor.py @@ -1,4 +1,6 @@ """The tests for the Sure Petcare binary sensor platform.""" +from surepy import MESTART_RESOURCE + from homeassistant.components.surepetcare.const import DOMAIN from homeassistant.setup import async_setup_component @@ -16,8 +18,7 @@ EXPECTED_ENTITY_IDS = { async def test_binary_sensors(hass, surepetcare) -> None: """Test the generation of unique ids.""" instance = surepetcare.return_value - instance.data = MOCK_API_DATA - instance.get_data.return_value = MOCK_API_DATA + instance._resource[MESTART_RESOURCE] = {"data": MOCK_API_DATA} with _patch_sensor_setup(): assert await async_setup_component(hass, DOMAIN, MOCK_CONFIG)