Skip to content
Open
13 changes: 10 additions & 3 deletions custom_components/roborock_custom_map/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
from homeassistant.components.roborock.entity import RoborockCoordinatedEntityV1
from homeassistant.config_entries import ConfigEntry
from homeassistant.const import EntityCategory
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.exceptions import HomeAssistantError
from homeassistant.helpers.dispatcher import async_dispatcher_connect
from homeassistant.helpers.entity_platform import AddConfigEntryEntitiesCallback
Expand Down Expand Up @@ -111,6 +111,7 @@ def __init__(

self.config_entry = config_entry
self.map_flag = map_flag
self.rotation_key = f"{coordinator.duid_slug}_{map_flag}"
self._home_trait = home_trait

if not map_name:
Expand Down Expand Up @@ -145,14 +146,20 @@ async def async_added_to_hass(self) -> None:
self.async_on_remove(
async_dispatcher_connect(
self.hass,
f"{SIGNAL_ROTATION_CHANGED}_{self.config_entry.entry_id}_{self.map_flag}",
f"{SIGNAL_ROTATION_CHANGED}_{self.config_entry.entry_id}_{self.rotation_key}",
self._handle_rotation_changed,
)
)

self.async_write_ha_state()

def _handle_rotation_changed(self) -> None:
"""Rotation changed; schedule state update in the event loop."""
self.hass.loop.call_soon_threadsafe(self._async_handle_rotation_changed)


@callback
def _async_handle_rotation_changed(self) -> None:
"""Rotation changed; bump last_updated to bust the image cache."""
self._attr_image_last_updated = dt_util.utcnow()
self.async_write_ha_state()
Expand Down Expand Up @@ -184,7 +191,7 @@ def _get_rotation(self) -> int:
self.hass.data.get(DOMAIN, {})
.get(self.config_entry.entry_id, {})
.get(CONF_MAP_ROTATION, {})
.get(self.map_flag, DEFAULT_MAP_ROTATION)
.get(self.rotation_key, DEFAULT_MAP_ROTATION)
)

if rotation not in MAP_ROTATION_OPTIONS:
Expand Down
13 changes: 5 additions & 8 deletions custom_components/roborock_custom_map/select.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

from __future__ import annotations

import logging

from homeassistant.components.roborock.coordinator import RoborockDataUpdateCoordinator
from homeassistant.components.roborock.entity import RoborockCoordinatedEntityV1
from homeassistant.components.select import SelectEntity
Expand All @@ -22,8 +20,6 @@
SIGNAL_ROTATION_CHANGED,
)

_LOGGER = logging.getLogger(__name__)

PARALLEL_UPDATES = 0


Expand Down Expand Up @@ -52,6 +48,7 @@ class RoborockMapRotationSelect(RoborockCoordinatedEntityV1, RestoreEntity, Sele

_attr_has_entity_name = True
_attr_entity_category = EntityCategory.CONFIG
_attr_translation_key = "rotation"

def __init__(
self,
Expand All @@ -66,14 +63,14 @@ def __init__(

self.config_entry = config_entry
self.map_flag = map_flag
self.rotation_key = f"{coordinator.duid_slug}_{map_flag}"

if not map_name:
map_name = f"Map {map_flag}"

self._attr_name = f"{map_name} rotation"
self._attr_options = [str(v) for v in MAP_ROTATION_OPTIONS]
self._attr_current_option = str(DEFAULT_MAP_ROTATION)
self._attr_translation_key = "rotation"

async def async_added_to_hass(self) -> None:
"""Restore previous rotation setting and store in hass.data."""
Expand All @@ -85,7 +82,7 @@ async def async_added_to_hass(self) -> None:

# Persist selection for the image entity to read
self.hass.data[DOMAIN][self.config_entry.entry_id][CONF_MAP_ROTATION][
self.map_flag
self.rotation_key
] = int(self._attr_current_option)

self.async_write_ha_state()
Expand All @@ -98,13 +95,13 @@ async def async_select_option(self, option: str) -> None:
self._attr_current_option = option

self.hass.data[DOMAIN][self.config_entry.entry_id][CONF_MAP_ROTATION][
self.map_flag
self.rotation_key
] = int(option)

# Notify the image entity to bust the cache via image_last_updated bump
async_dispatcher_send(
self.hass,
f"{SIGNAL_ROTATION_CHANGED}_{self.config_entry.entry_id}_{self.map_flag}",
f"{SIGNAL_ROTATION_CHANGED}_{self.config_entry.entry_id}_{self.rotation_key}",
)

self.async_write_ha_state()
Loading