Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove unused test_credentials method and add tests #57

Merged
merged 3 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 49 additions & 7 deletions custom_components/stateful_scenes/StatefulScenes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import logging

import yaml
import os
from homeassistant.core import HomeAssistant

from .const import ATTRIBUTES_TO_CHECK
Expand Down Expand Up @@ -32,16 +33,29 @@ class Hub:
"""State scene class."""

def __init__(
self, hass: HomeAssistant, scene_path: str, number_tolerance=1
self, hass: HomeAssistant, scene_path: str, number_tolerance:int=1
) -> None:
"""Initialize."""
"""Initialize the Hub class.

Args:
hass (HomeAssistant): Home Assistant instance
scene_path (str): Path to the yaml file containing the scenes
number_tolerance (int): Tolerance for comparing numbers

Raises:
StatefulScenesYamlNotFound: If the yaml file is not found
StatefulScenesYamlInvalid: If the yaml file is invalid

"""
self.scene_path = scene_path
self.number_tolerance = number_tolerance
self.hass = hass
self.scenes = []

scene_confs = self.load_scenes()
for scene_conf in scene_confs:
if not self.validate_scene(scene_conf):
continue
self.scenes.append(
Scene(
self.hass,
Expand All @@ -52,18 +66,38 @@ def __init__(

def load_scenes(self) -> list:
"""Load scenes from yaml file."""
# check if file exists
if self.scene_path is None:
raise StatefulScenesYamlNotFound("Scenes file not specified.")
if not os.path.exists(self.scene_path):
raise StatefulScenesYamlNotFound("No scenes file " + self.scene_path)

try:
with open(self.scene_path, encoding="utf-8") as f:
scenes_confs = yaml.load(f, Loader=yaml.FullLoader)
except OSError as err:
raise StatefulScenesYamlNotFound(
raise StatefulScenesYamlInvalid(
"No scenes found in " + self.scene_path
) from err

if not scenes_confs or not isinstance(scenes_confs, list):
raise StatefulScenesYamlInvalid("No scenes found in " + self.scene_path)

return scenes_confs

def validate_scene(self, scene_conf) -> None:
"""Validate scene configuration."""
def validate_scene(self, scene_conf: dict) -> None:
"""Validate scene configuration.

Args:
scene_conf (dict): Scene configuration

Raises:
StatefulScenesYamlInvalid: If the scene is invalid

Returns:
bool: True if the scene is valid

"""

if "entities" not in scene_conf:
raise StatefulScenesYamlInvalid("Scene is missing entities: " + scene_conf)
Expand All @@ -76,8 +110,16 @@ def validate_scene(self, scene_conf) -> None:

return True

def extract_scene_configuration(self, scene_conf) -> dict:
"""Extract entities and attributes from a scene."""
def extract_scene_configuration(self, scene_conf: dict) -> dict:
"""Extract entities and attributes from a scene.

Args:
scene_conf (dict): Scene configuration

Returns:
dict: Scene configuration

"""
entities = {}
for entity_id, scene_attributes in scene_conf["entities"].items():
domain = entity_id.split(".")[0]
Expand Down
9 changes: 0 additions & 9 deletions custom_components/stateful_scenes/config_flow.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,3 @@ async def async_step_user(
),
errors=_errors,
)

# async def _test_credentials(self, username: str, password: str) -> None:
# """Validate credentials."""
# client = IntegrationBlueprintApiClient(
# username=username,
# password=password,
# session=async_create_clientsession(self.hass),
# )
# await client.async_get_data()