-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #69 from ivanfmartinez/juicebox_commands
first version that can control current locally without enelx servers
- Loading branch information
Showing
16 changed files
with
1,676 additions
and
177 deletions.
There are no files selected for viewing
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
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 |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import yaml | ||
from pathlib import Path | ||
import logging | ||
|
||
from const import ( | ||
CONF_YAML, | ||
) | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
class JuiceboxConfig: | ||
|
||
|
||
def __init__(self, config_loc, filename=CONF_YAML): | ||
self.config_loc = Path(config_loc) | ||
self.config_loc.mkdir(parents=True, exist_ok=True) | ||
self.config_loc = self.config_loc.joinpath(filename) | ||
self.config_loc.touch(exist_ok=True) | ||
_LOGGER.info(f"config_loc: {self.config_loc}") | ||
self._config = {} | ||
self._changed = False | ||
|
||
|
||
async def load(self): | ||
config = {} | ||
try: | ||
_LOGGER.info(f"Reading config from {self.config_loc}") | ||
with open(self.config_loc, "r") as file: | ||
config = yaml.safe_load(file) | ||
except Exception as e: | ||
_LOGGER.warning(f"Can't load {self.config_loc}. ({e.__class__.__qualname__}: {e})") | ||
if not config: | ||
config = {} | ||
self._config = config | ||
|
||
async def write(self): | ||
try: | ||
_LOGGER.info(f"Writing config to {self.config_loc}") | ||
with open(self.config_loc, "w") as file: | ||
yaml.dump(self._config, file) | ||
self._changed = False | ||
return True | ||
except Exception as e: | ||
_LOGGER.warning( | ||
f"Can't write to {self.config_loc}. ({e.__class__.__qualname__}: {e})" | ||
) | ||
return False | ||
|
||
|
||
async def write_if_changed(self): | ||
if self._changed: | ||
return await self.write() | ||
return True | ||
|
||
def get(self, key, default): | ||
return self._config.get(key, default) | ||
|
||
# Get device specific configuration, if not found try to use global parameter | ||
def get_device(self, device, key, default): | ||
return self._config.get(device +"_" + key, self._config.get(key, default)) | ||
|
||
def update(self, data): | ||
# TODO detect changes | ||
return self._config.update(data) | ||
|
||
def update_value(self, key, value): | ||
if self._config.get(key, None) != value: | ||
self.update({ key : value }) | ||
self._changed = True | ||
|
||
def update_device_value(self, device, key, value): | ||
self.update_value(device + "_" + key, value) | ||
|
||
|
||
def pop(self, key): | ||
if key in self._config: | ||
self._config.pop(key, None) | ||
self._changed = True | ||
|
||
def is_changed(self): | ||
return self._changed | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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 |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# | ||
# Original code : https://github.com/philipkocanda/juicebox-protocol | ||
# | ||
class JuiceboxCRC: | ||
ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
|
||
def __init__(self, payload: str) -> None: | ||
self.payload = payload | ||
pass | ||
|
||
def integer(self) -> int: | ||
return self.crc(self.payload) | ||
|
||
|
||
def base35(self) -> str: | ||
return self.base35encode(self.integer()) | ||
|
||
|
||
def inspect(self) -> dict: | ||
return { | ||
"payload": self.payload, | ||
"base35": self.base35(), | ||
"integer": self.integer(), | ||
} | ||
|
||
def base35encode(self, number: int) -> str: | ||
base35 = "" | ||
|
||
# Sometimes it ends with 0 and the juicebox CRC should have 3 characters | ||
while (number > 1) or (len(base35) < 3): | ||
number, i = divmod(number, 35) | ||
if i == 24: | ||
i = 35 | ||
base35 = base35 + self.ALPHABET[i] | ||
|
||
return base35 | ||
|
||
|
||
def base35decode(self, number: str) -> int: | ||
decimal = 0 | ||
for i, s in enumerate(reversed(number)): | ||
decimal += self.ALPHABET.index(s) * (35**i) | ||
return decimal | ||
|
||
|
||
def crc(self, data: str) -> int: | ||
h = 0 | ||
for s in data: | ||
h ^= (h << 5) + (h >> 2) + ord(s) | ||
h &= 0xFFFF | ||
return h | ||
|
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 |
---|---|---|
@@ -0,0 +1,13 @@ | ||
# | ||
# Original code : https://github.com/philipkocanda/juicebox-protocol | ||
# | ||
class JuiceboxException(Exception): | ||
"Generic exception class for this library" | ||
pass | ||
|
||
class JuiceboxInvalidMessageFormat(JuiceboxException): | ||
pass | ||
|
||
|
||
class JuiceboxCRCError(JuiceboxException): | ||
pass |
Oops, something went wrong.