Files
ha-babytracker/custom_components/babytracker/entity.py
T
arneandClaude Opus 5.5 6466f002e0 Add Baby Tracker client and Home Assistant integration
Python client for the Baby Tracker sync server, reverse engineered from
the Android app (4.53), and a HACS-installable Home Assistant integration
built on it. Each baby gets a log calendar and summary sensors, and actions
add entries that sync back to the group.

Reading has been run against the real server. Uploading and running inside
real Home Assistant have not.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-04 10:04:39 +00:00

49 lines
1.6 KiB
Python

"""Base entity for a baby."""
from __future__ import annotations
from homeassistant.core import callback
from homeassistant.helpers.device_registry import DeviceEntryType, DeviceInfo
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from .babytracker_api import Baby
from .const import DOMAIN
from .coordinator import BabyTrackerCoordinator
class BabyEntity(CoordinatorEntity[BabyTrackerCoordinator]):
"""An entity on a baby's device."""
_attr_has_entity_name = True
def __init__(self, coordinator: BabyTrackerCoordinator, baby: Baby, key: str) -> None:
super().__init__(coordinator)
self.baby_id = baby.id
self._attr_unique_id = f"{baby.id}_{key}"
self._attr_translation_key = key
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, baby.id)},
name=baby.name,
manufacturer="Baby Tracker",
entry_type=DeviceEntryType.SERVICE,
)
@property
def available(self) -> bool:
return super().available and self.baby_id in self.coordinator.data.babies
def async_add_baby_entities(entry, coordinator: BabyTrackerCoordinator, async_add_entities, create) -> None:
"""Add entities for each baby now, and for babies that appear later."""
known: set[str] = set()
@callback
def add_new() -> None:
new = [baby for baby_id, baby in coordinator.data.babies.items() if baby_id not in known]
if new:
known.update(baby.id for baby in new)
async_add_entities([entity for baby in new for entity in create(baby)])
add_new()
entry.async_on_unload(coordinator.async_add_listener(add_new))