-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor: move hubitat config to it's own class in hubitat_gt.py so t…
…hat config can be used by component without circular import; fix hubitat address generation
- Loading branch information
1 parent
13d0321
commit 500f176
Showing
5 changed files
with
91 additions
and
112 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,32 +1,29 @@ | ||
from typing import Optional | ||
|
||
import yarl | ||
|
||
from gwproto.data_classes.component import Component | ||
from gwproto.types.hubitat_gt import HubitatGt | ||
|
||
|
||
class HubitatComponent(Component): | ||
host: str | ||
maker_api_id: int | ||
access_token: str | ||
mac_address: str | ||
hubitat_gt: HubitatGt | ||
|
||
def __init__( | ||
self, | ||
component_id: str, | ||
component_attribute_class_id: str, | ||
host: str, | ||
maker_api_id: int, | ||
access_token: str, | ||
mac_address: str, | ||
hubitat_gt: HubitatGt, | ||
display_name: Optional[str] = None, | ||
hw_uid: Optional[str] = None, | ||
): | ||
self.host = host | ||
self.maker_api_id = maker_api_id | ||
self.access_token = access_token | ||
self.mac_address = mac_address | ||
self.hubitat_gt = hubitat_gt | ||
super().__init__( | ||
component_id=component_id, | ||
component_attribute_class_id=component_attribute_class_id, | ||
display_name=display_name, | ||
hw_uid=hw_uid, | ||
) | ||
|
||
def urls(self) -> dict[str, Optional[yarl.URL]]: | ||
return self.hubitat_gt.urls() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
from typing import Optional | ||
|
||
import yarl | ||
from gridworks.property_format import predicate_validator | ||
from pydantic import BaseModel | ||
|
||
from gwproto.types.rest_poller_gt import URLArgs | ||
from gwproto.types.rest_poller_gt import URLConfig | ||
from gwproto.utils import has_mac_address_format | ||
|
||
|
||
class HubitatGt(BaseModel): | ||
Host: str | ||
MakerApiId: int | ||
AccessToken: str | ||
MacAddress: str | ||
|
||
_is_mac_address = predicate_validator("MacAddress", has_mac_address_format) | ||
|
||
def url_config(self) -> URLConfig: | ||
return URLConfig( | ||
url_args=URLArgs( | ||
scheme="http", | ||
host=self.Host, | ||
), | ||
) | ||
|
||
def maker_api_url_config(self) -> URLConfig: | ||
config = self.url_config() | ||
if config.url_args.query is None: | ||
config.url_args.query = [] | ||
config.url_args.query.append(("access_token", self.AccessToken)) | ||
if config.url_path_format is None: | ||
config.url_path_format = "" | ||
config.url_path_format += "/apps/api/{app_id}" | ||
if config.url_path_args is None: | ||
config.url_path_args = {} | ||
config.url_path_args.update({"app_id": self.MakerApiId}) | ||
return config | ||
|
||
def url_configs(self) -> dict[str, URLConfig]: | ||
return dict(base=self.url_config(), maker_api=self.maker_api_url_config()) | ||
|
||
def urls(self) -> dict[str, Optional[yarl.URL]]: | ||
return { | ||
name: URLConfig.make_url(config) | ||
for name, config in self.url_configs().items() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters