@@ -79,7 +79,7 @@ async def async_setup_entry(hass: HomeAssistant, entry: GiciskyConfigEntry) -> b
7979
8080 device_registry = dr .async_get (hass )
8181 event_classes = set (entry .data .get (CONF_DISCOVERED_EVENT_CLASSES , ()))
82- coordinator = GiciskyPassiveBluetoothProcessorCoordinator (
82+ bt_coordinator = GiciskyPassiveBluetoothProcessorCoordinator (
8383 hass ,
8484 _LOGGER ,
8585 address = address ,
@@ -91,26 +91,24 @@ async def async_setup_entry(hass: HomeAssistant, entry: GiciskyConfigEntry) -> b
9191 entry = entry ,
9292 )
9393
94- async def _async_poll_data () -> SensorUpdate :
94+ async def _async_poll_data (hass : HomeAssistant , entry : GiciskyConfigEntry , ) -> SensorUpdate :
9595 try :
96- device = async_ble_device_from_address (hass , address )
97- if not device :
98- raise UpdateFailed ("BLE Device none" )
99- sensor = await data .async_poll (device )
100- return sensor
96+ coordinator = entry .runtime_data
97+ return await coordinator .device_data .async_poll ()
10198 except Exception as err :
10299 raise UpdateFailed (f"polling error: { err } " ) from err
103100
104101 poll_coordinator = DataUpdateCoordinator [SensorUpdate ](
105102 hass ,
106103 _LOGGER ,
107104 name = DOMAIN ,
108- update_method = _async_poll_data ,
105+ update_method = partial ( _async_poll_data , hass , entry ) ,
109106 update_interval = timedelta (hours = 24 ),
110107 )
111108
112- entry .runtime_data = coordinator
109+ entry .runtime_data = bt_coordinator
113110 entry .runtime_data .poll_coordinator = poll_coordinator
111+ hass .data [DOMAIN ][entry .entry_id ]['poll_coordinator' ] = poll_coordinator
114112 await poll_coordinator .async_config_entry_first_refresh ()
115113 await hass .config_entries .async_forward_entry_setups (entry , PLATFORMS )
116114
@@ -129,14 +127,15 @@ async def writeservice(service: ServiceCall) -> None:
129127 entry_id = await get_entry_id_from_device (hass , device_id )
130128 address = hass .data [DOMAIN ][entry_id ]['address' ]
131129 data = hass .data [DOMAIN ][entry_id ]['data' ]
130+ coordinator = hass .data [DOMAIN ][entry_id ]['poll_coordinator' ]
132131 ble_device = async_ble_device_from_address (hass , address )
133132 threshold = int (service .data .get ("threshold" , 128 ))
134133 red_threshold = int (service .data .get ("red_threshold" , 128 ))
135134 image = await hass .async_add_executor_job (customimage , entry_id , data .device , service , hass )
136135
137136 max_retries = 3
138137 await data .set_connected (True )
139- await poll_coordinator .async_refresh ()
138+ await coordinator .async_refresh ()
140139 for attempt in range (1 , max_retries + 1 ):
141140 success = await update_image (ble_device , data .device , image , threshold , red_threshold )
142141 if success :
@@ -148,16 +147,16 @@ async def writeservice(service: ServiceCall) -> None:
148147 await sleep (1 )
149148 else :
150149 await data .set_connected (False )
151- await poll_coordinator .async_refresh ()
150+ await coordinator .async_refresh ()
152151 raise HomeAssistantError (f"Failed to write to { address } after { max_retries } attempts" )
153152 await data .set_connected (False )
154- await poll_coordinator .async_refresh ()
153+ await coordinator .async_refresh ()
155154
156155 # register the services
157156 hass .services .async_register (DOMAIN , "write" , writeservice )
158157
159158 # only start after all platforms have had a chance to subscribe
160- entry .async_on_unload (coordinator .async_start ())
159+ entry .async_on_unload (bt_coordinator .async_start ())
161160 return True
162161
163162
0 commit comments