44"""
55from __future__ import annotations
66
7+ from typing import cast
8+
79import aiohttp
810from defusedxml import ElementTree as etree
911
3537 ApplianceData ,
3638 DeviceData ,
3739 PlugwiseData ,
38- SmileBinarySensors ,
39- SmileSensors ,
40- SmileSwitches ,
4140)
4241from .exceptions import (
4342 InvalidSetupError ,
4443 PlugwiseError ,
4544 ResponseError ,
4645 UnsupportedDeviceError ,
4746)
48- from .helper import SmileComm , SmileHelper , update_helper
47+ from .helper import SmileComm , SmileHelper
48+
49+
50+ def remove_empty_platform_dicts (data : DeviceData ) -> DeviceData :
51+ """Helper-function for removing any empty platform dicts."""
52+ if not data ["binary_sensors" ]:
53+ data .pop ("binary_sensors" )
54+ if not data ["sensors" ]:
55+ data .pop ("sensors" )
56+ if not data ["switches" ]:
57+ data .pop ("switches" )
58+
59+ return data
4960
5061
5162class SmileData (SmileHelper ):
@@ -82,18 +93,24 @@ def _all_device_data(self) -> None:
8293 Collect initial data for each device and add to self.gw_data and self.gw_devices.
8394 """
8495 for device_id , device in self ._appl_data .items ():
85- bs_dict : SmileBinarySensors = {}
86- s_dict : SmileSensors = {}
87- sw_dict : SmileSwitches = {}
96+ self .gw_devices .update ({device_id : cast (DeviceData , device )})
97+
8898 data = self ._get_device_data (device_id )
89- self .gw_devices [device_id ] = self ._update_device_with_dicts (
90- device_id , data , device , bs_dict , s_dict , sw_dict
91- )
99+ # Add plugwise notification binary_sensor to the relevant gateway
100+ if device_id == self .gateway_id and (
101+ self ._is_thermostat
102+ or (not self ._smile_legacy and self .smile_type == "power" )
103+ ):
104+ data ["binary_sensors" ]["plugwise_notification" ] = False
105+
106+ self .gw_devices [device_id ].update (data )
92107
93108 # Update for cooling
94109 if self .gw_devices [device_id ]["dev_class" ] in ZONE_THERMOSTATS :
95110 self .update_for_cooling (self .gw_devices [device_id ])
96111
112+ remove_empty_platform_dicts (self .gw_devices [device_id ])
113+
97114 self .gw_data .update (
98115 {"smile_name" : self .smile_name , "gateway_id" : self .gateway_id }
99116 )
@@ -153,10 +170,10 @@ def _device_data_switching_group(
153170 counter = 0
154171 for member in details ["members" ]:
155172 member_data = self ._get_appliance_data (member )
156- if member_data .get ("relay" ):
173+ if member_data [ "switches" ] .get ("relay" ):
157174 counter += 1
158175
159- device_data ["relay" ] = counter != 0
176+ device_data ["switches" ][ " relay" ] = counter != 0
160177
161178 return device_data
162179
@@ -174,7 +191,7 @@ def _device_data_adam(
174191 and self ._on_off_device
175192 and self ._heating_valves () is not None
176193 ):
177- device_data ["heating_state" ] = self ._heating_valves () != 0
194+ device_data ["binary_sensors" ][ " heating_state" ] = self ._heating_valves () != 0
178195
179196 return device_data
180197
@@ -275,7 +292,7 @@ def _get_device_data(self, dev_id: str) -> DeviceData:
275292 self ._home_location , "outdoor_temperature"
276293 )
277294 if outdoor_temperature is not None :
278- device_data ["outdoor_temperature" ] = outdoor_temperature
295+ device_data ["sensors" ][ " outdoor_temperature" ] = outdoor_temperature
279296
280297 # Show the allowed regulation modes
281298 if self ._reg_allowed_modes :
@@ -519,31 +536,23 @@ async def async_update(self) -> PlugwiseData:
519536
520537 self .gw_data ["notifications" ] = self ._notifications
521538
522- for dev_id , dev_dict in self .gw_devices .items ():
523- data = self ._get_device_data (dev_id )
524- for key , value in data .items ():
525- if key in dev_dict :
526- dev_dict [key ] = value # type: ignore [literal-required]
527-
528- for item in ("binary_sensors" , "sensors" , "switches" ):
529- notifs : dict [str , dict [str , str ]] = {}
530- if item == "binary_sensors" :
531- notifs = self ._notifications
532- if item in dev_dict :
533- for key in data :
534- update_helper (
535- data ,
536- self .gw_devices ,
537- dev_dict ,
538- dev_id ,
539- item ,
540- key ,
541- notifs ,
542- )
539+ for device_id , device in self .gw_devices .items ():
540+ data = self ._get_device_data (device_id )
541+ if (
542+ "binary_sensors" in device
543+ and "plugwise_notification" in device ["binary_sensors" ]
544+ ):
545+ data ["binary_sensors" ]["plugwise_notification" ] = bool (
546+ self ._notifications
547+ )
548+
549+ device .update (data )
543550
544551 # Update for cooling
545- if dev_dict ["dev_class" ] in ZONE_THERMOSTATS :
546- self .update_for_cooling (dev_dict )
552+ if device ["dev_class" ] in ZONE_THERMOSTATS :
553+ self .update_for_cooling (device )
554+
555+ remove_empty_platform_dicts (device )
547556
548557 return PlugwiseData (self .gw_data , self .gw_devices )
549558
0 commit comments