|
| 1 | +"""Support for Roborock image.""" |
| 2 | + |
| 3 | +from datetime import datetime |
| 4 | +import logging |
| 5 | + |
| 6 | +from homeassistant.components.image import ImageEntity |
| 7 | +from homeassistant.components.roborock.coordinator import RoborockDataUpdateCoordinator |
| 8 | +from homeassistant.components.roborock.entity import RoborockCoordinatedEntityV1 |
| 9 | +from homeassistant.config_entries import ConfigEntry |
| 10 | +from homeassistant.const import EntityCategory |
| 11 | +from homeassistant.core import HomeAssistant |
| 12 | +from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback |
| 13 | + |
| 14 | +_LOGGER = logging.getLogger(__name__) |
| 15 | + |
| 16 | +PARALLEL_UPDATES = 0 |
| 17 | + |
| 18 | + |
| 19 | +async def async_setup_entry( |
| 20 | + hass: HomeAssistant, |
| 21 | + config_entry, |
| 22 | + async_add_entities: AddConfigEntryEntitiesCallback, |
| 23 | +) -> None: |
| 24 | + """Set up Roborock image platform.""" |
| 25 | + |
| 26 | + async_add_entities( |
| 27 | + ( |
| 28 | + RoborockMap( |
| 29 | + config_entry, |
| 30 | + f"{coord.duid_slug}_custom_map_{map_info.name}", |
| 31 | + coord, |
| 32 | + map_info.flag, |
| 33 | + map_info.name, |
| 34 | + ) |
| 35 | + for coord in config_entry.runtime_data |
| 36 | + for map_info in coord.maps.values() |
| 37 | + ), |
| 38 | + ) |
| 39 | + |
| 40 | + |
| 41 | +class RoborockMap(RoborockCoordinatedEntityV1, ImageEntity): |
| 42 | + """A class to let you visualize the map.""" |
| 43 | + |
| 44 | + _attr_has_entity_name = True |
| 45 | + image_last_updated: datetime |
| 46 | + _attr_name: str |
| 47 | + |
| 48 | + def __init__( |
| 49 | + self, |
| 50 | + config_entry: ConfigEntry, |
| 51 | + unique_id: str, |
| 52 | + coordinator: RoborockDataUpdateCoordinator, |
| 53 | + map_flag: int, |
| 54 | + map_name: str, |
| 55 | + ) -> None: |
| 56 | + """Initialize a Roborock map.""" |
| 57 | + RoborockCoordinatedEntityV1.__init__(self, unique_id, coordinator) |
| 58 | + ImageEntity.__init__(self, coordinator.hass) |
| 59 | + self.config_entry = config_entry |
| 60 | + self._attr_name = map_name + "_custom" |
| 61 | + self.map_flag = map_flag |
| 62 | + self.cached_map = b"" |
| 63 | + self._attr_entity_category = EntityCategory.DIAGNOSTIC |
| 64 | + |
| 65 | + @property |
| 66 | + def is_selected(self) -> bool: |
| 67 | + """Return if this map is the currently selected map.""" |
| 68 | + return self.map_flag == self.coordinator.current_map |
| 69 | + |
| 70 | + async def async_added_to_hass(self) -> None: |
| 71 | + """When entity is added to hass load any previously cached maps from disk.""" |
| 72 | + await super().async_added_to_hass() |
| 73 | + self._attr_image_last_updated = self.coordinator.maps[ |
| 74 | + self.map_flag |
| 75 | + ].last_updated |
| 76 | + self.async_write_ha_state() |
| 77 | + |
| 78 | + def _handle_coordinator_update(self) -> None: |
| 79 | + # If the coordinator has updated the map, we can update the image. |
| 80 | + self._attr_image_last_updated = self.coordinator.maps[ |
| 81 | + self.map_flag |
| 82 | + ].last_updated |
| 83 | + |
| 84 | + super()._handle_coordinator_update() |
| 85 | + |
| 86 | + async def async_image(self) -> bytes | None: |
| 87 | + """Get the cached image.""" |
| 88 | + return self.coordinator.maps[self.map_flag].image |
| 89 | + |
| 90 | + @property |
| 91 | + def extra_state_attributes(self): |
| 92 | + map_data = self.coordinator.maps[self.map_flag].map_data |
| 93 | + if map_data is None: |
| 94 | + return {} |
| 95 | + for room in map_data.rooms.values(): |
| 96 | + room.name = self.coordinator.maps[self.map_flag].rooms.get(room.number) |
| 97 | + |
| 98 | + return { |
| 99 | + "calibration_points": self.coordinator.maps[ |
| 100 | + self.map_flag |
| 101 | + ].map_data.calibration(), |
| 102 | + "rooms": map_data.rooms, |
| 103 | + "zones": map_data.zones, |
| 104 | + } |
0 commit comments