Skip to content

Commit 9d2bb4d

Browse files
committed
Optimize
1 parent 5b14d73 commit 9d2bb4d

File tree

1 file changed

+36
-38
lines changed

1 file changed

+36
-38
lines changed

plugwise/helper.py

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -771,54 +771,52 @@ 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+
value = self._location_value(self._home_location, measurement, attrs)
784+
if value is not None:
785+
if measurement == "wind_vector":
786+
value_list: list[str] = str(value).split(",")
787+
data["weather"]["wind_speed"] = format_measure(
788+
value_list[0].strip("("),
789+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
790+
)
791+
data["weather"]["wind_bearing"] = format_measure(
792+
value_list[1].strip(")"),
793+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
794+
)
795+
self._count += 2
796+
else:
797+
key = cast(WeatherType, measurement)
798+
data["weather"][key] = value
799+
self._count += 1
800+
801+
def _location_value(
802+
self, loc_id: str, measurement: str, attrs: DATA | UOM
801803
) -> float | int | str | None:
802804
"""Helper-function for smile.py: _get_entity_data().
803805
804806
Obtain the value/state for the given object from a location in DOMAIN_OBJECTS
805807
"""
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
808+
locator = f"./location[@id='{loc_id}']/logs/point_log[type='{measurement}']/period/measurement"
809+
if (found := self._domain_objects.find(locator)) is None:
810+
return None
820811

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

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

0 commit comments

Comments
 (0)