|
| 1 | +"""Support for loading picture from Neato.""" |
| 2 | + |
| 3 | +from __future__ import annotations |
| 4 | + |
| 5 | +from datetime import timedelta |
| 6 | +import logging |
| 7 | +from typing import Any |
| 8 | + |
| 9 | +from pybotvac.exceptions import NeatoRobotException |
| 10 | +from pybotvac.robot import Robot |
| 11 | +from urllib3.response import HTTPResponse |
| 12 | + |
| 13 | +from homeassistant.components.camera import Camera |
| 14 | +from homeassistant.config_entries import ConfigEntry |
| 15 | +from homeassistant.core import HomeAssistant |
| 16 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 17 | + |
| 18 | +from .const import NEATO_LOGIN, NEATO_MAP_DATA, NEATO_ROBOTS, SCAN_INTERVAL_MINUTES |
| 19 | +from .entity import NeatoEntity |
| 20 | +from .hub import NeatoHub |
| 21 | + |
| 22 | +_LOGGER = logging.getLogger(__name__) |
| 23 | + |
| 24 | +SCAN_INTERVAL = timedelta(minutes=SCAN_INTERVAL_MINUTES) |
| 25 | +ATTR_GENERATED_AT = "generated_at" |
| 26 | + |
| 27 | + |
| 28 | +async def async_setup_entry( |
| 29 | + hass: HomeAssistant, |
| 30 | + entry: ConfigEntry, |
| 31 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 32 | +) -> None: |
| 33 | + """Set up Neato camera with config entry.""" |
| 34 | + neato: NeatoHub = hass.data[NEATO_LOGIN] |
| 35 | + mapdata: dict[str, Any] | None = hass.data.get(NEATO_MAP_DATA) |
| 36 | + dev = [ |
| 37 | + NeatoCleaningMap(neato, robot, mapdata) |
| 38 | + for robot in hass.data[NEATO_ROBOTS] |
| 39 | + if "maps" in robot.traits |
| 40 | + ] |
| 41 | + |
| 42 | + if not dev: |
| 43 | + return |
| 44 | + |
| 45 | + _LOGGER.debug("Adding robots for cleaning maps %s", dev) |
| 46 | + async_add_entities(dev, True) |
| 47 | + |
| 48 | + |
| 49 | +class NeatoCleaningMap(NeatoEntity, Camera): |
| 50 | + """Neato cleaning map for last clean.""" |
| 51 | + |
| 52 | + _attr_translation_key = "cleaning_map" |
| 53 | + |
| 54 | + def __init__( |
| 55 | + self, neato: NeatoHub, robot: Robot, mapdata: dict[str, Any] | None |
| 56 | + ) -> None: |
| 57 | + """Initialize Neato cleaning map.""" |
| 58 | + super().__init__(robot) |
| 59 | + Camera.__init__(self) |
| 60 | + self.neato = neato |
| 61 | + self._mapdata = mapdata |
| 62 | + self._available = neato is not None |
| 63 | + self._robot_serial: str = self.robot.serial |
| 64 | + self._attr_unique_id = self.robot.serial |
| 65 | + self._generated_at: str | None = None |
| 66 | + self._image_url: str | None = None |
| 67 | + self._image: bytes | None = None |
| 68 | + |
| 69 | + def camera_image( |
| 70 | + self, width: int | None = None, height: int | None = None |
| 71 | + ) -> bytes | None: |
| 72 | + """Return image response.""" |
| 73 | + self.update() |
| 74 | + return self._image |
| 75 | + |
| 76 | + def update(self) -> None: |
| 77 | + """Check the contents of the map list.""" |
| 78 | + |
| 79 | + _LOGGER.debug("Running camera update for '%s'", self.entity_id) |
| 80 | + try: |
| 81 | + self.neato.update_robots() |
| 82 | + except NeatoRobotException as ex: |
| 83 | + if self._available: # Print only once when available |
| 84 | + _LOGGER.error( |
| 85 | + "Neato camera connection error for '%s': %s", self.entity_id, ex |
| 86 | + ) |
| 87 | + self._image = None |
| 88 | + self._image_url = None |
| 89 | + self._available = False |
| 90 | + return |
| 91 | + |
| 92 | + if self._mapdata: |
| 93 | + map_data: dict[str, Any] = self._mapdata[self._robot_serial]["maps"][0] |
| 94 | + if (image_url := map_data["url"]) == self._image_url: |
| 95 | + _LOGGER.debug( |
| 96 | + "The map image_url for '%s' is the same as old", self.entity_id |
| 97 | + ) |
| 98 | + return |
| 99 | + |
| 100 | + try: |
| 101 | + image: HTTPResponse = self.neato.download_map(image_url) |
| 102 | + except NeatoRobotException as ex: |
| 103 | + if self._available: # Print only once when available |
| 104 | + _LOGGER.error( |
| 105 | + "Neato camera connection error for '%s': %s", self.entity_id, ex |
| 106 | + ) |
| 107 | + self._image = None |
| 108 | + self._image_url = None |
| 109 | + self._available = False |
| 110 | + return |
| 111 | + |
| 112 | + self._image = image.read() |
| 113 | + self._image_url = image_url |
| 114 | + self._generated_at = map_data.get("generated_at") |
| 115 | + self._available = True |
| 116 | + |
| 117 | + @property |
| 118 | + def available(self) -> bool: |
| 119 | + """Return if the robot is available.""" |
| 120 | + return self._available |
| 121 | + |
| 122 | + @property |
| 123 | + def extra_state_attributes(self) -> dict[str, Any]: |
| 124 | + """Return the state attributes of the vacuum cleaner.""" |
| 125 | + data: dict[str, Any] = {} |
| 126 | + |
| 127 | + if self._generated_at is not None: |
| 128 | + data[ATTR_GENERATED_AT] = self._generated_at |
| 129 | + |
| 130 | + return data |
0 commit comments