2019-10-07 18:09:08 +00:00
|
|
|
"""Support for Neato sensors."""
|
2021-08-08 13:02:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-10-10 06:08:11 +00:00
|
|
|
from datetime import timedelta
|
2019-10-07 18:09:08 +00:00
|
|
|
import logging
|
2021-08-08 13:02:37 +00:00
|
|
|
from typing import Any
|
2019-10-07 18:09:08 +00:00
|
|
|
|
|
|
|
from pybotvac.exceptions import NeatoRobotException
|
2021-08-08 13:02:37 +00:00
|
|
|
from pybotvac.robot import Robot
|
2019-10-07 18:09:08 +00:00
|
|
|
|
2021-12-16 14:49:11 +00:00
|
|
|
from homeassistant.components.sensor import SensorDeviceClass, SensorEntity
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-16 14:49:11 +00:00
|
|
|
from homeassistant.const import PERCENTAGE
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-12-16 14:49:11 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo, EntityCategory
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-10-07 18:09:08 +00:00
|
|
|
|
2021-12-12 23:24:46 +00:00
|
|
|
from . import NeatoHub
|
2019-10-10 06:08:11 +00:00
|
|
|
from .const import NEATO_DOMAIN, NEATO_LOGIN, NEATO_ROBOTS, SCAN_INTERVAL_MINUTES
|
2019-10-07 18:09:08 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)
|
|
|
|
|
|
|
|
BATTERY = "Battery"
|
|
|
|
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Set up the Neato sensor using config entry."""
|
|
|
|
dev = []
|
2021-08-08 13:02:37 +00:00
|
|
|
neato: NeatoHub = hass.data[NEATO_LOGIN]
|
2019-10-07 18:09:08 +00:00
|
|
|
for robot in hass.data[NEATO_ROBOTS]:
|
|
|
|
dev.append(NeatoSensor(neato, robot))
|
|
|
|
|
|
|
|
if not dev:
|
|
|
|
return
|
|
|
|
|
|
|
|
_LOGGER.debug("Adding robots for sensors %s", dev)
|
|
|
|
async_add_entities(dev, True)
|
|
|
|
|
|
|
|
|
2021-03-22 18:46:46 +00:00
|
|
|
class NeatoSensor(SensorEntity):
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Neato sensor."""
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def __init__(self, neato: NeatoHub, robot: Robot) -> None:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Initialize Neato sensor."""
|
|
|
|
self.robot = robot
|
2021-08-08 13:02:37 +00:00
|
|
|
self._available: bool = False
|
|
|
|
self._robot_name: str = f"{self.robot.name} {BATTERY}"
|
|
|
|
self._robot_serial: str = self.robot.serial
|
|
|
|
self._state: dict[str, Any] | None = None
|
2019-10-07 18:09:08 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def update(self) -> None:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Update Neato Sensor."""
|
|
|
|
try:
|
|
|
|
self._state = self.robot.state
|
|
|
|
except NeatoRobotException as ex:
|
|
|
|
if self._available:
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato sensor connection error for '%s': %s", self.entity_id, ex
|
|
|
|
)
|
2019-10-07 18:09:08 +00:00
|
|
|
self._state = None
|
|
|
|
self._available = False
|
|
|
|
return
|
|
|
|
|
|
|
|
self._available = True
|
|
|
|
_LOGGER.debug("self._state=%s", self._state)
|
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def name(self) -> str:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return the name of this sensor."""
|
|
|
|
return self._robot_name
|
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def unique_id(self) -> str:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return unique ID."""
|
|
|
|
return self._robot_serial
|
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def device_class(self) -> str:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return the device class."""
|
2021-12-16 14:49:11 +00:00
|
|
|
return SensorDeviceClass.BATTERY
|
2019-10-07 18:09:08 +00:00
|
|
|
|
2021-10-25 07:44:15 +00:00
|
|
|
@property
|
2021-12-27 17:23:08 +00:00
|
|
|
def entity_category(self) -> EntityCategory:
|
2021-10-25 07:44:15 +00:00
|
|
|
"""Device entity category."""
|
2021-12-16 14:49:11 +00:00
|
|
|
return EntityCategory.DIAGNOSTIC
|
2021-10-25 07:44:15 +00:00
|
|
|
|
2019-10-07 18:09:08 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def available(self) -> bool:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return availability."""
|
|
|
|
return self._available
|
|
|
|
|
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_value(self) -> str | None:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return the state."""
|
2021-08-08 13:02:37 +00:00
|
|
|
if self._state is not None:
|
|
|
|
return str(self._state["details"]["charge"])
|
|
|
|
return None
|
2019-10-07 18:09:08 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-12 11:26:17 +00:00
|
|
|
def native_unit_of_measurement(self) -> str:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Return unit of measurement."""
|
2020-09-05 19:09:14 +00:00
|
|
|
return PERCENTAGE
|
2019-10-07 18:09:08 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-10-07 18:09:08 +00:00
|
|
|
"""Device info for neato robot."""
|
2021-10-24 09:34:45 +00:00
|
|
|
return DeviceInfo(identifiers={(NEATO_DOMAIN, self._robot_serial)})
|