forked from pschmitt/roombapy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Properly handle unknown broadcast messages (pschmitt#231)
* Properly handle unknown broadcast messages * Fix typing for capabilities * Add review suggestions
- Loading branch information
Showing
5 changed files
with
227 additions
and
61 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,40 +1,38 @@ | ||
class RoombaInfo: | ||
hostname = None | ||
firmware = None | ||
ip = None | ||
mac = None | ||
robot_name = None | ||
sku = None | ||
capabilities = None | ||
blid = None | ||
password = None | ||
from functools import cached_property | ||
from typing import Dict, Optional | ||
|
||
def __init__( | ||
self, hostname, robot_name, ip, mac, firmware, sku, capabilities | ||
): | ||
"""Create object with information about roomba.""" | ||
self.hostname = hostname | ||
self.firmware = firmware | ||
self.ip = ip | ||
self.mac = mac | ||
self.robot_name = robot_name | ||
self.sku = sku | ||
self.capabilities = capabilities | ||
self.blid = hostname.split("-")[1] | ||
from pydantic import BaseModel, Field, computed_field, field_validator | ||
|
||
def __str__(self) -> str: | ||
"""Nice output to console.""" | ||
return ", ".join( | ||
[ | ||
"{key}={value}".format(key=key, value=self.__dict__.get(key)) | ||
for key in self.__dict__ | ||
] | ||
) | ||
|
||
class RoombaInfo(BaseModel): | ||
hostname: str | ||
firmware: str = Field(alias="sw") | ||
ip: str | ||
mac: str | ||
robot_name: str = Field(alias="robotname") | ||
sku: str | ||
capabilities: Dict[str, int] = Field(alias="cap") | ||
password: Optional[str] = None | ||
|
||
@field_validator("hostname") | ||
@classmethod | ||
def hostname_validator(cls, value: str) -> str: | ||
if "-" not in value: | ||
raise ValueError(f"hostname does not contain a dash: {value}") | ||
model_name, blid = value.split("-") | ||
if blid == "": | ||
raise ValueError(f"empty blid: {value}") | ||
if model_name.lower() not in {"roomba", "irobot"}: | ||
raise ValueError(f"unsupported model in hostname: {value}") | ||
return value | ||
|
||
@computed_field | ||
@cached_property | ||
def blid(self) -> str: | ||
return self.hostname.split("-")[1] | ||
|
||
def __hash__(self) -> int: | ||
"""Hashcode.""" | ||
return hash(self.mac) | ||
|
||
def __eq__(self, o: object) -> bool: | ||
"""Equals.""" | ||
return isinstance(o, RoombaInfo) and self.mac == o.mac |
Oops, something went wrong.