Update surepy to v0.4.0 (#44556)

* Update surepy to v0.4.0

* Clarify pylint disable

Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
Will Hughes 2021-01-02 15:52:33 +13:00 committed by GitHub
parent 321c0a87ae
commit a6e474c7c9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 41 additions and 37 deletions

View File

@ -3,10 +3,11 @@ import logging
from typing import Any, Dict, List from typing import Any, Dict, List
from surepy import ( from surepy import (
MESTART_RESOURCE,
SurePetcare, SurePetcare,
SurePetcareAuthenticationError, SurePetcareAuthenticationError,
SurePetcareError, SurePetcareError,
SureProductID, SurepyProduct,
) )
import voluptuous as vol import voluptuous as vol
@ -81,7 +82,7 @@ async def async_setup(hass, config) -> bool:
async_get_clientsession(hass), async_get_clientsession(hass),
api_timeout=SURE_API_TIMEOUT, api_timeout=SURE_API_TIMEOUT,
) )
await surepy.get_data()
except SurePetcareAuthenticationError: except SurePetcareAuthenticationError:
_LOGGER.error("Unable to connect to surepetcare.io: Wrong credentials!") _LOGGER.error("Unable to connect to surepetcare.io: Wrong credentials!")
return False return False
@ -91,14 +92,14 @@ async def async_setup(hass, config) -> bool:
# add feeders # add feeders
things = [ things = [
{CONF_ID: feeder, CONF_TYPE: SureProductID.FEEDER} {CONF_ID: feeder, CONF_TYPE: SurepyProduct.FEEDER}
for feeder in conf[CONF_FEEDERS] for feeder in conf[CONF_FEEDERS]
] ]
# add flaps (don't differentiate between CAT and PET for now) # add flaps (don't differentiate between CAT and PET for now)
things.extend( things.extend(
[ [
{CONF_ID: flap, CONF_TYPE: SureProductID.PET_FLAP} {CONF_ID: flap, CONF_TYPE: SurepyProduct.PET_FLAP}
for flap in conf[CONF_FLAPS] 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]) device_data = await surepy.device(device[CONF_ID])
if ( if (
CONF_PARENT in device_data 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 and device_data[CONF_PARENT][CONF_ID] not in hub_ids
): ):
things.append( things.append(
{ {
CONF_ID: device_data[CONF_PARENT][CONF_ID], 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]) hub_ids.add(device_data[CONF_PARENT][CONF_ID])
# add pets # add pets
things.extend( 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) _LOGGER.debug("Devices and Pets to setup: %s", things)
@ -158,8 +159,11 @@ class SurePetcareAPI:
async def async_update(self, arg: Any = None) -> None: async def async_update(self, arg: Any = None) -> None:
"""Refresh Sure Petcare data.""" """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: for thing in self.ids:
sure_id = thing[CONF_ID] sure_id = thing[CONF_ID]
sure_type = thing[CONF_TYPE] sure_type = thing[CONF_TYPE]
@ -168,13 +172,13 @@ class SurePetcareAPI:
type_state = self.states.setdefault(sure_type, {}) type_state = self.states.setdefault(sure_type, {})
if sure_type in [ if sure_type in [
SureProductID.CAT_FLAP, SurepyProduct.CAT_FLAP,
SureProductID.PET_FLAP, SurepyProduct.PET_FLAP,
SureProductID.FEEDER, SurepyProduct.FEEDER,
SureProductID.HUB, SurepyProduct.HUB,
]: ]:
type_state[sure_id] = await self.surepy.device(sure_id) 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) type_state[sure_id] = await self.surepy.pet(sure_id)
except SurePetcareError as error: except SurePetcareError as error:

View File

@ -3,7 +3,7 @@ from datetime import datetime
import logging import logging
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from surepy import SureLocationID, SureProductID from surepy import SureLocationID, SurepyProduct
from homeassistant.components.binary_sensor import ( from homeassistant.components.binary_sensor import (
DEVICE_CLASS_CONNECTIVITY, DEVICE_CLASS_CONNECTIVITY,
@ -37,15 +37,15 @@ async def async_setup_platform(
# connectivity # connectivity
if sure_type in [ if sure_type in [
SureProductID.CAT_FLAP, SurepyProduct.CAT_FLAP,
SureProductID.PET_FLAP, SurepyProduct.PET_FLAP,
SureProductID.FEEDER, SurepyProduct.FEEDER,
]: ]:
entities.append(DeviceConnectivity(sure_id, sure_type, spc)) entities.append(DeviceConnectivity(sure_id, sure_type, spc))
if sure_type == SureProductID.PET: if sure_type == SurepyProduct.PET:
entity = Pet(sure_id, spc) entity = Pet(sure_id, spc)
elif sure_type == SureProductID.HUB: elif sure_type == SurepyProduct.HUB:
entity = Hub(sure_id, spc) entity = Hub(sure_id, spc)
else: else:
continue continue
@ -63,7 +63,7 @@ class SurePetcareBinarySensor(BinarySensorEntity):
_id: int, _id: int,
spc: SurePetcareAPI, spc: SurePetcareAPI,
device_class: str, device_class: str,
sure_type: SureProductID, sure_type: SurepyProduct,
): ):
"""Initialize a Sure Petcare binary sensor.""" """Initialize a Sure Petcare binary sensor."""
self._id = _id self._id = _id
@ -138,7 +138,7 @@ class Hub(SurePetcareBinarySensor):
def __init__(self, _id: int, spc: SurePetcareAPI) -> None: def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Hub.""" """Initialize a Sure Petcare Hub."""
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, SureProductID.HUB) super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, SurepyProduct.HUB)
@property @property
def available(self) -> bool: def available(self) -> bool:
@ -168,7 +168,7 @@ class Pet(SurePetcareBinarySensor):
def __init__(self, _id: int, spc: SurePetcareAPI) -> None: def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Pet.""" """Initialize a Sure Petcare Pet."""
super().__init__(_id, spc, DEVICE_CLASS_PRESENCE, SureProductID.PET) super().__init__(_id, spc, DEVICE_CLASS_PRESENCE, SurepyProduct.PET)
@property @property
def is_on(self) -> bool: def is_on(self) -> bool:
@ -205,7 +205,7 @@ class DeviceConnectivity(SurePetcareBinarySensor):
def __init__( def __init__(
self, self,
_id: int, _id: int,
sure_type: SureProductID, sure_type: SurepyProduct,
spc: SurePetcareAPI, spc: SurePetcareAPI,
) -> None: ) -> None:
"""Initialize a Sure Petcare Device.""" """Initialize a Sure Petcare Device."""

View File

@ -3,5 +3,5 @@
"name": "Sure Petcare", "name": "Sure Petcare",
"documentation": "https://www.home-assistant.io/integrations/surepetcare", "documentation": "https://www.home-assistant.io/integrations/surepetcare",
"codeowners": ["@benleb"], "codeowners": ["@benleb"],
"requirements": ["surepy==0.2.6"] "requirements": ["surepy==0.4.0"]
} }

View File

@ -2,7 +2,7 @@
import logging import logging
from typing import Any, Dict, Optional from typing import Any, Dict, Optional
from surepy import SureLockStateID, SureProductID from surepy import SureLockStateID, SurepyProduct
from homeassistant.const import ( from homeassistant.const import (
ATTR_VOLTAGE, ATTR_VOLTAGE,
@ -40,13 +40,13 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
sure_type = entity[CONF_TYPE] sure_type = entity[CONF_TYPE]
if sure_type in [ if sure_type in [
SureProductID.CAT_FLAP, SurepyProduct.CAT_FLAP,
SureProductID.PET_FLAP, SurepyProduct.PET_FLAP,
SureProductID.FEEDER, SurepyProduct.FEEDER,
]: ]:
entities.append(SureBattery(entity[CONF_ID], sure_type, spc)) 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)) entities.append(Flap(entity[CONF_ID], sure_type, spc))
async_add_entities(entities, True) async_add_entities(entities, True)
@ -55,7 +55,7 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
class SurePetcareSensor(Entity): class SurePetcareSensor(Entity):
"""A binary sensor implementation for Sure Petcare Entities.""" """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.""" """Initialize a Sure Petcare sensor."""
self._id = _id self._id = _id

View File

@ -2127,7 +2127,7 @@ sucks==0.9.4
sunwatcher==0.2.1 sunwatcher==0.2.1
# homeassistant.components.surepetcare # homeassistant.components.surepetcare
surepy==0.2.6 surepy==0.4.0
# homeassistant.components.swiss_hydrological_data # homeassistant.components.swiss_hydrological_data
swisshydrodata==0.0.3 swisshydrodata==0.0.3

View File

@ -1049,7 +1049,7 @@ stringcase==1.2.0
sunwatcher==0.2.1 sunwatcher==0.2.1
# homeassistant.components.surepetcare # homeassistant.components.surepetcare
surepy==0.2.6 surepy==0.4.0
# homeassistant.components.synology_dsm # homeassistant.components.synology_dsm
synologydsm-api==1.0.1 synologydsm-api==1.0.1

View File

@ -18,6 +18,5 @@ async def surepetcare(hass):
async_get_clientsession(hass), async_get_clientsession(hass),
api_timeout=1, api_timeout=1,
) )
instance.get_data = AsyncMock(return_value=None) instance._get_resource = AsyncMock(return_value=None)
yield mock_surepetcare yield mock_surepetcare

View File

@ -1,4 +1,6 @@
"""The tests for the Sure Petcare binary sensor platform.""" """The tests for the Sure Petcare binary sensor platform."""
from surepy import MESTART_RESOURCE
from homeassistant.components.surepetcare.const import DOMAIN from homeassistant.components.surepetcare.const import DOMAIN
from homeassistant.setup import async_setup_component from homeassistant.setup import async_setup_component
@ -16,8 +18,7 @@ EXPECTED_ENTITY_IDS = {
async def test_binary_sensors(hass, surepetcare) -> None: async def test_binary_sensors(hass, surepetcare) -> None:
"""Test the generation of unique ids.""" """Test the generation of unique ids."""
instance = surepetcare.return_value instance = surepetcare.return_value
instance.data = MOCK_API_DATA instance._resource[MESTART_RESOURCE] = {"data": MOCK_API_DATA}
instance.get_data.return_value = MOCK_API_DATA
with _patch_sensor_setup(): with _patch_sensor_setup():
assert await async_setup_component(hass, DOMAIN, MOCK_CONFIG) assert await async_setup_component(hass, DOMAIN, MOCK_CONFIG)