mirror of
https://github.com/home-assistant/core.git
synced 2025-07-09 06:17:07 +00:00
Surepetcare, Use entity class vars and some clean up (#52205)
This commit is contained in:
parent
db2fda09b9
commit
b45c8466b4
@ -480,7 +480,7 @@ homeassistant/components/subaru/* @G-Two
|
|||||||
homeassistant/components/suez_water/* @ooii
|
homeassistant/components/suez_water/* @ooii
|
||||||
homeassistant/components/sun/* @Swamp-Ig
|
homeassistant/components/sun/* @Swamp-Ig
|
||||||
homeassistant/components/supla/* @mwegrzynek
|
homeassistant/components/supla/* @mwegrzynek
|
||||||
homeassistant/components/surepetcare/* @benleb
|
homeassistant/components/surepetcare/* @benleb @danielhiversen
|
||||||
homeassistant/components/swiss_hydrological_data/* @fabaff
|
homeassistant/components/swiss_hydrological_data/* @fabaff
|
||||||
homeassistant/components/swiss_public_transport/* @fabaff
|
homeassistant/components/swiss_public_transport/* @fabaff
|
||||||
homeassistant/components/switchbot/* @danielhiversen
|
homeassistant/components/switchbot/* @danielhiversen
|
||||||
|
@ -41,9 +41,7 @@ async def async_setup_platform(
|
|||||||
EntityType.FEEDER,
|
EntityType.FEEDER,
|
||||||
EntityType.FELAQUA,
|
EntityType.FELAQUA,
|
||||||
]:
|
]:
|
||||||
entities.append(
|
entities.append(DeviceConnectivity(surepy_entity.id, spc))
|
||||||
DeviceConnectivity(surepy_entity.id, surepy_entity.type, spc)
|
|
||||||
)
|
|
||||||
|
|
||||||
if surepy_entity.type == EntityType.PET:
|
if surepy_entity.type == EntityType.PET:
|
||||||
entities.append(Pet(surepy_entity.id, spc))
|
entities.append(Pet(surepy_entity.id, spc))
|
||||||
@ -56,18 +54,17 @@ async def async_setup_platform(
|
|||||||
class SurePetcareBinarySensor(BinarySensorEntity):
|
class SurePetcareBinarySensor(BinarySensorEntity):
|
||||||
"""A binary sensor implementation for Sure Petcare Entities."""
|
"""A binary sensor implementation for Sure Petcare Entities."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
_id: int,
|
_id: int,
|
||||||
spc: SurePetcareAPI,
|
spc: SurePetcareAPI,
|
||||||
device_class: str,
|
device_class: str,
|
||||||
sure_type: EntityType,
|
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Sure Petcare binary sensor."""
|
"""Initialize a Sure Petcare binary sensor."""
|
||||||
|
|
||||||
self._id = _id
|
self._id = _id
|
||||||
self._device_class = device_class
|
|
||||||
|
|
||||||
self._spc: SurePetcareAPI = spc
|
self._spc: SurePetcareAPI = spc
|
||||||
|
|
||||||
self._surepy_entity: SurepyEntity = self._spc.states[self._id]
|
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()}"
|
self._name = f"{self._surepy_entity.type.name.capitalize()} {name.capitalize()}"
|
||||||
|
|
||||||
@property
|
self._attr_device_class = device_class
|
||||||
def should_poll(self) -> bool:
|
self._attr_unique_id = f"{self._surepy_entity.household_id}-{self._id}"
|
||||||
"""Return if the entity should use default polling."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the device if any."""
|
"""Return the name of the device if any."""
|
||||||
return self._name
|
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
|
@callback
|
||||||
def _async_update(self) -> None:
|
def _async_update(self) -> None:
|
||||||
"""Get the latest data and update the state."""
|
"""Get the latest data and update the state."""
|
||||||
@ -121,7 +106,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, EntityType.HUB)
|
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
@ -153,7 +138,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, EntityType.PET)
|
super().__init__(_id, spc, DEVICE_CLASS_PRESENCE)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def is_on(self) -> bool:
|
def is_on(self) -> bool:
|
||||||
@ -186,22 +171,19 @@ class DeviceConnectivity(SurePetcareBinarySensor):
|
|||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
_id: int,
|
_id: int,
|
||||||
sure_type: EntityType,
|
|
||||||
spc: SurePetcareAPI,
|
spc: SurePetcareAPI,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a Sure Petcare Device."""
|
"""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
|
@property
|
||||||
def name(self) -> str:
|
def name(self) -> str:
|
||||||
"""Return the name of the device if any."""
|
"""Return the name of the device if any."""
|
||||||
return f"{self._name}_connectivity"
|
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
|
@property
|
||||||
def available(self) -> bool:
|
def available(self) -> bool:
|
||||||
"""Return true if entity is available."""
|
"""Return true if entity is available."""
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
"domain": "surepetcare",
|
"domain": "surepetcare",
|
||||||
"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", "@danielhiversen"],
|
||||||
"requirements": ["surepy==0.6.0"],
|
"requirements": ["surepy==0.6.0"],
|
||||||
"iot_class": "cloud_polling"
|
"iot_class": "cloud_polling"
|
||||||
}
|
}
|
||||||
|
@ -46,8 +46,10 @@ async def async_setup_platform(hass, config, async_add_entities, discovery_info=
|
|||||||
async_add_entities(entities)
|
async_add_entities(entities)
|
||||||
|
|
||||||
|
|
||||||
class SurePetcareSensor(SensorEntity):
|
class SureBattery(SensorEntity):
|
||||||
"""A binary sensor implementation for Sure Petcare Entities."""
|
"""A sensor implementation for Sure Petcare Entities."""
|
||||||
|
|
||||||
|
_attr_should_poll = False
|
||||||
|
|
||||||
def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
|
def __init__(self, _id: int, spc: SurePetcareAPI) -> None:
|
||||||
"""Initialize a Sure Petcare sensor."""
|
"""Initialize a Sure Petcare sensor."""
|
||||||
@ -57,9 +59,12 @@ class SurePetcareSensor(SensorEntity):
|
|||||||
|
|
||||||
self._surepy_entity: SurepyEntity = self._spc.states[_id]
|
self._surepy_entity: SurepyEntity = self._spc.states[_id]
|
||||||
self._state: dict[str, Any] = {}
|
self._state: dict[str, Any] = {}
|
||||||
self._name = (
|
|
||||||
f"{self._surepy_entity.type.name.capitalize()} "
|
self._attr_device_class = DEVICE_CLASS_BATTERY
|
||||||
f"{self._surepy_entity.name.capitalize()}"
|
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
|
@property
|
||||||
@ -67,17 +72,12 @@ class SurePetcareSensor(SensorEntity):
|
|||||||
"""Return true if entity is available."""
|
"""Return true if entity is available."""
|
||||||
return bool(self._state)
|
return bool(self._state)
|
||||||
|
|
||||||
@property
|
|
||||||
def should_poll(self) -> bool:
|
|
||||||
"""Return true."""
|
|
||||||
return False
|
|
||||||
|
|
||||||
@callback
|
@callback
|
||||||
def _async_update(self) -> None:
|
def _async_update(self) -> None:
|
||||||
"""Get the latest data and update the state."""
|
"""Get the latest data and update the state."""
|
||||||
self._surepy_entity = self._spc.states[self._id]
|
self._surepy_entity = self._spc.states[self._id]
|
||||||
self._state = self._surepy_entity.raw_data()["status"]
|
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:
|
async def async_added_to_hass(self) -> None:
|
||||||
"""Register callbacks."""
|
"""Register callbacks."""
|
||||||
@ -86,37 +86,15 @@ class SurePetcareSensor(SensorEntity):
|
|||||||
)
|
)
|
||||||
self._async_update()
|
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
|
@property
|
||||||
def state(self) -> int | None:
|
def state(self) -> int | None:
|
||||||
"""Return battery level in percent."""
|
"""Return battery level in percent."""
|
||||||
battery_percent: int | None
|
|
||||||
try:
|
try:
|
||||||
per_battery_voltage = self._state["battery"] / 4
|
per_battery_voltage = self._state["battery"] / 4
|
||||||
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
|
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):
|
except (KeyError, TypeError):
|
||||||
battery_percent = None
|
return 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
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||||
@ -130,8 +108,3 @@ class SureBattery(SurePetcareSensor):
|
|||||||
}
|
}
|
||||||
|
|
||||||
return attributes
|
return attributes
|
||||||
|
|
||||||
@property
|
|
||||||
def unit_of_measurement(self) -> str:
|
|
||||||
"""Return the unit of measurement."""
|
|
||||||
return PERCENTAGE
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user