2019-02-14 04:35:12 +00:00
|
|
|
"""Support for loading picture from Neato."""
|
2021-08-08 13:02:37 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2019-03-21 05:56:46 +00:00
|
|
|
from datetime import timedelta
|
2017-04-13 14:41:25 +00:00
|
|
|
import logging
|
2021-08-08 13:02:37 +00:00
|
|
|
from typing import Any
|
2017-04-13 14:41:25 +00:00
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
from pybotvac.exceptions import NeatoRobotException
|
2021-08-08 13:02:37 +00:00
|
|
|
from pybotvac.robot import Robot
|
|
|
|
from urllib3.response import HTTPResponse
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2017-04-13 14:41:25 +00:00
|
|
|
from homeassistant.components.camera import Camera
|
2021-08-08 13:02:37 +00:00
|
|
|
from homeassistant.components.neato import NeatoHub
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2019-03-21 05:56:46 +00:00
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
from .const import (
|
|
|
|
NEATO_DOMAIN,
|
2019-10-10 06:08:11 +00:00
|
|
|
NEATO_LOGIN,
|
2019-10-06 18:10:11 +00:00
|
|
|
NEATO_MAP_DATA,
|
|
|
|
NEATO_ROBOTS,
|
|
|
|
SCAN_INTERVAL_MINUTES,
|
|
|
|
)
|
2017-04-13 14:41:25 +00:00
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
2019-10-06 18:10:11 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES)
|
2019-10-07 06:30:49 +00:00
|
|
|
ATTR_GENERATED_AT = "generated_at"
|
2018-06-27 20:55:27 +00:00
|
|
|
|
2017-04-13 14:41:25 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
2019-10-06 11:05:51 +00:00
|
|
|
"""Set up Neato camera with config entry."""
|
2017-04-13 14:41:25 +00:00
|
|
|
dev = []
|
2021-08-08 13:02:37 +00:00
|
|
|
neato: NeatoHub = hass.data[NEATO_LOGIN]
|
|
|
|
mapdata: dict[str, Any] | None = hass.data.get(NEATO_MAP_DATA)
|
2017-04-13 14:41:25 +00:00
|
|
|
for robot in hass.data[NEATO_ROBOTS]:
|
2019-07-31 19:25:30 +00:00
|
|
|
if "maps" in robot.traits:
|
2019-10-07 19:49:54 +00:00
|
|
|
dev.append(NeatoCleaningMap(neato, robot, mapdata))
|
2019-10-06 11:05:51 +00:00
|
|
|
|
|
|
|
if not dev:
|
|
|
|
return
|
|
|
|
|
2017-04-30 05:04:49 +00:00
|
|
|
_LOGGER.debug("Adding robots for cleaning maps %s", dev)
|
2019-10-06 11:05:51 +00:00
|
|
|
async_add_entities(dev, True)
|
2017-04-13 14:41:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
class NeatoCleaningMap(Camera):
|
|
|
|
"""Neato cleaning map for last clean."""
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def __init__(
|
|
|
|
self, neato: NeatoHub, robot: Robot, mapdata: dict[str, Any] | None
|
|
|
|
) -> None:
|
2017-04-13 14:41:25 +00:00
|
|
|
"""Initialize Neato cleaning map."""
|
|
|
|
super().__init__()
|
|
|
|
self.robot = robot
|
2019-10-07 19:49:54 +00:00
|
|
|
self.neato = neato
|
|
|
|
self._mapdata = mapdata
|
2020-12-16 22:39:41 +00:00
|
|
|
self._available = neato is not None
|
2019-10-06 18:10:11 +00:00
|
|
|
self._robot_name = f"{self.robot.name} Cleaning Map"
|
2021-08-08 13:02:37 +00:00
|
|
|
self._robot_serial: str = self.robot.serial
|
|
|
|
self._generated_at: str | None = None
|
|
|
|
self._image_url: str | None = None
|
|
|
|
self._image: bytes | None = None
|
2017-04-13 14:41:25 +00:00
|
|
|
|
2021-08-11 00:33:06 +00:00
|
|
|
def camera_image(
|
|
|
|
self, width: int | None = None, height: int | None = None
|
|
|
|
) -> bytes | None:
|
2017-04-13 14:41:25 +00:00
|
|
|
"""Return image response."""
|
|
|
|
self.update()
|
|
|
|
return self._image
|
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
def update(self) -> None:
|
2017-04-13 14:41:25 +00:00
|
|
|
"""Check the contents of the map list."""
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.debug("Running camera update for '%s'", self.entity_id)
|
2019-10-06 18:10:11 +00:00
|
|
|
try:
|
|
|
|
self.neato.update_robots()
|
2019-10-07 19:49:54 +00:00
|
|
|
except NeatoRobotException as ex:
|
|
|
|
if self._available: # Print only once when available
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato camera connection error for '%s': %s", self.entity_id, ex
|
|
|
|
)
|
2019-10-07 19:49:54 +00:00
|
|
|
self._image = None
|
|
|
|
self._image_url = None
|
|
|
|
self._available = False
|
|
|
|
return
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2021-08-08 13:02:37 +00:00
|
|
|
if self._mapdata:
|
|
|
|
map_data: dict[str, Any] = self._mapdata[self._robot_serial]["maps"][0]
|
2021-10-22 09:34:45 +00:00
|
|
|
if (image_url := map_data["url"]) == self._image_url:
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.debug(
|
|
|
|
"The map image_url for '%s' is the same as old", self.entity_id
|
|
|
|
)
|
2019-10-07 19:49:54 +00:00
|
|
|
return
|
2019-10-06 18:10:11 +00:00
|
|
|
|
2019-10-07 19:49:54 +00:00
|
|
|
try:
|
2021-08-08 13:02:37 +00:00
|
|
|
image: HTTPResponse = self.neato.download_map(image_url)
|
2019-10-06 18:10:11 +00:00
|
|
|
except NeatoRobotException as ex:
|
2019-10-07 06:30:49 +00:00
|
|
|
if self._available: # Print only once when available
|
2020-07-15 16:26:57 +00:00
|
|
|
_LOGGER.error(
|
|
|
|
"Neato camera connection error for '%s': %s", self.entity_id, ex
|
|
|
|
)
|
2019-10-06 18:10:11 +00:00
|
|
|
self._image = None
|
|
|
|
self._image_url = None
|
2019-10-07 06:30:49 +00:00
|
|
|
self._available = False
|
2019-10-07 19:49:54 +00:00
|
|
|
return
|
|
|
|
|
|
|
|
self._image = image.read()
|
|
|
|
self._image_url = image_url
|
2021-08-08 13:02:37 +00:00
|
|
|
self._generated_at = map_data.get("generated_at")
|
2019-10-07 19:49:54 +00:00
|
|
|
self._available = True
|
2017-04-13 14:41:25 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def name(self) -> str:
|
2017-04-13 14:41:25 +00:00
|
|
|
"""Return the name of this camera."""
|
|
|
|
return self._robot_name
|
2018-10-12 22:33:13 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def unique_id(self) -> str:
|
2018-10-12 22:33:13 +00:00
|
|
|
"""Return unique ID."""
|
|
|
|
return self._robot_serial
|
2019-10-06 11:05:51 +00:00
|
|
|
|
2019-10-07 06:30:49 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def available(self) -> bool:
|
2019-10-07 06:30:49 +00:00
|
|
|
"""Return if the robot is available."""
|
|
|
|
return self._available
|
|
|
|
|
2019-10-06 11:05:51 +00:00
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def device_info(self) -> DeviceInfo:
|
2019-10-06 11:05:51 +00:00
|
|
|
"""Device info for neato robot."""
|
2021-10-24 09:34:45 +00:00
|
|
|
return DeviceInfo(identifiers={(NEATO_DOMAIN, self._robot_serial)})
|
2019-10-07 06:30:49 +00:00
|
|
|
|
|
|
|
@property
|
2021-08-08 13:02:37 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2019-10-07 06:30:49 +00:00
|
|
|
"""Return the state attributes of the vacuum cleaner."""
|
2021-08-08 13:02:37 +00:00
|
|
|
data: dict[str, Any] = {}
|
2019-10-07 06:30:49 +00:00
|
|
|
|
|
|
|
if self._generated_at is not None:
|
|
|
|
data[ATTR_GENERATED_AT] = self._generated_at
|
|
|
|
|
|
|
|
return data
|