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 ![]()
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
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)
)