diff --git a/custom_components/roborock_custom_map/image.py b/custom_components/roborock_custom_map/image.py index 051dd0c..a43b917 100644 --- a/custom_components/roborock_custom_map/image.py +++ b/custom_components/roborock_custom_map/image.py @@ -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 @@ -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: @@ -145,7 +146,7 @@ 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, ) ) @@ -153,6 +154,12 @@ async def async_added_to_hass(self) -> None: 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() @@ -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: diff --git a/custom_components/roborock_custom_map/select.py b/custom_components/roborock_custom_map/select.py index e383e53..a28d3ba 100644 --- a/custom_components/roborock_custom_map/select.py +++ b/custom_components/roborock_custom_map/select.py @@ -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 @@ -22,8 +20,6 @@ SIGNAL_ROTATION_CHANGED, ) -_LOGGER = logging.getLogger(__name__) - PARALLEL_UPDATES = 0 @@ -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, @@ -66,6 +63,7 @@ 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}" @@ -73,7 +71,6 @@ def __init__( 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.""" @@ -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() @@ -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()