Allow reading and writing device name
This commit is contained in:
parent
4dc42ee6f5
commit
6c0f1c3d13
2 changed files with 41 additions and 0 deletions
|
@ -20,3 +20,7 @@ if __name__ == "__main__":
|
|||
sleep(5)
|
||||
cc.load_enabled = False
|
||||
print(f"Load enabled: {cc.load_enabled}")
|
||||
|
||||
# print(f"Name: {cc.name}")
|
||||
# cc.name = "☀️ 🔌🔋Charger"
|
||||
# print(f"Name: {cc.name}")
|
||||
|
|
|
@ -229,6 +229,9 @@ def try_read_parse(
|
|||
class ChargeController:
|
||||
device: BaseInterface
|
||||
|
||||
manufacturer: str = "SRNE Solar Co., Ltd."
|
||||
manufacturer_id: str = "srne"
|
||||
|
||||
def __init__(self, device: BaseInterface):
|
||||
self.device = device
|
||||
|
||||
|
@ -282,6 +285,40 @@ class ChargeController:
|
|||
self._cached_version = f"{major}.{minor}.{patch}"
|
||||
return self._cached_version
|
||||
|
||||
_cached_name: str | None = None
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
if self._cached_name is not None:
|
||||
return self._cached_name
|
||||
data = readMemory(self.device, 0x0049, 16)
|
||||
if data is None:
|
||||
raise IOError
|
||||
res = data.decode("UTF-16BE").strip()
|
||||
return res
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str):
|
||||
bin_value = bytearray(value.encode("UTF-16BE"))
|
||||
if len(bin_value) > 32:
|
||||
raise ValueError(
|
||||
f"value must be no more than 32 bytes once encoded as UTF-16BE. {len(bin_value)} bytes supplied"
|
||||
)
|
||||
|
||||
# Pad name to 32 bytes to ensure ensure nothing is left of old name
|
||||
while len(bin_value) < 32:
|
||||
bin_value.extend(b"\x00\x20")
|
||||
print(len(bin_value), bin_value)
|
||||
|
||||
data = writeMemoryMultiple(self.device, 0x0049, bin_value)
|
||||
if data is None:
|
||||
raise IOError # FIXME: Raise specific error in readMemory
|
||||
|
||||
res = data.decode("UTF-16BE").strip()
|
||||
if res != value:
|
||||
log(f"setting device name failed; {res!r} != {value!r}")
|
||||
self._cached_name = value
|
||||
|
||||
@property
|
||||
def load_enabled(self) -> bool:
|
||||
data = readMemory(self.device, 0x010A, 1)
|
||||
|
|
Loading…
Reference in a new issue