1 """Support for iCloud sensors."""
3 from __future__
import annotations
16 from .account
import IcloudAccount, IcloudDevice
17 from .const
import DOMAIN
21 hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
23 """Set up device tracker for iCloud component."""
24 account: IcloudAccount = hass.data[DOMAIN][entry.unique_id]
29 """Update the values of the account."""
32 account.listeners.append(
41 """Add new tracker entities from the account."""
44 for dev_id, device
in account.devices.items():
45 if dev_id
in tracked
or device.battery_level
is None:
55 """Representation of a iCloud device battery sensor."""
57 _attr_device_class = SensorDeviceClass.BATTERY
58 _attr_native_unit_of_measurement = PERCENTAGE
59 _attr_should_poll =
False
60 _attr_has_entity_name =
True
62 def __init__(self, account: IcloudAccount, device: IcloudDevice) ->
None:
63 """Initialize the battery sensor."""
69 configuration_url=
"https://icloud.com/",
70 identifiers={(DOMAIN, device.unique_id)},
72 model=device.device_model,
78 """Battery state percentage."""
79 return self.
_device_device.battery_level
83 """Battery state icon handling."""
85 battery_level=self.
_device_device.battery_level,
86 charging=self.
_device_device.battery_status ==
"Charging",
91 """Return default attributes for the iCloud device entity."""
92 return self.
_device_device.extra_state_attributes
95 """Register state update callback."""
101 """Clean up after entity before removal."""
None async_will_remove_from_hass(self)
None __init__(self, IcloudAccount account, IcloudDevice device)
None async_added_to_hass(self)
dict[str, Any] extra_state_attributes(self)
int|None native_value(self)
None async_write_ha_state(self)
None async_setup_entry(HomeAssistant hass, ConfigEntry entry, AddEntitiesCallback async_add_entities)
def add_entities(account, async_add_entities, tracked)
Callable[[], None] async_dispatcher_connect(HomeAssistant hass, str signal, Callable[..., Any] target)
str icon_for_battery_level(int|None battery_level=None, bool charging=False)