2525 async_dispatcher_connect ,
2626 async_dispatcher_send ,
2727)
28- from homeassistant .helpers .entity import Entity
2928
3029from .const import DISCOVERY_TOPIC , DOMAIN , LOGGER
31- from .device_sensor import PGLabDeviceSensor
30+ from .coordinator import PGLabSensorsCoordinator
3231
3332if TYPE_CHECKING :
34- from . import PGLABConfigEntry
33+ from . import PGLabConfigEntry
3534
3635# Supported platforms.
3736PLATFORMS = [
@@ -69,23 +68,28 @@ def get_device_id_from_discovery_topic(topic: str) -> str | None:
6968class DiscoverDeviceInfo :
7069 """Keeps information of the PGLab discovered device."""
7170
72- def __init__ (self , pglab_device : PyPGLabDevice ) -> None :
71+ def __init__ (
72+ self ,
73+ hass : HomeAssistant ,
74+ config_entry : PGLabConfigEntry ,
75+ pglab_device : PyPGLabDevice ,
76+ ) -> None :
7377 """Initialize the device discovery info."""
7478
7579 # Hash string represents the devices actual configuration,
7680 # it depends on the number of available relays and shutters.
7781 # When the hash string changes the devices entities must be rebuilt.
7882 self ._hash = pglab_device .hash
7983 self ._entities : list [tuple [str , str ]] = []
80- self ._sensors = PGLabDeviceSensor ( pglab_device )
84+ self .coordinator = PGLabSensorsCoordinator ( hass , config_entry , pglab_device )
8185
82- def add_entity (self , entity : Entity ) -> None :
86+ def add_entity (self , platform_domain : str , entity_unique_id : str | None ) -> None :
8387 """Add an entity."""
8488
8589 # PGLabEntity always have unique IDs
8690 if TYPE_CHECKING :
87- assert entity . unique_id is not None
88- self ._entities .append ((entity . platform . domain , entity . unique_id ))
91+ assert entity_unique_id is not None
92+ self ._entities .append ((platform_domain , entity_unique_id ))
8993
9094 @property
9195 def hash (self ) -> int :
@@ -97,18 +101,15 @@ def entities(self) -> list[tuple[str, str]]:
97101 """Return array of entities available."""
98102 return self ._entities
99103
100- @property
101- def sensors (self ) -> PGLabDeviceSensor :
102- """Return the PGLab device sensor."""
103- return self ._sensors
104-
105104
106- async def createDiscoverDeviceInfo (pglab_device : PyPGLabDevice ) -> DiscoverDeviceInfo :
105+ async def create_discover_device_info (
106+ hass : HomeAssistant , config_entry : PGLabConfigEntry , pglab_device : PyPGLabDevice
107+ ) -> DiscoverDeviceInfo :
107108 """Create a new DiscoverDeviceInfo instance."""
108- discovery_info = DiscoverDeviceInfo (pglab_device )
109+ discovery_info = DiscoverDeviceInfo (hass , config_entry , pglab_device )
109110
110111 # Subscribe to sensor state changes.
111- await discovery_info .sensors .subscribe_topics ()
112+ await discovery_info .coordinator .subscribe_topics ()
112113 return discovery_info
113114
114115
@@ -184,7 +185,10 @@ def __clean_discovered_device(self, hass: HomeAssistant, device_id: str) -> None
184185 del self ._discovered [device_id ]
185186
186187 async def start (
187- self , hass : HomeAssistant , mqtt : PyPGLabMqttClient , entry : PGLABConfigEntry
188+ self ,
189+ hass : HomeAssistant ,
190+ mqtt : PyPGLabMqttClient ,
191+ config_entry : PGLabConfigEntry ,
188192 ) -> None :
189193 """Start discovering a PGLab devices."""
190194
@@ -210,7 +214,7 @@ async def discovery_message_received(msg: ReceiveMessage) -> None:
210214 # Create a new device.
211215 device_registry = dr .async_get (hass )
212216 device_registry .async_get_or_create (
213- config_entry_id = entry .entry_id ,
217+ config_entry_id = config_entry .entry_id ,
214218 configuration_url = f"http://{ pglab_device .ip } /" ,
215219 connections = {(CONNECTION_NETWORK_MAC , pglab_device .mac )},
216220 identifiers = {(DOMAIN , pglab_device .id )},
@@ -241,7 +245,9 @@ async def discovery_message_received(msg: ReceiveMessage) -> None:
241245 self .__clean_discovered_device (hass , pglab_device .id )
242246
243247 # Add a new device.
244- discovery_info = await createDiscoverDeviceInfo (pglab_device )
248+ discovery_info = await create_discover_device_info (
249+ hass , config_entry , pglab_device
250+ )
245251 self ._discovered [pglab_device .id ] = discovery_info
246252
247253 # Create all new relay entities.
@@ -256,7 +262,7 @@ async def discovery_message_received(msg: ReceiveMessage) -> None:
256262 hass ,
257263 CREATE_NEW_ENTITY [Platform .SENSOR ],
258264 pglab_device ,
259- discovery_info .sensors ,
265+ discovery_info .coordinator ,
260266 )
261267
262268 topics = {
@@ -267,7 +273,7 @@ async def discovery_message_received(msg: ReceiveMessage) -> None:
267273 }
268274
269275 # Forward setup all HA supported platforms.
270- await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
276+ await hass .config_entries .async_forward_entry_setups (config_entry , PLATFORMS )
271277
272278 self ._mqtt_client = mqtt
273279 self ._substate = async_prepare_subscribe_topics (hass , self ._substate , topics )
@@ -282,22 +288,24 @@ async def register_platform(
282288 )
283289 self ._disconnect_platform .append (disconnect_callback )
284290
285- async def stop (self , hass : HomeAssistant , entry : PGLABConfigEntry ) -> None :
291+ async def stop (self , hass : HomeAssistant , config_entry : PGLabConfigEntry ) -> None :
286292 """Stop to discovery PG LAB devices."""
287- await hass .config_entries .async_unload_platforms (entry , PLATFORMS )
293+ await hass .config_entries .async_unload_platforms (config_entry , PLATFORMS )
288294
289295 # Disconnect all registered platforms.
290296 for disconnect_callback in self ._disconnect_platform :
291297 disconnect_callback ()
292298
293299 async_unsubscribe_topics (hass , self ._substate )
294300
295- async def add_entity (self , entity : Entity , device_id : str ):
301+ async def add_entity (
302+ self , platform_domain : str , entity_unique_id : str | None , device_id : str
303+ ):
296304 """Save a new PG LAB device entity."""
297305
298306 # Be sure that the device is been discovered.
299307 if device_id not in self ._discovered :
300308 raise PGLabDiscoveryError ("Unknown device, device_id not discovered" )
301309
302310 discovery_info = self ._discovered [device_id ]
303- discovery_info .add_entity (entity )
311+ discovery_info .add_entity (platform_domain , entity_unique_id )
0 commit comments