Skip to content

Commit 6488ec9

Browse files
committed
Optimize
1 parent 5b14d73 commit 6488ec9

File tree

1 file changed

+39
-38
lines changed

1 file changed

+39
-38
lines changed

plugwise/helper.py

Lines changed: 39 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -771,54 +771,55 @@ def _get_gateway_mode(
771771
self._count += 1
772772

773773
def _get_gateway_measurements(self, entity_id: str, data: GwEntityData) -> None:
774-
"""Adam & Anna: the Gateway weather-data is present in DOMAIN_OBJECTS and LOCATIONS.
774+
"""Adam & Anna: the Gateway weather-data is present under the Home location.
775775
776776
Available under the Home location.
777777
"""
778+
if not (self._is_thermostat and entity_id == self.gateway_id):
779+
return
780+
778781
measurements = DEVICE_MEASUREMENTS
779-
if self._is_thermostat and entity_id == self.gateway_id:
780-
for measurement, attrs in measurements.items():
781-
value = self._object_value(self._home_location, measurement, attrs)
782-
if value is not None:
783-
if measurement == "wind_vector":
784-
value_list: list[str] = str(value).split(",")
785-
data["weather"]["wind_speed"] = format_measure(
786-
value_list[0].strip("("),
787-
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
788-
)
789-
data["weather"]["wind_bearing"] = format_measure(
790-
value_list[1].strip(")"),
791-
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
792-
)
793-
self._count += 2
794-
else:
795-
key = cast(WeatherType, measurement)
796-
data["weather"][key] = value
797-
self._count += 1
798-
799-
def _object_value(
800-
self, obj_id: str, measurement: str, attrs: DATA | UOM
782+
for measurement, attrs in measurements.items():
783+
if (
784+
value := self._loc_value(self._home_location, measurement, attrs)
785+
) is None:
786+
continue
787+
788+
if measurement == "wind_vector":
789+
value_list: list[str] = str(value).split(",")
790+
data["weather"]["wind_speed"] = format_measure(
791+
value_list[0].strip("("),
792+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
793+
)
794+
data["weather"]["wind_bearing"] = format_measure(
795+
value_list[1].strip(")"),
796+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
797+
)
798+
self._count += 2
799+
else:
800+
key = cast(WeatherType, measurement)
801+
data["weather"][key] = value
802+
self._count += 1
803+
804+
def _loc_value(
805+
self, loc_id: str, measurement: str, attrs: DATA | UOM
801806
) -> float | int | str | None:
802807
"""Helper-function for smile.py: _get_entity_data().
803808
804809
Obtain the value/state for the given object from a location in DOMAIN_OBJECTS
805810
"""
806-
value: str = ""
807-
search = self._domain_objects
808-
locator = f'./location[@id="{obj_id}"]/logs/point_log[type="{measurement}"]/period/measurement'
809-
if (found := search.find(locator)) is not None:
810-
value = found.text
811-
match measurement:
812-
case "humidity":
813-
return int(float(value))
814-
case "outdoor_temperature" | "solar_irradiance":
815-
return format_measure(
816-
value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
817-
)
818-
case _:
819-
return value
811+
locator = f"./location[@id='{loc_id}']/logs/point_log[type='{measurement}']/period/measurement"
812+
if (found := self._domain_objects.find(locator)) is None:
813+
return None
820814

821-
return None
815+
value: str = found.text
816+
match measurement:
817+
case "humidity":
818+
return int(float(value))
819+
case "outdoor_temperature" | "solar_irradiance":
820+
return format_measure(value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT))
821+
case _:
822+
return value
822823

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

0 commit comments

Comments
 (0)