Clean up surepetcare binary sensor (#52217)
Co-authored-by: Martin Hjelmare <marhje52@gmail.com>
This commit is contained in:
parent
a824313e9f
commit
a788b6ebc1
2 changed files with 58 additions and 79 deletions
|
@ -90,12 +90,10 @@ async def async_setup(hass: HomeAssistant, config: dict) -> bool:
|
|||
async_track_time_interval(hass, spc.async_update, SCAN_INTERVAL)
|
||||
|
||||
# load platforms
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform("binary_sensor", DOMAIN, {}, config)
|
||||
)
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform("sensor", DOMAIN, {}, config)
|
||||
)
|
||||
for platform in PLATFORMS:
|
||||
hass.async_create_task(
|
||||
hass.helpers.discovery.async_load_platform(platform, DOMAIN, {}, config)
|
||||
)
|
||||
|
||||
async def handle_set_lock_state(call):
|
||||
"""Call when setting the lock state."""
|
||||
|
@ -150,6 +148,7 @@ class SurePetcareAPI:
|
|||
self.states = await self.surepy.get_entities()
|
||||
except SurePetcareError as error:
|
||||
_LOGGER.error("Unable to fetch data: %s", error)
|
||||
return
|
||||
|
||||
async_dispatcher_send(self.hass, TOPIC_UPDATE)
|
||||
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
"""Support for Sure PetCare Flaps/Pets binary sensors."""
|
||||
from __future__ import annotations
|
||||
|
||||
from abc import abstractmethod
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from surepy.entities import SurepyEntity
|
||||
from surepy.enums import EntityType, Location, SureEnum
|
||||
from surepy.enums import EntityType, Location
|
||||
|
||||
from homeassistant.components.binary_sensor import (
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
|
@ -67,31 +67,28 @@ class SurePetcareBinarySensor(BinarySensorEntity):
|
|||
self._id = _id
|
||||
self._spc: SurePetcareAPI = spc
|
||||
|
||||
self._surepy_entity: SurepyEntity = self._spc.states[self._id]
|
||||
self._state: SureEnum | dict[str, Any] = None
|
||||
surepy_entity: SurepyEntity = self._spc.states[self._id]
|
||||
|
||||
# cover special case where a device has no name set
|
||||
if self._surepy_entity.name:
|
||||
name = self._surepy_entity.name
|
||||
if surepy_entity.name:
|
||||
name = surepy_entity.name
|
||||
else:
|
||||
name = f"Unnamed {self._surepy_entity.type.name.capitalize()}"
|
||||
name = f"Unnamed {surepy_entity.type.name.capitalize()}"
|
||||
|
||||
self._name = f"{self._surepy_entity.type.name.capitalize()} {name.capitalize()}"
|
||||
self._name = f"{surepy_entity.type.name.capitalize()} {name.capitalize()}"
|
||||
|
||||
self._attr_device_class = device_class
|
||||
self._attr_unique_id = f"{self._surepy_entity.household_id}-{self._id}"
|
||||
self._attr_unique_id = f"{surepy_entity.household_id}-{self._id}"
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
"""Return the name of the device if any."""
|
||||
return self._name
|
||||
|
||||
@abstractmethod
|
||||
@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)
|
||||
|
||||
async def async_added_to_hass(self) -> None:
|
||||
"""Register callbacks."""
|
||||
|
@ -108,29 +105,23 @@ class Hub(SurePetcareBinarySensor):
|
|||
"""Initialize a Sure Petcare Hub."""
|
||||
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY)
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return true if entity is available."""
|
||||
return bool(self._state["online"])
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if entity is online."""
|
||||
return self.available
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the state attributes of the device."""
|
||||
attributes = None
|
||||
if self._surepy_entity.raw_data():
|
||||
attributes = {
|
||||
"led_mode": int(self._surepy_entity.raw_data()["status"]["led_mode"]),
|
||||
@callback
|
||||
def _async_update(self) -> None:
|
||||
"""Get the latest data and update the state."""
|
||||
surepy_entity = self._spc.states[self._id]
|
||||
state = surepy_entity.raw_data()["status"]
|
||||
self._attr_is_on = self._attr_available = bool(state["online"])
|
||||
if surepy_entity.raw_data():
|
||||
self._attr_extra_state_attributes = {
|
||||
"led_mode": int(surepy_entity.raw_data()["status"]["led_mode"]),
|
||||
"pairing_mode": bool(
|
||||
self._surepy_entity.raw_data()["status"]["pairing_mode"]
|
||||
surepy_entity.raw_data()["status"]["pairing_mode"]
|
||||
),
|
||||
}
|
||||
|
||||
return attributes
|
||||
else:
|
||||
self._attr_extra_state_attributes = None
|
||||
_LOGGER.debug("%s -> state: %s", self._name, state)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class Pet(SurePetcareBinarySensor):
|
||||
|
@ -140,29 +131,24 @@ class Pet(SurePetcareBinarySensor):
|
|||
"""Initialize a Sure Petcare Pet."""
|
||||
super().__init__(_id, spc, DEVICE_CLASS_PRESENCE)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if entity is at home."""
|
||||
try:
|
||||
return bool(Location(self._state.where) == Location.INSIDE)
|
||||
except (KeyError, TypeError):
|
||||
return False
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the state attributes of the device."""
|
||||
attributes = None
|
||||
if self._state:
|
||||
attributes = {"since": self._state.since, "where": self._state.where}
|
||||
|
||||
return attributes
|
||||
|
||||
@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.location
|
||||
_LOGGER.debug("%s -> self._state: %s", self._name, self._state)
|
||||
surepy_entity = self._spc.states[self._id]
|
||||
state = surepy_entity.location
|
||||
try:
|
||||
self._attr_is_on = bool(Location(state.where) == Location.INSIDE)
|
||||
except (KeyError, TypeError):
|
||||
self._attr_is_on = False
|
||||
if state:
|
||||
self._attr_extra_state_attributes = {
|
||||
"since": state.since,
|
||||
"where": state.where,
|
||||
}
|
||||
else:
|
||||
self._attr_extra_state_attributes = None
|
||||
_LOGGER.debug("%s -> state: %s", self._name, state)
|
||||
self.async_write_ha_state()
|
||||
|
||||
|
||||
class DeviceConnectivity(SurePetcareBinarySensor):
|
||||
|
@ -176,7 +162,7 @@ class DeviceConnectivity(SurePetcareBinarySensor):
|
|||
"""Initialize a Sure Petcare Device."""
|
||||
super().__init__(_id, spc, DEVICE_CLASS_CONNECTIVITY)
|
||||
self._attr_unique_id = (
|
||||
f"{self._surepy_entity.household_id}-{self._id}-connectivity"
|
||||
f"{self._spc.states[self._id].household_id}-{self._id}-connectivity"
|
||||
)
|
||||
|
||||
@property
|
||||
|
@ -184,24 +170,18 @@ class DeviceConnectivity(SurePetcareBinarySensor):
|
|||
"""Return the name of the device if any."""
|
||||
return f"{self._name}_connectivity"
|
||||
|
||||
@property
|
||||
def available(self) -> bool:
|
||||
"""Return true if entity is available."""
|
||||
return bool(self._state)
|
||||
|
||||
@property
|
||||
def is_on(self) -> bool:
|
||||
"""Return true if entity is online."""
|
||||
return self.available
|
||||
|
||||
@property
|
||||
def extra_state_attributes(self) -> dict[str, Any] | None:
|
||||
"""Return the state attributes of the device."""
|
||||
attributes = None
|
||||
if self._state:
|
||||
attributes = {
|
||||
"device_rssi": f'{self._state["signal"]["device_rssi"]:.2f}',
|
||||
"hub_rssi": f'{self._state["signal"]["hub_rssi"]:.2f}',
|
||||
@callback
|
||||
def _async_update(self) -> None:
|
||||
"""Get the latest data and update the state."""
|
||||
surepy_entity = self._spc.states[self._id]
|
||||
state = surepy_entity.raw_data()["status"]
|
||||
self._attr_is_on = self._attr_available = bool(self.state)
|
||||
if state:
|
||||
self._attr_extra_state_attributes = {
|
||||
"device_rssi": f'{state["signal"]["device_rssi"]:.2f}',
|
||||
"hub_rssi": f'{state["signal"]["hub_rssi"]:.2f}',
|
||||
}
|
||||
|
||||
return attributes
|
||||
else:
|
||||
self._attr_extra_state_attributes = None
|
||||
_LOGGER.debug("%s -> state: %s", self._name, state)
|
||||
self.async_write_ha_state()
|
||||
|
|
Loading…
Add table
Reference in a new issue