Skip to content

Commit

Permalink
Optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
bouwew committed Dec 21, 2024
1 parent 5b14d73 commit 6488ec9
Showing 1 changed file with 39 additions and 38 deletions.
77 changes: 39 additions & 38 deletions plugwise/helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,54 +771,55 @@ def _get_gateway_mode(
self._count += 1

def _get_gateway_measurements(self, entity_id: str, data: GwEntityData) -> None:
"""Adam & Anna: the Gateway weather-data is present in DOMAIN_OBJECTS and LOCATIONS.
"""Adam & Anna: the Gateway weather-data is present under the Home location.
Available under the Home location.
"""
if not (self._is_thermostat and entity_id == self.gateway_id):
return

measurements = DEVICE_MEASUREMENTS
if self._is_thermostat and entity_id == self.gateway_id:
for measurement, attrs in measurements.items():
value = self._object_value(self._home_location, measurement, attrs)
if value is not None:
if measurement == "wind_vector":
value_list: list[str] = str(value).split(",")
data["weather"]["wind_speed"] = format_measure(
value_list[0].strip("("),
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
)
data["weather"]["wind_bearing"] = format_measure(
value_list[1].strip(")"),
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
)
self._count += 2
else:
key = cast(WeatherType, measurement)
data["weather"][key] = value
self._count += 1

def _object_value(
self, obj_id: str, measurement: str, attrs: DATA | UOM
for measurement, attrs in measurements.items():
if (
value := self._loc_value(self._home_location, measurement, attrs)
) is None:
continue

if measurement == "wind_vector":
value_list: list[str] = str(value).split(",")
data["weather"]["wind_speed"] = format_measure(
value_list[0].strip("("),
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
)
data["weather"]["wind_bearing"] = format_measure(
value_list[1].strip(")"),
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
)
self._count += 2
else:
key = cast(WeatherType, measurement)
data["weather"][key] = value
self._count += 1

def _loc_value(
self, loc_id: str, measurement: str, attrs: DATA | UOM
) -> float | int | str | None:
"""Helper-function for smile.py: _get_entity_data().
Obtain the value/state for the given object from a location in DOMAIN_OBJECTS
"""
value: str = ""
search = self._domain_objects
locator = f'./location[@id="{obj_id}"]/logs/point_log[type="{measurement}"]/period/measurement'
if (found := search.find(locator)) is not None:
value = found.text
match measurement:
case "humidity":
return int(float(value))
case "outdoor_temperature" | "solar_irradiance":
return format_measure(
value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
)
case _:
return value
locator = f"./location[@id='{loc_id}']/logs/point_log[type='{measurement}']/period/measurement"
if (found := self._domain_objects.find(locator)) is None:
return None

return None
value: str = found.text
match measurement:
case "humidity":
return int(float(value))
case "outdoor_temperature" | "solar_irradiance":
return format_measure(value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT))
case _:
return value

def _process_c_heating_state(self, data: GwEntityData) -> None:
"""Helper-function for _get_measurement_data().
Expand Down

0 comments on commit 6488ec9

Please sign in to comment.