From 0dfc85e03ae216db48a420402c28c9550f886d6c Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 16 Dec 2022 23:36:59 +0100 Subject: [PATCH 1/2] Poc for maps --- custom_components/purei9/__init__.py | 2 +- custom_components/purei9/camera.py | 69 ++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 custom_components/purei9/camera.py diff --git a/custom_components/purei9/__init__.py b/custom_components/purei9/__init__.py index 690587c..36e19d8 100644 --- a/custom_components/purei9/__init__.py +++ b/custom_components/purei9/__init__.py @@ -4,7 +4,7 @@ from purei9_unofficial.cloudv3 import CloudClient from . import const, coordinator -PLATFORMS = ["vacuum", "sensor"] +PLATFORMS = ["vacuum", "sensor", "camera"] async def async_setup_entry(hass, config_entry) -> bool: """Setup the integration after the config flow""" diff --git a/custom_components/purei9/camera.py b/custom_components/purei9/camera.py new file mode 100644 index 0000000..72e4a4c --- /dev/null +++ b/custom_components/purei9/camera.py @@ -0,0 +1,69 @@ +"""Home Assistant camera entity""" +from homeassistant.components.camera import Camera +from purei9_unofficial.cloud import CloudMap +from . import purei9, const + +_LOGGER = logging.getLogger(__name__) + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Initial setup for the workers. Download and identify all workers.""" + data = hass.data[const.DOMAIN][config_entry.entry_id] + + entities = [] + + for coord in data[const.COORDINATORS]: + robot_name = await hass.async_add_executor_job(coord.robot.getname) + robot_id = await hass.async_add_executor_job(coord.robot.getid) + maps = await hass.async_add_executor_job(coord.robot.getMaps) + + for map in maps: + entities.append( + PureI9Map(map, robot_id, robot_name, map.image) + ) + + async_add_entities(entities) + +class PureI9Map(Camera): + """Map displaying the vacuums cleaning areas""" + def __init__( + self, + map: CloudMap, + unique_id, + name, + image + ): + super().__init__() + self._map = map + self._unique_id = unique_id + self._name = name + self._image = image + + @property + def device_info(self): + """Return information for the device registry""" + # See: https://developers.home-assistant.io/docs/device_registry_index/ + return { + "identifiers": {(const.DOMAIN, self._unique_id)}, + "name": self._name, + "manufacturer": const.MANUFACTURER, + # We don't know the exact model, i.e. Pure i9 or Pure i9.2, + # so only report a default model + "default_model": const.MODEL + } + + @property + def unique_id(self) -> str: + """Unique identifier to the vacuum that this map belongs to""" + return self._unique_id + + @property + def name(self) -> str: + """Name of the vacuum that this map belongs to""" + return self._name + + def camera_image(self, width = None, height = None): + return self._image + + def update(self) -> None: + self._map.get() + self._image = self._map.image From 398768c755f5eb1f1556df3dcad953776f7ddd7b Mon Sep 17 00:00:00 2001 From: Niklas Ekman Date: Fri, 16 Dec 2022 23:47:03 +0100 Subject: [PATCH 2/2] fix --- custom_components/purei9/camera.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/custom_components/purei9/camera.py b/custom_components/purei9/camera.py index 72e4a4c..ecbd703 100644 --- a/custom_components/purei9/camera.py +++ b/custom_components/purei9/camera.py @@ -1,7 +1,8 @@ """Home Assistant camera entity""" +import logging from homeassistant.components.camera import Camera from purei9_unofficial.cloud import CloudMap -from . import purei9, const +from . import const _LOGGER = logging.getLogger(__name__) @@ -12,14 +13,11 @@ async def async_setup_entry(hass, config_entry, async_add_entities): entities = [] for coord in data[const.COORDINATORS]: - robot_name = await hass.async_add_executor_job(coord.robot.getname) robot_id = await hass.async_add_executor_job(coord.robot.getid) maps = await hass.async_add_executor_job(coord.robot.getMaps) for map in maps: - entities.append( - PureI9Map(map, robot_id, robot_name, map.image) - ) + entities.append(PureI9Map.create(robot_id, map)) async_add_entities(entities) @@ -27,23 +25,32 @@ class PureI9Map(Camera): """Map displaying the vacuums cleaning areas""" def __init__( self, + robot_id, map: CloudMap, unique_id, name, image ): - super().__init__() + self._robot_id = robot_id self._map = map self._unique_id = unique_id self._name = name self._image = image + @staticmethod + def create(robot_id: str, map: CloudMap): + """Named constructor for creating a new instance from a CloudMap""" + return PureI9Map(robot_id, map, map.id, map.name, map.image) + @property def device_info(self): """Return information for the device registry""" # See: https://developers.home-assistant.io/docs/device_registry_index/ return { - "identifiers": {(const.DOMAIN, self._unique_id)}, + "identifiers": { + (const.DOMAIN, self._robot_id), + (const.DOMAIN, self._unique_id) + }, "name": self._name, "manufacturer": const.MANUFACTURER, # We don't know the exact model, i.e. Pure i9 or Pure i9.2, @@ -64,6 +71,6 @@ def name(self) -> str: def camera_image(self, width = None, height = None): return self._image - def update(self) -> None: - self._map.get() + async def async_update(self): + await self.hass.async_add_executor_job(self._map.get) self._image = self._map.image