Skip to content

Commit

Permalink
Use class name instead of subdomain for --utility
Browse files Browse the repository at this point in the history
  • Loading branch information
tronikos committed Jul 13, 2023
1 parent e4fa5b4 commit 9daae12
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
5 changes: 3 additions & 2 deletions src/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,16 @@

import aiohttp

from opower import AggregateType, Opower, get_supported_utility_subdomains
from opower import AggregateType, Opower, get_supported_utilities


async def _main():
parser = argparse.ArgumentParser()
parser.add_argument(
"--utility",
help="Utility (subdomain of opower.com). Defaults to pge",
choices=get_supported_utility_subdomains(),
choices=[utility.__name__.lower() for utility in get_supported_utilities()],
type=str.lower,
default="pge",
)
parser.add_argument(
Expand Down
4 changes: 2 additions & 2 deletions src/opower/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
Opower,
UnitOfMeasure,
UsageRead,
get_supported_utilities,
get_supported_utility_names,
get_supported_utility_subdomains,
)

__all__ = [
Expand All @@ -25,6 +25,6 @@
"Opower",
"UnitOfMeasure",
"UsageRead",
"get_supported_utilities",
"get_supported_utility_names",
"get_supported_utility_subdomains",
]
16 changes: 10 additions & 6 deletions src/opower/opower.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,24 @@ class UsageRead:
consumption: float # taken from consumption.value field, in KWH or THERM


def get_supported_utilities() -> list[type["UtilityBase"]]:
"""Return a list of all supported utilities."""
return UtilityBase.subclasses


def get_supported_utility_names() -> list[str]:
"""Return a list of names of all supported utilities."""
return [utility.name() for utility in UtilityBase.subclasses]


def get_supported_utility_subdomains() -> list[str]:
"""Return a list of subdomains of all supported utilities."""
return [utility.subdomain() for utility in UtilityBase.subclasses]


def _select_utility(name_or_subdomain: str) -> type[UtilityBase]:
"""Return the utility with the given name or subdomain."""
for utility in UtilityBase.subclasses:
if name_or_subdomain in [utility.name(), utility.subdomain()]:
if name_or_subdomain.lower() in [
utility.name().lower(),
utility.__name__.lower(),
utility.subdomain().lower(),
]:
return utility
raise ValueError(f"Utility {name_or_subdomain} not found")

Expand Down

0 comments on commit 9daae12

Please sign in to comment.