Skip to content

Commit

Permalink
Remove explicit boolean check for smart devices
Browse files Browse the repository at this point in the history
  • Loading branch information
dormant-user committed Dec 25, 2024
1 parent 11ce3cf commit bcc83ff
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 77 deletions.
2 changes: 1 addition & 1 deletion docs/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3696,7 +3696,7 @@ <h1>———-Executors———-<a class="headerlink" href="#executors" title="

<dl class="py function">
<dt class="sig sig-object py" id="jarvis.executors.files.get_smart_devices">
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.files.</span></span><span class="sig-name descname"><span class="pre">get_smart_devices</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">dict</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">bool</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.files.get_smart_devices" title="Permalink to this definition">¶</a></dt>
<span class="sig-prename descclassname"><span class="pre">jarvis.executors.files.</span></span><span class="sig-name descname"><span class="pre">get_smart_devices</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span> <span class="sig-return"><span class="sig-return-icon">&#x2192;</span> <span class="sig-return-typehint"><span class="pre">dict</span><span class="w"> </span><span class="p"><span class="pre">|</span></span><span class="w"> </span><span class="pre">None</span></span></span><a class="headerlink" href="#jarvis.executors.files.get_smart_devices" title="Permalink to this definition">¶</a></dt>
<dd><p>Load smart devices’ data from feed file.</p>
</dd></dl>

Expand Down
17 changes: 2 additions & 15 deletions jarvis/executors/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,22 +190,9 @@ def put_automation(
_dumper(models.fileio.automation, sorted_data)


def get_smart_devices() -> dict | bool | None:
def get_smart_devices() -> dict | None:
"""Load smart devices' data from feed file."""
# fixme: Change the logic to NOT look for False specifically
try:
with open(models.fileio.smart_devices) as file:
if smart_devices := yaml.load(stream=file, Loader=yaml.FullLoader):
return smart_devices
else:
logger.warning("'%s' is empty.", models.fileio.smart_devices)
except (yaml.YAMLError, FileNotFoundError) as error:
if isinstance(error, FileNotFoundError):
logger.warning("%s not found.", models.fileio.smart_devices)
return
else:
logger.debug(error)
return False
return _loader(models.fileio.smart_devices)


def put_smart_devices(data: dict) -> None:
Expand Down
15 changes: 7 additions & 8 deletions jarvis/executors/lights.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,16 +123,15 @@ def lights(phrase: str) -> None:
if not internet.vpn_checker():
return

smart_devices = files.get_smart_devices()
if smart_devices is False:
speaker.speak(
text=f"I'm sorry {models.env.title}! I wasn't able to read the source information."
)
return
if smart_devices and (lights_map := get_lights(data=smart_devices)):
if (smart_devices := files.get_smart_devices()) and (
lights_map := get_lights(data=smart_devices)
):
logger.debug(lights_map)
else:
logger.warning("%s is empty for lights.", models.fileio.smart_devices)
logger.warning(
"Smart devices are not configured for lights in %s",
models.fileio.smart_devices,
)
support.no_env_vars()
return

Expand Down
14 changes: 6 additions & 8 deletions jarvis/executors/tv.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,15 @@ def television(phrase: str) -> None:
if not internet.vpn_checker():
return

smart_devices = files.get_smart_devices()
if smart_devices is False:
speaker.speak(
text=f"I'm sorry {models.env.title}! I wasn't able to read the source information."
)
return
if smart_devices and (tv_mapping := get_tv(data=smart_devices)):
if (smart_devices := files.get_smart_devices()) and (
tv_mapping := get_tv(data=smart_devices)
):
tv_map, key = tv_mapping
logger.debug("%s stored with key: '%s'", tv_map, key)
else:
logger.warning("%s is empty for tv.", models.fileio.smart_devices)
logger.warning(
"Smart devices are not configured for tv in %s", models.fileio.smart_devices
)
support.no_env_vars()
return

Expand Down
92 changes: 47 additions & 45 deletions jarvis/modules/logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from logging import Formatter
from logging.config import dictConfig

from pydantic import BaseModel
from pydantic import BaseModel, Field

from jarvis.modules.builtin_overrides import AddProcessName
from jarvis.modules.models import models
Expand Down Expand Up @@ -84,53 +84,55 @@ class APIConfig(BaseModel):
)
ERROR_LOG_FORMAT: str = "%(levelname)s\t %(message)s"

LOG_CONFIG: dict = {
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": DEFAULT_LOG_FORM,
"use_colors": False,
},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": ACCESS_LOG_FORMAT,
"use_colors": False,
},
"error": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": ERROR_LOG_FORMAT,
"use_colors": False,
LOG_CONFIG: dict = Field(
{
"version": 1,
"disable_existing_loggers": True,
"formatters": {
"default": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": DEFAULT_LOG_FORM,
"use_colors": False,
},
"access": {
"()": "uvicorn.logging.AccessFormatter",
"fmt": ACCESS_LOG_FORMAT,
"use_colors": False,
},
"error": {
"()": "uvicorn.logging.DefaultFormatter",
"fmt": ERROR_LOG_FORMAT,
"use_colors": False,
},
},
},
"handlers": {
"default": {
"formatter": "default",
"class": "logging.FileHandler",
"filename": DEFAULT_LOG_FILENAME,
"handlers": {
"default": {
"formatter": "default",
"class": "logging.FileHandler",
"filename": DEFAULT_LOG_FILENAME,
},
"access": {
"formatter": "access",
"class": "logging.FileHandler",
"filename": ACCESS_LOG_FILENAME,
},
"error": {
"formatter": "error",
"class": "logging.FileHandler",
"filename": DEFAULT_LOG_FILENAME,
},
},
"access": {
"formatter": "access",
"class": "logging.FileHandler",
"filename": ACCESS_LOG_FILENAME,
"loggers": {
"uvicorn": {"handlers": ["default"], "level": DEFAULT_LOG_LEVEL},
"uvicorn.access": {"handlers": ["access"], "level": DEFAULT_LOG_LEVEL},
"uvicorn.error": {
"handlers": ["error"],
"level": DEFAULT_LOG_LEVEL,
"propagate": True, # Since FastAPI is threaded
},
},
"error": {
"formatter": "error",
"class": "logging.FileHandler",
"filename": DEFAULT_LOG_FILENAME,
},
},
"loggers": {
"uvicorn": {"handlers": ["default"], "level": DEFAULT_LOG_LEVEL},
"uvicorn.access": {"handlers": ["access"], "level": DEFAULT_LOG_LEVEL},
"uvicorn.error": {
"handlers": ["error"],
"level": DEFAULT_LOG_LEVEL,
"propagate": True, # Since FastAPI is threaded
},
},
}
}
)


def log_file(filename: str) -> str:
Expand Down

0 comments on commit bcc83ff

Please sign in to comment.