Refresh coordinator data on request

Hello,
i'm trying to get to the world of config_flow an old integration (it's still the max integration https://github.com/MassiPi/maxcube ).
I'd like to get it to be configured via GUI, possibility to create devices above entities and simply get modern :slight_smile:
and i almost did it! Where i'm stuck: for how the cube works, when you send a command (to change temperature, for example) you need to manually refresh data after a couple of seconds to get it updated. In the "old" integration everything is blocking and it's easy to achieve. In the new one, it seems i can't sort out how.
But to be honest, it seems to update data every minute also if i put a update_interval at 30 seconds. So i'm probably missing something "basic". Or, on the other and, i'm probably making something wrong in code :slight_smile: here is the coordinator.py code
Thanks for any idea

class MaxCoordinator(DataUpdateCoordinator):
    def __init__(self, hass: HomeAssistant, entry: ConfigEntry):
        super().__init__(
            hass,
            _LOGGER,
            name=DOMAIN,
            update_interval=timedelta(seconds=30)
        )
        self.entry = entry
        self.host = entry.data[CONF_HOST]
        self.port = entry.data[CONF_PORT]
        self.mutex = Lock()

        self.cube = MaxCube(self.host, self.port, now=now)
        self.cube.use_persistent_connection = True
        
    async def _async_update_data(self):
        """Fetch data from MaxCube."""
        try:
            # MaxCube library is blocking → run in executor
            return await self.hass.async_add_executor_job(
                self._safe_update
            )

        except timeout as err:
            raise UpdateFailed(f"MaxCube connection failed: {err}") from err
            
    def _safe_update(self):
        """Blocking update wrapped for executor."""
        self.cube.update()

        devices_by_room = {}

        for d in self.cube.devices:
            devices_by_room.setdefault(d.room_id, []).append(d)

        self._build_window_open_fix(devices_by_room)
        self._update_cube_derived_state()
        return {
            "cube": self.cube,
            "devices": self.cube.devices,
            "rooms_by_id": {r.id: r for r in self.cube.rooms},
            "devices_by_room": devices_by_room
        }
        
    async def async_close(self):
        await self.hass.async_add_executor_job(
            self.cube.disconnect()
        )        

    async def async_set_temperature(self, device, temp, mode):
        """Write + forced refresh for MAX cube latency."""
        def _write():
            with self.mutex:
                try:
                    self.cube.set_temperature_mode(device, temp, mode)
                except (socket.timeout, OSError):
                    _LOGGER.error("Setting HVAC mode failed")

        await self.hass.async_add_executor_job(_write)

        await asyncio.sleep(5)
        await self.async_request_refresh()

    def set_temperature(self, device, temp, mode):
        self.hass.async_create_task(
            self.async_set_temperature(device, temp, mode)
        )

You can request an update of your coordinator with

await coordinator.async_request_refresh()

that's also what i expected, but it seems not to work (it's already in the quoted code..)
do you see any big mistake in the structure of the coordinator? the update_interval is correctly specified, isn't it?
it really seems i can't get a refresh faster than once a minute..
thanks!

ok i think i sorted it out and i'd like to leave a trace to help other first-try-coders :rofl:
in my case this was a porting to the "modern world" of old integration, and i left the "should poll" attribute in underlying entities. This causes the entities to have another separate update interval. Also, i just forgot to super().init(coordinator) so totally my fault :rofl:
Now, next issue is about naming and unique id of entities, that it seems i can't manage

I did this a little while ago now but if it helps you.

1 Like

loving this, thank you!
i'm probably missing something structural, but let's see what can i get :slight_smile:

for example, my level of ignorance is that i'm developing on my official home assistant and i had no idea of the dev container existence O_O
(i probably have tons of garbage in my db..)