2020-01-06 14:00:01 +00:00
|
|
|
"""Support for Sure PetCare Flaps/Pets sensors."""
|
2021-03-18 13:31:38 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
import logging
|
2021-03-18 13:31:38 +00:00
|
|
|
from typing import Any
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-01-02 02:52:33 +00:00
|
|
|
from surepy import SureLockStateID, SurepyProduct
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
from homeassistant.components.sensor import SensorEntity
|
2020-02-28 19:46:48 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
ATTR_VOLTAGE,
|
|
|
|
CONF_ID,
|
|
|
|
CONF_TYPE,
|
|
|
|
DEVICE_CLASS_BATTERY,
|
2020-09-05 19:09:14 +00:00
|
|
|
PERCENTAGE,
|
2020-02-28 19:46:48 +00:00
|
|
|
)
|
2020-01-06 14:00:01 +00:00
|
|
|
from homeassistant.core import callback
|
|
|
|
from homeassistant.helpers.dispatcher import async_dispatcher_connect
|
|
|
|
|
2020-02-09 16:46:00 +00:00
|
|
|
from . import SurePetcareAPI
|
2020-01-06 14:00:01 +00:00
|
|
|
from .const import (
|
|
|
|
DATA_SURE_PETCARE,
|
|
|
|
SPC,
|
|
|
|
SURE_BATT_VOLTAGE_DIFF,
|
|
|
|
SURE_BATT_VOLTAGE_LOW,
|
|
|
|
TOPIC_UPDATE,
|
|
|
|
)
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
|
|
|
async def async_setup_platform(hass, config, async_add_entities, discovery_info=None):
|
|
|
|
"""Set up Sure PetCare Flaps sensors."""
|
|
|
|
if discovery_info is None:
|
|
|
|
return
|
|
|
|
|
2020-02-09 16:46:00 +00:00
|
|
|
entities = []
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
spc = hass.data[DATA_SURE_PETCARE][SPC]
|
|
|
|
|
2020-02-09 16:46:00 +00:00
|
|
|
for entity in spc.ids:
|
|
|
|
sure_type = entity[CONF_TYPE]
|
2020-01-06 14:00:01 +00:00
|
|
|
|
2020-02-09 16:46:00 +00:00
|
|
|
if sure_type in [
|
2021-01-02 02:52:33 +00:00
|
|
|
SurepyProduct.CAT_FLAP,
|
|
|
|
SurepyProduct.PET_FLAP,
|
|
|
|
SurepyProduct.FEEDER,
|
2020-02-09 16:46:00 +00:00
|
|
|
]:
|
|
|
|
entities.append(SureBattery(entity[CONF_ID], sure_type, spc))
|
|
|
|
|
2021-01-02 02:52:33 +00:00
|
|
|
if sure_type in [SurepyProduct.CAT_FLAP, SurepyProduct.PET_FLAP]:
|
2020-02-09 16:46:00 +00:00
|
|
|
entities.append(Flap(entity[CONF_ID], sure_type, spc))
|
|
|
|
|
|
|
|
async_add_entities(entities, True)
|
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class SurePetcareSensor(SensorEntity):
|
2020-02-09 16:46:00 +00:00
|
|
|
"""A binary sensor implementation for Sure Petcare Entities."""
|
|
|
|
|
2021-01-02 02:52:33 +00:00
|
|
|
def __init__(self, _id: int, sure_type: SurepyProduct, spc: SurePetcareAPI):
|
2020-02-09 16:46:00 +00:00
|
|
|
"""Initialize a Sure Petcare sensor."""
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
self._id = _id
|
2020-02-09 16:46:00 +00:00
|
|
|
self._sure_type = sure_type
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
self._spc = spc
|
2021-03-18 13:31:38 +00:00
|
|
|
self._spc_data: dict[str, Any] = self._spc.states[self._sure_type].get(self._id)
|
|
|
|
self._state: dict[str, Any] = {}
|
2020-02-09 16:46:00 +00:00
|
|
|
|
|
|
|
self._name = (
|
|
|
|
f"{self._sure_type.name.capitalize()} "
|
|
|
|
f"{self._spc_data['name'].capitalize()}"
|
|
|
|
)
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
self._async_unsub_dispatcher_connect = None
|
|
|
|
|
|
|
|
@property
|
2020-02-09 16:46:00 +00:00
|
|
|
def name(self) -> str:
|
|
|
|
"""Return the name of the device if any."""
|
|
|
|
return self._name
|
|
|
|
|
|
|
|
@property
|
|
|
|
def unique_id(self) -> str:
|
|
|
|
"""Return an unique ID."""
|
|
|
|
return f"{self._spc_data['household_id']}-{self._id}"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return true if entity is available."""
|
|
|
|
return bool(self._state)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def should_poll(self) -> bool:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return true."""
|
|
|
|
return False
|
|
|
|
|
2020-02-09 16:46:00 +00:00
|
|
|
async def async_update(self) -> None:
|
|
|
|
"""Get the latest data and update the state."""
|
|
|
|
self._spc_data = self._spc.states[self._sure_type].get(self._id)
|
|
|
|
self._state = self._spc_data.get("status")
|
|
|
|
_LOGGER.debug("%s -> self._state: %s", self._name, self._state)
|
|
|
|
|
|
|
|
async def async_added_to_hass(self) -> None:
|
|
|
|
"""Register callbacks."""
|
|
|
|
|
|
|
|
@callback
|
|
|
|
def update() -> None:
|
|
|
|
"""Update the state."""
|
|
|
|
self.async_schedule_update_ha_state(True)
|
|
|
|
|
|
|
|
self._async_unsub_dispatcher_connect = async_dispatcher_connect(
|
|
|
|
self.hass, TOPIC_UPDATE, update
|
|
|
|
)
|
|
|
|
|
|
|
|
async def async_will_remove_from_hass(self) -> None:
|
|
|
|
"""Disconnect dispatcher listener when removed."""
|
|
|
|
if self._async_unsub_dispatcher_connect:
|
|
|
|
self._async_unsub_dispatcher_connect()
|
|
|
|
|
|
|
|
|
|
|
|
class Flap(SurePetcareSensor):
|
|
|
|
"""Sure Petcare Flap."""
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def state(self) -> int | None:
|
2020-02-09 16:46:00 +00:00
|
|
|
"""Return battery level in percent."""
|
|
|
|
return SureLockStateID(self._state["locking"]["mode"]).name.capitalize()
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-02-09 16:46:00 +00:00
|
|
|
"""Return the state attributes of the device."""
|
|
|
|
attributes = None
|
|
|
|
if self._state:
|
2020-02-28 19:46:48 +00:00
|
|
|
attributes = {"learn_mode": bool(self._state["learn_mode"])}
|
2020-02-09 16:46:00 +00:00
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
|
|
|
|
class SureBattery(SurePetcareSensor):
|
|
|
|
"""Sure Petcare Flap."""
|
|
|
|
|
2020-01-06 14:00:01 +00:00
|
|
|
@property
|
2020-02-09 16:46:00 +00:00
|
|
|
def name(self) -> str:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return the name of the device if any."""
|
2020-02-09 16:46:00 +00:00
|
|
|
return f"{self._name} Battery Level"
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def state(self) -> int | None:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return battery level in percent."""
|
2021-03-18 13:31:38 +00:00
|
|
|
battery_percent: int | None
|
2020-01-06 14:00:01 +00:00
|
|
|
try:
|
|
|
|
per_battery_voltage = self._state["battery"] / 4
|
|
|
|
voltage_diff = per_battery_voltage - SURE_BATT_VOLTAGE_LOW
|
2020-02-09 16:46:00 +00:00
|
|
|
battery_percent = min(int(voltage_diff / SURE_BATT_VOLTAGE_DIFF * 100), 100)
|
2020-01-06 14:00:01 +00:00
|
|
|
except (KeyError, TypeError):
|
|
|
|
battery_percent = None
|
|
|
|
|
|
|
|
return battery_percent
|
|
|
|
|
|
|
|
@property
|
2020-02-09 16:46:00 +00:00
|
|
|
def unique_id(self) -> str:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return an unique ID."""
|
2020-02-09 16:46:00 +00:00
|
|
|
return f"{self._spc_data['household_id']}-{self._id}-battery"
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
@property
|
2020-02-09 16:46:00 +00:00
|
|
|
def device_class(self) -> str:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return the device class."""
|
|
|
|
return DEVICE_CLASS_BATTERY
|
|
|
|
|
|
|
|
@property
|
2021-03-18 13:31:38 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any] | None:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return state attributes."""
|
|
|
|
attributes = None
|
|
|
|
if self._state:
|
2020-02-09 16:46:00 +00:00
|
|
|
voltage_per_battery = float(self._state["battery"]) / 4
|
|
|
|
attributes = {
|
|
|
|
ATTR_VOLTAGE: f"{float(self._state['battery']):.2f}",
|
|
|
|
f"{ATTR_VOLTAGE}_per_battery": f"{voltage_per_battery:.2f}",
|
|
|
|
}
|
2020-01-06 14:00:01 +00:00
|
|
|
|
|
|
|
return attributes
|
|
|
|
|
|
|
|
@property
|
2020-02-09 16:46:00 +00:00
|
|
|
def unit_of_measurement(self) -> str:
|
2020-01-06 14:00:01 +00:00
|
|
|
"""Return the unit of measurement."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|