Skip to content

Commit

Permalink
Make supply voltage configurable in hubitat tank
Browse files Browse the repository at this point in the history
  • Loading branch information
anschweitzer committed Oct 19, 2023
1 parent 8c427ac commit d3fe6c3
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 51 deletions.
2 changes: 2 additions & 0 deletions src/gwproto/data_classes/components/hubitat_tank_component.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

class HubitatTankComponent(Component, ComponentResolver):
hubitat: HubitatComponentGt
sensor_supply_voltage: float
devices_gt: list[FibaroTempSensorSettingsGt]
devices: list[FibaroTempSensorSettings] = []

Expand All @@ -42,6 +43,7 @@ def __init__(
MacAddress="000000000000",
),
)
self.sensor_supply_voltage = tank_gt.sensor_supply_voltage
self.devices_gt = list(tank_gt.devices)
super().__init__(
display_name=display_name,
Expand Down
11 changes: 10 additions & 1 deletion src/gwproto/types/hubitat_gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,17 @@ def maker_api_url_config(self) -> URLConfig:
config.url_path_args.update({"app_id": self.MakerApiId})
return config

def devices_url_config(self) -> URLConfig:
config = self.maker_api_url_config()
config.url_path_format += "/devices"
return config

def url_configs(self) -> dict[str, URLConfig]:
return dict(base=self.url_config(), maker_api=self.maker_api_url_config())
return dict(
base=self.url_config(),
maker_api=self.maker_api_url_config(),
devices=self.devices_url_config(),
)

def urls(self) -> dict[str, Optional[yarl.URL]]:
return {
Expand Down
94 changes: 44 additions & 50 deletions src/gwproto/types/hubitat_tank_gt.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class FibaroTempSensorSettingsGt(BaseModel):
device_id: int
fibaro_component_id: str
analog_input_id: conint(ge=1, le=2)
tank_label: str = ""
exponent: int = 1
telemetry_name_gt_enum_symbol: str = "c89d0ba1"
temp_unit_gt_enum_symbol: str = "ec14bd47"
Expand Down Expand Up @@ -153,57 +154,46 @@ def resolve_rest(

# Verify new URL produced by combining any inline REST configuration
# with hubitat configuration is valid.
url_str = ""
try:
url_str = str(self.rest.url)
hubitat_gt = hubitat.component_gt.Hubitat
# check host
if hubitat_gt.Host != self.rest.url.host:
raise ValueError(
"ERROR host expected to be "
f"{hubitat_gt.Host} but host in url is "
f"{self.rest.url.host}, from url: <{url_str}>"
)

# check api_id
if hubitat_gt.MakerApiId != self.api_id:
raise ValueError(
"ERROR api_id expected to be "
f"{hubitat_gt.MakerApiId} but api_id in url is "
f"{self.api_id}, from url: <{url_str}>"
)

# check device_id
id_match = HUBITAT_ID_REGEX.match(url_str)
if not id_match:
raise ValueError(
f"ERROR. ID regex <{HUBITAT_ID_REGEX.pattern}> failed to match "
f" url <{url_str}>"
)
found_device_id = int(id_match.group("device_id"))
if self.device_id != found_device_id:
raise ValueError(
"ERROR explicit device_id is "
f"{self.device_id} but device in url is "
f"{found_device_id}, from url: <{url_str}>"
)

# check token match
if hubitat_gt.AccessToken != self.access_token:
raise ValueError(
"ERROR explicit access_token is "
f"{hubitat_gt.AccessToken} but device in url is "
f"{self.access_token}, from url: <{url_str}>"
)

except BaseException as e:
if isinstance(e, ValueError):
raise e
url_str = str(self.rest.url)
hubitat_gt = hubitat.component_gt.Hubitat
# check host
if hubitat_gt.Host != self.rest.url.host:
raise ValueError(
f"ERROR in FibaroTempSensorSettings.validate_url() for url <{url_str}>\n"
f" {type(e)} <{e}>"
) from e
raise e
"ERROR host expected to be "
f"{hubitat_gt.Host} but host in url is "
f"{self.rest.url.host}, from url: <{url_str}>"
)

# check api_id
if hubitat_gt.MakerApiId != self.api_id:
raise ValueError(
"ERROR api_id expected to be "
f"{hubitat_gt.MakerApiId} but api_id in url is "
f"{self.api_id}, from url: <{url_str}>"
)

# check device_id
id_match = HUBITAT_ID_REGEX.match(url_str)
if not id_match:
raise ValueError(
f"ERROR. ID regex <{HUBITAT_ID_REGEX.pattern}> failed to match "
f" url <{url_str}>"
)
found_device_id = int(id_match.group("device_id"))
if self.device_id != found_device_id:
raise ValueError(
"ERROR explicit device_id is "
f"{self.device_id} but device in url is "
f"{found_device_id}, from url: <{url_str}>"
)

# check token match
if hubitat_gt.AccessToken != self.access_token:
raise ValueError(
"ERROR explicit access_token is "
f"{hubitat_gt.AccessToken} but device in url is "
f"{self.access_token}, from url: <{url_str}>"
)

@classmethod
def create(
Expand All @@ -224,8 +214,12 @@ def create(
return settings


DEFAULT_TANK_MODULE_VOLTAGE = 23.7


class HubitatTankSettingsGt(BaseModel):
hubitat_component_id: str
sensor_supply_voltage: float = DEFAULT_TANK_MODULE_VOLTAGE
devices: list[FibaroTempSensorSettingsGt] = []

class Config:
Expand Down

0 comments on commit d3fe6c3

Please sign in to comment.