Skip to content

Commit

Permalink
Catch fileNotFound error
Browse files Browse the repository at this point in the history
  • Loading branch information
bramstroker committed Aug 23, 2022
1 parent a506d98 commit eaa1a8f
Showing 1 changed file with 25 additions and 16 deletions.
41 changes: 25 additions & 16 deletions custom_components/powercalc/power_profile/library.py
Expand Up @@ -2,7 +2,7 @@

import json
import os
from re import M
import logging
from typing import NamedTuple

from homeassistant.core import HomeAssistant
Expand All @@ -13,6 +13,7 @@

CUSTOM_DATA_DIRECTORY = "powercalc-custom-models"

_LOGGER = logging.getLogger(__name__)

class ProfileLibrary:
def __init__(self, hass: HomeAssistant):
Expand Down Expand Up @@ -115,29 +116,37 @@ async def get_profiles_by_manufacturer(
for model in os.listdir(manufacturer_dir):
if model.startswith("."):
continue
profiles.append(
await self._create_power_profile(
ModelInfo(manufacturer, model),
os.path.join(manufacturer_dir, model),
)

power_profile = await self._create_power_profile(
ModelInfo(manufacturer, model),
os.path.join(manufacturer_dir, model),
)
if power_profile is None:
continue

profiles.append(power_profile)

self._profiles[manufacturer] = profiles
return profiles

async def _create_power_profile(
self, model_info: ModelInfo, directory: str
) -> PowerProfile:
) -> PowerProfile | None:
model_json_path = os.path.join(directory, "model.json")
with open(model_json_path) as file:
json_data = json.load(file)
profile = PowerProfile(
self._hass,
manufacturer=model_info.manufacturer,
model=model_info.model,
directory=directory,
json_data=json_data,
)
try:
with open(model_json_path) as file:
json_data = json.load(file)
profile = PowerProfile(
self._hass,
manufacturer=model_info.manufacturer,
model=model_info.model,
directory=directory,
json_data=json_data,
)
except FileNotFoundError:
_LOGGER.error("model.json file not found in directory %s", directory)
return None

return profile


Expand Down

0 comments on commit eaa1a8f

Please sign in to comment.