11"""Support for Gicisky binary sensors."""
22
33from __future__ import annotations
4- from functools import partial
4+ import logging
55from .gicisky_ble import (
66 BinarySensorDeviceClass as GiciskyBinarySensorDeviceClass ,
77 SensorUpdate ,
1616 PassiveBluetoothDataUpdate ,
1717 PassiveBluetoothProcessorEntity ,
1818)
19- from homeassistant .core import HomeAssistant
19+ from homeassistant .core import HomeAssistant , callback
2020from homeassistant .helpers .entity_platform import AddEntitiesCallback
2121from homeassistant .helpers .sensor import sensor_device_info_to_hass_device_info
22-
22+ from homeassistant .helpers .device_registry import DeviceInfo , CONNECTION_BLUETOOTH
23+ from homeassistant .helpers .update_coordinator import CoordinatorEntity
24+ from homeassistant .helpers .update_coordinator import DataUpdateCoordinator
25+ from homeassistant .config_entries import ConfigEntry
26+ from propcache .api import cached_property
2327from .coordinator import GiciskyPassiveBluetoothDataProcessor
2428from .device import device_key_to_bluetooth_entity_key
2529from .types import GiciskyConfigEntry
30+ from .const import (
31+ DOMAIN
32+ )
33+
34+ _LOGGER = logging .getLogger (__name__ )
2635
2736BINARY_SENSOR_DESCRIPTIONS = {
2837 GiciskyBinarySensorDeviceClass .BATTERY : BinarySensorEntityDescription (
@@ -185,6 +194,8 @@ async def async_setup_entry(
185194 coordinator .async_register_processor (processor , BinarySensorEntityDescription )
186195 )
187196
197+ connectivity_coordinator = hass .data [DOMAIN ][entry .entry_id ]["connectivity_coordinator" ]
198+ async_add_entities ([GiciskyBluetoothConnectivitySensorEntity (hass , entry , connectivity_coordinator )])
188199
189200class GiciskyBluetoothBinarySensorEntity (
190201 PassiveBluetoothProcessorEntity [GiciskyPassiveBluetoothDataProcessor [bool | None ]],
@@ -202,8 +213,55 @@ def available(self) -> bool:
202213 """Return True if entity is available."""
203214 return super ().available
204215
205- async def async_added_to_hass (self ) -> None :
206- await super ().async_added_to_hass ()
207- poll_coordinator = self .processor .coordinator .poll_coordinator
208- remove = poll_coordinator .async_add_listener (partial (self .processor .async_handle_update , poll_coordinator .data ))
209- self .async_on_remove (remove )
216+ class GiciskyBluetoothConnectivitySensorEntity (
217+ CoordinatorEntity [DataUpdateCoordinator [bool ]],
218+ BinarySensorEntity ,
219+ ):
220+ _attr_device_class = BinarySensorDeviceClass .CONNECTIVITY
221+
222+ def __init__ (self , hass : HomeAssistant , entry : ConfigEntry , coordinator : DataUpdateCoordinator [bool ]):
223+ CoordinatorEntity .__init__ (self , coordinator )
224+ # self.hass = hass
225+ address = hass .data [DOMAIN ][entry .entry_id ]['address' ]
226+ self ._address = address
227+ self ._identifier = address .replace (":" , "" )[- 8 :]
228+ self ._attr_name = f"Gicisky { self ._identifier } Connectivity"
229+ self ._attr_unique_id = f"gicisky_{ self ._identifier } _connectivity"
230+ self ._is_on = False
231+
232+ """Representation of a Gicisky binary sensor."""
233+ @property
234+ def is_on (self ) -> bool | None :
235+ """Return the native value."""
236+ return self ._is_on
237+
238+ @property
239+ def device_info (self ) -> DeviceInfo :
240+ return DeviceInfo (
241+ connections = {
242+ (
243+ CONNECTION_BLUETOOTH ,
244+ self ._address ,
245+ )
246+ },
247+ name = f"Gicisky { self ._identifier } " ,
248+ manufacturer = "Gicisky" ,
249+ )
250+
251+ @cached_property
252+ def available (self ) -> bool :
253+ """Entity always either data or empty."""
254+ return True
255+
256+ @property
257+ def data (self ) -> bytes :
258+ """Return coordinator data for this entity."""
259+ return self .coordinator .data
260+
261+
262+ @callback
263+ def _handle_coordinator_update (self ) -> None :
264+ """Handle updated data from the coordinator."""
265+ _LOGGER .debug ("Updated binary data" )
266+ self ._is_on = self .data
267+ super ()._handle_coordinator_update ()
0 commit comments