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

Setter for username and password #6

Merged
merged 2 commits into from
Dec 12, 2023
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
28 changes: 25 additions & 3 deletions sunweg/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
)
from .device import MPPT, Inverter, Phase, String
from .plant import Plant
from .util import SingletonMeta, Status
from .util import Status


class SunWegApiError(RuntimeError):
Expand All @@ -29,8 +29,8 @@ class LoginError(SunWegApiError):
pass


class APIHelper(metaclass=SingletonMeta):
"""Singleton class to call sunweg.net api."""
class APIHelper():
"""Class to call sunweg.net api."""

SERVER_URI = SUNWEG_URL

Expand All @@ -47,6 +47,28 @@ def __init__(self, username: str, password: str) -> None:
self._username = username
self._password = password
self.session = session()

def _set_username(self, username:str) -> None:
"""
Set username.

:param username: username for authentication
:type username: str
"""
self._username = username

username = property(None, _set_username)

def _set_password(self, password:str) -> None:
"""
Set password.

:param password: password for authentication
:type password: str
"""
self._password = password

password = property(None, _set_password)

def authenticate(self) -> bool:
"""
Expand Down
12 changes: 0 additions & 12 deletions sunweg/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,3 @@ class Status(Enum):
WARN = 2
ERROR = 1


class SingletonMeta(type):
"""Singleton meta."""

_instances: dict = {}

def __call__(cls, *args, **kwargs):
"""Handle singleton creation."""
if cls not in cls._instances:
instance = super().__call__(*args, **kwargs)
cls._instances[cls] = instance
return cls._instances[cls]
10 changes: 10 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,13 @@ def test_complete_inverter_401(self) -> None:
)
api.complete_inverter(inverter)
assert not inverter.is_complete

def test_setters(self) -> None:
api = APIHelper("[email protected]", "password")
assert api._username == "[email protected]"
assert api._password == "password"

api.username = "[email protected]"
api.password = "password1"
assert api._username == "[email protected]"
assert api._password == "password1"