@@ -16,6 +16,7 @@ def __init__(self, hass: HomeAssistant, vhh: VirtualHousehold):
1616 self ._vhh = vhh
1717 self .plugs = dict ()
1818 self ._known_plugs = set ()
19+ self ._known_plug_names = dict ()
1920 self .sensors = dict ()
2021 self .on_start_sensor_queue = dict ()
2122 self ._unsubscribe_from_signals = [
@@ -43,7 +44,8 @@ def __init__(self, hass: HomeAssistant, vhh: VirtualHousehold):
4344
4445 async def enqueue_plug_for_adding (self , network_info : dict ):
4546 _LOGGER .debug (f"Adding to plug processing queue: { network_info } " )
46- await self ._plug_added_queue .add ((network_info ['mac' ], network_info ['host' ], network_info ['port' ]))
47+ await self ._plug_added_queue .add ((network_info ['mac' ], network_info ['host' ],
48+ network_info ['port' ], network_info ['name' ]))
4749
4850 async def process_plug_queue (self ):
4951 """Start the background task if not already running."""
@@ -53,22 +55,22 @@ async def process_plug_queue(self):
5355 self ._monitor_add_plug_queue = self ._hass .async_create_background_task (self ._monitor_plug_queue (), name = "plug_queue_monitor" )
5456 _LOGGER .debug ("Background task started" )
5557
56- def _plug_has_been_seen (self , mac_address )-> bool :
57- return mac_address in self .plugs or mac_address in self ._known_plugs
58+ def _plug_has_been_seen (self , mac_address , name )-> bool :
59+ return mac_address in self .plugs or mac_address in self ._known_plugs or name in self . _known_plug_names
5860
5961 async def _monitor_plug_queue (self ):
6062 """The actual background task loop."""
6163 try :
6264 while not self ._stop_task and self ._plug_added_queue :
6365 queue_snapshot = await self ._plug_added_queue .copy ()
64- for mac_address , host , port in queue_snapshot :
65- if not self ._plug_has_been_seen (mac_address ):
66+ for mac_address , host , port , name in queue_snapshot :
67+ if not self ._plug_has_been_seen (mac_address , name ):
6668 async_dispatcher_send (self ._hass , f"{ DOMAIN } _create_plug" ,
67- mac_address , host , port )
69+ mac_address , host , port , name )
6870 else :
6971 _LOGGER .debug (f"Plug: { mac_address } has already been created as an entity in Home Assistant."
7072 f" Skipping and flushing from queue." )
71- await self ._plug_added_queue .remove ((mac_address , host , port ))
73+ await self ._plug_added_queue .remove ((mac_address , host , port , name ))
7274
7375
7476 await asyncio .sleep (5 )
@@ -94,11 +96,12 @@ async def stop_processing_plug_queue(self):
9496 _LOGGER .debug ("Background task stopped" )
9597 self ._monitor_add_plug_queue = None
9698
97- def _create_api (self , mac_address , ip , port ):
98- _LOGGER .info (f"Adding API for mac={ mac_address } , ip={ ip } , port={ port } " )
99+ def _create_api (self , mac_address , ip , port , name ):
100+ _LOGGER .info (f"Creating API for mac={ mac_address } , ip={ ip } , port={ port } " )
99101 api = PlugApi (mac = mac_address , ip = ip , port = port )
100102 self .plugs [mac_address ] = api
101103 self ._known_plugs .add (mac_address )
104+ self ._known_plug_names [name ] = mac_address
102105 known_evs = [
103106 'exception' ,
104107 'average_flow' ,
@@ -117,7 +120,9 @@ def _create_api(self, mac_address, ip, port):
117120 api .connect ()
118121
119122 def add_api (self , network_info ):
120- self ._create_api (mac_address = network_info ['mac' ], ip = network_info ['host' ], port = network_info ['port' ])
123+ _LOGGER .debug ("Manually adding API, this could cause API's and entities to get out of sync" )
124+ self ._create_api (mac_address = network_info ['mac' ], ip = network_info ['host' ],
125+ port = network_info ['port' ], name = network_info ['name' ])
121126
122127
123128 async def handle_message (self , event : str , message : dict ):
@@ -152,49 +157,59 @@ async def disconnect(self):
152157 def _acknowledge_sensor_added_to_homeassistant (self ,mac , role ):
153158 self .sensors [mac ] = role
154159
155- @ callback
156- async def _acknowledge_plug_added_to_homeassistant ( self , mac_address , host , port ):
157- self ._create_api (mac_address , host , port )
158- await self ._plug_added_queue .remove ((mac_address , host , port ))
160+ async def _acknowledge_plug_added_to_homeassistant ( self , mac_address , host , port , name ):
161+ _LOGGER . info ( f"Adding new API for mac= { mac_address } , ip= { host } , port= { port } " )
162+ self ._create_api (mac_address , host , port , name )
163+ await self ._plug_added_queue .remove ((mac_address , host , port , name ))
159164
160- @callback
161165 async def _plug_added (self , info ):
162166 _LOGGER .debug (f" Request to add plug received: { info } " )
163167 network_info = dict ()
164168 network_info ['mac' ] = info ['properties' ][b'id' ].decode ('utf-8' )
165169 network_info ['host' ] = info ['addresses' ][0 ]
166170 network_info ['port' ] = info ['port' ]
171+ network_info ['name' ] = info ['name' ]
167172
168173 if self ._safe_to_process_plug_queue :
169174 await self .enqueue_plug_for_adding (network_info )
170175 await self .process_plug_queue ()
171176 else :
172177 await self .enqueue_plug_for_adding (network_info )
173178
174- @callback
175179 async def _plug_updated (self , info ):
176180 _LOGGER .debug (f" Request to update plug received: { info } " )
177181 mac = info ['properties' ][b'id' ].decode ('utf-8' )
178182 host = info ['addresses' ][0 ]
179183 port = info ['port' ]
184+ name = info ['name' ]
185+
180186 if mac in self .plugs :
181- self .plugs [mac ].disconnect ()
187+ current_api : PlugApi = self .plugs [mac ]
188+ if current_api ._listener ._ip == host and current_api ._listener ._port == port :
189+ _LOGGER .info (f"Request to update plug with mac { mac } does not alter ip from existing API."
190+ f"IP still { host } and port is { port } . Skipping update..." )
191+ return
192+ await current_api .disconnect ()
182193
183194 if mac in self ._known_plugs :
184- self ._create_api (mac , host , port )
195+ self ._create_api (mac , host , port , name )
185196 else :
186197 network_info = dict ()
187198 network_info ['mac' ] = mac
188199 network_info ['host' ] = host
189200 network_info ['port' ] = port
201+ network_info ['name' ] = name
190202 await self .enqueue_plug_for_adding (network_info )
191203 await self .process_plug_queue ()
192204
193- @callback
194- def _plug_remove (self ,name , info ):
205+ async def _plug_remove (self ,name , info ):
195206 _LOGGER .debug (f" Request to delete plug received: { info } " )
196- mac = info ['properties' ][b'id' ].decode ('utf-8' )
197- if mac in self .plugs :
198- self .plugs [mac ].disconnect ()
207+ if name in self ._known_plug_names :
208+ mac = self ._known_plug_names [name ]
209+ if mac in self .plugs :
210+ await self .plugs [mac ].disconnect ()
199211
200- del self .plugs [mac ]
212+ del self .plugs [mac ]
213+ else :
214+ _LOGGER .warning (f"Received request to delete api for gateway with name [{ name } ], but this name"
215+ f"is not associated with an existing PlugAPI. Ignoring..." )
0 commit comments