Skip to content

Commit 4f0842f

Browse files
committed
ISSUE-4 Retry getting device info if failed. Do not cache unknown
1 parent 67bcfe4 commit 4f0842f

1 file changed

Lines changed: 51 additions & 48 deletions

File tree

  • custom_components/ipixel_color

custom_components/ipixel_color/api.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -80,56 +80,59 @@ async def get_device_info(self) -> dict[str, Any] | None:
8080
"""Query device information and store it."""
8181
if self._device_info is not None:
8282
return self._device_info
83-
84-
try:
85-
command = build_device_info_command()
86-
87-
# Set up notification response
88-
self._device_response = None
89-
response_received = asyncio.Event()
90-
91-
def response_handler(sender: Any, data: bytearray) -> None:
92-
self._device_response = bytes(data)
93-
response_received.set()
94-
95-
# Enable notifications temporarily
96-
await self._bluetooth._client.start_notify(
97-
"0000fa03-0000-1000-8000-00805f9b34fb", response_handler
98-
)
99-
83+
84+
for attempt in range(1, 4):
10085
try:
101-
# Send command
102-
await self._bluetooth._client.write_gatt_char(
103-
"0000fa02-0000-1000-8000-00805f9b34fb", command
104-
)
105-
106-
# Wait for response (5 second timeout)
107-
await asyncio.wait_for(response_received.wait(), timeout=5.0)
108-
109-
if self._device_response:
110-
self._device_info = parse_device_response(self._device_response)
111-
else:
112-
raise Exception("No response received")
113-
114-
finally:
115-
await self._bluetooth._client.stop_notify(
116-
"0000fa03-0000-1000-8000-00805f9b34fb"
86+
command = build_device_info_command()
87+
88+
# Set up notification response
89+
self._device_response = None
90+
response_received = asyncio.Event()
91+
92+
def response_handler(sender: Any, data: bytearray) -> None:
93+
self._device_response = bytes(data)
94+
response_received.set()
95+
96+
# Enable notifications temporarily
97+
await self._bluetooth._client.start_notify(
98+
"0000fa03-0000-1000-8000-00805f9b34fb", response_handler
11799
)
118-
119-
_LOGGER.info("Device info retrieved: %s", self._device_info)
120-
return self._device_info
121-
122-
except Exception as err:
123-
_LOGGER.error("Failed to get device info: %s", err)
124-
# Return default values
125-
self._device_info = {
126-
"width": 64,
127-
"height": 16,
128-
"device_type": "Unknown",
129-
"mcu_version": "Unknown",
130-
"wifi_version": "Unknown"
131-
}
132-
return self._device_info
100+
101+
try:
102+
# Send command
103+
await self._bluetooth._client.write_gatt_char(
104+
"0000fa02-0000-1000-8000-00805f9b34fb", command
105+
)
106+
107+
# Wait for response (10 second timeout)
108+
await asyncio.wait_for(response_received.wait(), timeout=10.0)
109+
110+
if self._device_response:
111+
self._device_info = parse_device_response(self._device_response)
112+
else:
113+
raise Exception("No response received")
114+
115+
finally:
116+
await self._bluetooth._client.stop_notify(
117+
"0000fa03-0000-1000-8000-00805f9b34fb"
118+
)
119+
120+
_LOGGER.info("Device info retrieved: %s", self._device_info)
121+
return self._device_info
122+
123+
except Exception as err:
124+
_LOGGER.warning("Failed to get device info (attempt %d/3): %s", attempt, err)
125+
if attempt < 3:
126+
await asyncio.sleep(2)
127+
# All attempts failed - return default values (don't cache to allow retries)
128+
_LOGGER.error("Failed to get device info after 3 attempts")
129+
return {
130+
"width": 64,
131+
"height": 16,
132+
"device_type": "Unknown",
133+
"mcu_version": "Unknown",
134+
"wifi_version": "Unknown"
135+
}
133136

134137
async def display_text(self, text: str, antialias: bool = True, font_size: float | None = None, font: str | None = None, line_spacing: int = 0) -> bool:
135138
"""Display text as image using PIL.

0 commit comments

Comments
 (0)