Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cli): discover must return a list #266

Merged
merged 7 commits into from
Aug 10, 2024
Merged
Changes from 3 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
24 changes: 13 additions & 11 deletions midealocal/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,14 @@ async def _get_keys(self, device_id: int) -> dict[int, dict[str, Any]]:

return {**cloud_keys, **default_keys}

async def discover(self) -> MideaDevice | None:
async def discover(self) -> list[MideaDevice]:
"""Discover device information."""
devices = discover(ip_address=self.namespace.host)

device_list: list[MideaDevice] = []
if len(devices) == 0:
_LOGGER.error("No devices found.")
return None
return device_list

# Dump only basic device info from the base class
_LOGGER.info("Found %d devices.", len(devices))
Expand Down Expand Up @@ -99,10 +100,11 @@ async def discover(self) -> MideaDevice | None:
_LOGGER.debug("Trying to connect with key: %s", key)
if dev.connect():
_LOGGER.info("Found device:\n%s", dev.attributes)
return dev
device_list.append(dev)
break

_LOGGER.debug("Unable to connect with key: %s", key)
return None
return device_list

def message(self) -> None:
"""Load message into device."""
Expand Down Expand Up @@ -168,23 +170,23 @@ async def download(self) -> None:

async def set_attribute(self) -> None:
"""Set attribute for device."""
device = await self.discover()
if device is None:
device_list = await self.discover()
if len(device_list) != 1:
return

_LOGGER.info(
"Setting attribute %s for %s [%s]",
self.namespace.attribute,
device.device_id,
device.device_type,
device_list[0].device_id,
device_list[0].device_type,
)
device.set_attribute(
device_list[0].set_attribute(
self.namespace.attribute,
self._cast_attr_value(),
)
await asyncio.sleep(2)
device.refresh_status(True)
_LOGGER.info("New device status:\n%s", device.attributes)
device_list[0].refresh_status(True)
_LOGGER.info("New device status:\n%s", device_list[0].attributes)

def _cast_attr_value(self) -> int | bool | str:
if self.namespace.attr_type == "bool":
Expand Down