Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions switchbot/devices/bulb.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@

_LOGGER = logging.getLogger(__name__)

from .device import ColorMode
from .base_light import SwitchbotBaseLight
from .device import ColorMode


class SwitchbotBulb(SwitchbotBaseLight):
Expand Down Expand Up @@ -88,9 +88,9 @@ async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
self._update_state(result)
return self._check_command_result(result, 1, {0x80})

def _update_state(self, result: bytes) -> None:
def _update_state(self, result: bytes | None) -> None:
"""Update device state."""
if len(result) < 10:
if not result or len(result) < 10:
return
self._state["r"] = result[3]
self._state["g"] = result[4]
Expand Down
7 changes: 7 additions & 0 deletions switchbot/devices/curtain.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,10 @@ async def get_extended_info_summary(self) -> dict[str, Any] | None:
"""Get basic info for all devices in chain."""
_data = await self._sendcommand(key=CURTAIN_EXT_SUM_KEY)

if not _data:
_LOGGER.error("%s: Unsuccessful, no result from device", self.name)
return None

if _data in (b"\x07", b"\x00"):
_LOGGER.error("%s: Unsuccessful, please try again", self.name)
return None
Expand Down Expand Up @@ -127,6 +131,9 @@ async def get_extended_info_adv(self) -> dict[str, Any] | None:
"""Get advance page info for device chain."""

_data = await self._sendcommand(key=CURTAIN_EXT_ADV_KEY)
if not _data:
_LOGGER.error("%s: Unsuccessful, no result from device", self.name)
return None

if _data in (b"\x07", b"\x00"):
_LOGGER.error("%s: Unsuccessful, please try again", self.name)
Expand Down
12 changes: 6 additions & 6 deletions switchbot/devices/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def _commandkey(self, key: str) -> str:
key_suffix = key[4:]
return KEY_PASSWORD_PREFIX + key_action + self._password_encoded + key_suffix

async def _sendcommand(self, key: str, retry: int | None = None) -> bytes:
async def _sendcommand(self, key: str, retry: int | None = None) -> bytes | None:
"""Send command to device and read response."""
if retry is None:
retry = self._retry_count
Expand Down Expand Up @@ -145,7 +145,7 @@ async def _sendcommand(self, key: str, retry: int | None = None) -> bytes:
self.rssi,
exc_info=True,
)
return b"\x00"
return None
except CharacteristicMissingError as ex:
if attempt == retry:
_LOGGER.error(
Expand All @@ -155,7 +155,7 @@ async def _sendcommand(self, key: str, retry: int | None = None) -> bytes:
self.rssi,
exc_info=True,
)
return b"\x00"
return None

_LOGGER.debug(
"%s: characteristic missing: %s; RSSI: %s",
Expand All @@ -172,7 +172,7 @@ async def _sendcommand(self, key: str, retry: int | None = None) -> bytes:
self.rssi,
exc_info=True,
)
return b"\x00"
return None

_LOGGER.debug(
"%s: communication failed with:", self.name, exc_info=True
Expand Down Expand Up @@ -411,10 +411,10 @@ async def update(self) -> None:
"""Update state of device."""

def _check_command_result(
self, result: bytes, index: int, values: set[int]
self, result: bytes | None, index: int, values: set[int]
) -> bool:
"""Check command result."""
if not result:
if not result or len(result) - 1 < index:
raise SwitchbotOperationError(
f"{self.name}: Sending command failed (rssi={self.rssi})"
)
Expand Down
4 changes: 2 additions & 2 deletions switchbot/devices/light_strip.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ async def set_rgb(self, brightness: int, r: int, g: int, b: int) -> bool:
self._update_state(result)
return self._check_command_result(result, 1, {0x80})

def _update_state(self, result: bytes) -> None:
def _update_state(self, result: bytes | None) -> None:
"""Update device state."""
if len(result) < 10:
if not result or len(result) < 10:
return
self._state["r"] = result[3]
self._state["g"] = result[4]
Expand Down