Skip to content

Commit acd0cee

Browse files
committed
Further typing fixes
1 parent 5561f35 commit acd0cee

File tree

4 files changed

+34
-15
lines changed

4 files changed

+34
-15
lines changed

plugwise/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,8 @@ def _count_data_items(self, data: GwEntityData) -> None:
147147
self._count += len(data["sensors"]) - 1
148148
if "switches" in data:
149149
self._count += len(data["switches"]) - 1
150+
if "weather" in data:
151+
self._count += len(data["weather"]) - 1
150152
self._count += len(data)
151153

152154
def _power_data_peak_value(self, loc: Munch, legacy: bool) -> Munch:

plugwise/constants.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ class ThermoLoc(TypedDict, total=False):
521521
secondary: list[str]
522522

523523

524-
@dataclass
525-
class WeatherData:
524+
class WeatherData(TypedDict, total=False):
526525
"""Smile Weather data class."""
527526

528527
humidity: int

plugwise/helper.py

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
SensorType,
4545
ThermoLoc,
4646
ToggleNameType,
47+
WeatherType,
4748
)
4849
from plugwise.exceptions import (
4950
ConnectionFailedError,
@@ -527,7 +528,12 @@ def _get_measurement_data(self, entity_id: str) -> GwEntityData:
527528
528529
Collect the appliance-data based on entity_id.
529530
"""
530-
data: GwEntityData = {"binary_sensors": {}, "sensors": {}, "switches": {}}
531+
data: GwEntityData = {
532+
"binary_sensors": {},
533+
"sensors": {},
534+
"switches": {},
535+
"weather": {},
536+
}
531537
# Get P1 smartmeter data from LOCATIONS
532538
entity = self.gw_entities[entity_id]
533539
# !! DON'T CHANGE below two if-lines, will break stuff !!
@@ -771,29 +777,33 @@ def _get_gateway_measurements(self, entity_id: str, data: GwEntityData) -> None:
771777
"""
772778
measurements = DEVICE_MEASUREMENTS
773779
if self._is_thermostat and entity_id == self.gateway_id:
774-
data["weather"] = {}
775780
for measurement, attrs in measurements.items():
776781
value = self._object_value(self._home_location, measurement, attrs)
777782
if value is not None:
778783
if measurement == "wind_vector":
779-
value = value.split(",")
784+
value_list: list[str] = str(value).split(",")
780785
data["weather"]["wind_speed"] = format_measure(
781-
value[0].strip("("), getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
786+
value_list[0].strip("("),
787+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
782788
)
783789
data["weather"]["wind_bearing"] = format_measure(
784-
value[1].strip(")"), getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
790+
value_list[1].strip(")"),
791+
getattr(attrs, ATTR_UNIT_OF_MEASUREMENT),
785792
)
786793
self._count += 2
787794
else:
788-
data["weather"][measurement] = value
795+
key = cast(WeatherType, measurement)
796+
data["weather"][key] = value
789797
self._count += 1
790798

791-
def _object_value(self, obj_id: str, measurement: str, attrs: str) -> float | int | str| None:
799+
def _object_value(
800+
self, obj_id: str, measurement: str, attrs: DATA | UOM
801+
) -> float | int | str | None:
792802
"""Helper-function for smile.py: _get_entity_data().
793803
794804
Obtain the value/state for the given object from a location in DOMAIN_OBJECTS
795805
"""
796-
value: float | int | str | None = None
806+
value: str = ""
797807
search = self._domain_objects
798808
locator = f'./location[@id="{obj_id}"]/logs/point_log[type="{measurement}"]/period/measurement'
799809
if (found := search.find(locator)) is not None:
@@ -802,14 +812,20 @@ def _object_value(self, obj_id: str, measurement: str, attrs: str) -> float | in
802812
case "humidity":
803813
return int(float(value))
804814
case "outdoor_temperature":
805-
return format_measure(value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT))
815+
return format_measure(
816+
value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
817+
)
806818
case "solar_irradiance":
807-
return format_measure(value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT))
819+
return format_measure(
820+
value, getattr(attrs, ATTR_UNIT_OF_MEASUREMENT)
821+
)
808822
case "weather_description":
809823
return value
810824
case "wind_vector":
811825
return value
812826

827+
return None
828+
813829
def _process_c_heating_state(self, data: GwEntityData) -> None:
814830
"""Helper-function for _get_measurement_data().
815831

plugwise/util.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,14 @@ def power_data_local_format(
203203

204204
def remove_empty_platform_dicts(data: GwEntityData) -> None:
205205
"""Helper-function for removing any empty platform dicts."""
206-
if not data["binary_sensors"]:
206+
if "binary_sensors" in data and not data["binary_sensors"]:
207207
data.pop("binary_sensors")
208-
if not data["sensors"]:
208+
if "sensors" in data and not data["sensors"]:
209209
data.pop("sensors")
210-
if not data["switches"]:
210+
if "switches" in data and not data["switches"]:
211211
data.pop("switches")
212+
if "weather" in data and not data["weather"]:
213+
data.pop("weather")
212214

213215

214216
def return_valid(value: etree | None, default: etree) -> etree:

0 commit comments

Comments
 (0)