Surepetcare, Use entity class vars and some clean up (#52205)

This commit is contained in:
Daniel Hjelseth Høyer 2021-06-27 00:40:40 +02:00 committed by GitHub
parent db2fda09b9
commit b45c8466b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 71 deletions

View File

@ -480,7 +480,7 @@ homeassistant/components/subaru/* @G-Two
homeassistant/components/suez_water/* @ooii
homeassistant/components/sun/* @Swamp-Ig
homeassistant/components/supla/* @mwegrzynek
homeassistant/components/surepetcare/* @benleb
homeassistant/components/surepetcare/* @benleb @danielhiversen
homeassistant/components/swiss_hydrological_data/* @fabaff
homeassistant/components/swiss_public_transport/* @fabaff
homeassistant/components/switchbot/* @danielhiversen

View File

@ -41,9 +41,7 @@ async def async_setup_platform(
EntityType.FEEDER,
EntityType.FELAQUA,
]:
entities.append(
DeviceConnectivity(surepy_entity.id, surepy_entity.type, spc)
)
entities.append(DeviceConnectivity(surepy_entity.id, spc))
if surepy_entity.type == EntityType.PET:
entities.append(Pet(surepy_entity.id, spc))
@ -56,18 +54,17 @@ async def async_setup_platform(
class SurePetcareBinarySensor(BinarySensorEntity):
"""A binary sensor implementation for Sure Petcare Entities."""
_attr_should_poll = False
def __init__(
self,
_id: int,
spc: SurePetcareAPI,
device_class: str,
sure_type: EntityType,
) -> None:
"""Initialize a Sure Petcare binary sensor."""
self._id = _id
self._device_class = device_class
self._spc: SurePetcareAPI = spc
self._surepy_entity: SurepyEntity = self._spc.states[self._id]
@ -81,26 +78,14 @@ class SurePetcareBinarySensor(BinarySensorEntity):
self._name = f"{self._surepy_entity.type.name.capitalize()} {name.capitalize()}"
@property
def should_poll(self) -> bool:
"""Return if the entity should use default polling."""
return False
self._attr_device_class = device_class
self._attr_unique_id = f"{self._surepy_entity.household_id}-{self._id}"
@property
def name(self) -> str:
"""Return the name of the device if any."""
return self._name
@property
def device_class(self) -> str:
"""Return the device class."""
return None if not self._device_class else self._device_class
@property
def unique_id(self) -> str:
"""Return an unique ID."""
return f"{self._surepy_entity.household_id}-{self._id}"
@callback
def _async_update(self) -> None:
"""Get the latest data and update the state."""
@ -121,7 +106,7 @@ class Hub(SurePetcareBinarySensor):
def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Hub."""
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, EntityType.HUB)
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY)
@property
def available(self) -> bool:
@ -153,7 +138,7 @@ class Pet(SurePetcareBinarySensor):
def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare Pet."""
super().__init__(_id, spc, DEVICE_CLASS_PRESENCE, EntityType.PET)
super().__init__(_id, spc, DEVICE_CLASS_PRESENCE)
@property
def is_on(self) -> bool:
@ -186,22 +171,19 @@ class DeviceConnectivity(SurePetcareBinarySensor):
def __init__(
self,
_id: int,
sure_type: EntityType,
spc: SurePetcareAPI,
) -> None:
"""Initialize a Sure Petcare Device."""
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY, sure_type)
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY)
self._attr_unique_id = (
f"{self._surepy_entity.household_id}-{self._id}-connectivity"
)
@property
def name(self) -> str:
"""Return the name of the device if any."""
return f"{self._name}_connectivity"
@property
def unique_id(self) -> str:
"""Return an unique ID."""
return f"{self._surepy_entity.household_id}-{self._id}-connectivity"
@property
def available(self) -> bool:
"""Return true if entity is available."""

View File

@ -2,7 +2,7 @@
"domain": "surepetcare",
"name": "Sure Petcare",
"documentation": "https://www.home-assistant.io/integrations/surepetcare",
"codeowners": ["@benleb"],
"codeowners": ["@benleb", "@danielhiversen"],
"requirements": ["surepy==0.6.0"],
"iot_class": "cloud_polling"
}

View File

@ -46,8 +46,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
async_add_entities(entities)
class SurePetcareSensor(SensorEntity):
"""A binary sensor implementation for Sure Petcare Entities."""
class SureBattery(SensorEntity):
"""A sensor implementation for Sure Petcare Entities."""
_attr_should_poll = False
def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
"""Initialize a Sure Petcare sensor."""
@ -57,9 +59,12 @@ class SurePetcareSensor(SensorEntity):
self._surepy_entity: SurepyEntity = self._spc.states[_id]
self._state: dict[str, Any] = {}
self._name = (
f"{self._surepy_entity.type.name.capitalize()} "
f"{self._surepy_entity.name.capitalize()}"
self._attr_device_class = DEVICE_CLASS_BATTERY
self._attr_name = f"{self._surepy_entity.type.name.capitalize()} {self._surepy_entity.name.capitalize()} Battery Level"
self._attr_unit_of_measurement = PERCENTAGE
self._attr_unique_id = (
f"{self._surepy_entity.household_id}-{self._surepy_entity.id}-battery"
)
@property
@ -67,17 +72,12 @@ class SurePetcareSensor(SensorEntity):
"""Return true if entity is available."""
return bool(self._state)
@property
def should_poll(self) -> bool:
"""Return true."""
return False
@callback
def _async_update(self) -> None:
"""Get the latest data and update the state."""
self._surepy_entity = self._spc.states[self._id]
self._state = self._surepy_entity.raw_data()["status"]
_LOGGER.debug("%s -> self._state: %s", self._name, self._state)
_LOGGER.debug("%s -> self._state: %s", self.name, self._state)
async def async_added_to_hass(self) -> None:
"""Register callbacks."""
@ -86,37 +86,15 @@ class SurePetcareSensor(SensorEntity):
)
self._async_update()
class SureBattery(SurePetcareSensor):
"""Sure Petcare Flap."""
@property
def name(self) -> str:
"""Return the name of the device if any."""
return f"{self._name} Battery Level"
@property
def state(self) -> int | None:
"""Return battery level in percent."""
battery_percent: int | None
try:
per_battery_voltage = self._state["battery"] / 4
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
battery_percent = min(int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100)
return min(int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100)
except (KeyError, TypeError):
battery_percent = None
return battery_percent
@property
def unique_id(self) -> str:
"""Return an unique ID."""
return f"{self._surepy_entity.household_id}-{self._surepy_entity.id}-battery"
@property
def device_class(self) -> str:
"""Return the device class."""
return DEVICE_CLASS_BATTERY
return None
@property
def extra_state_attributes(self) -> dict[str, Any] | None:
@ -130,8 +108,3 @@ class SureBattery(SurePetcareSensor):
}
return attributes
@property
def unit_of_measurement(self) -> str:
"""Return the unit of measurement."""
return PERCENTAGE