LifeFlow/home-assistant/custom_components/lifeflow/sensor.py

95 lines
2.7 KiB
Python

"""LifeFlow sensors for Home Assistant."""
from __future__ import annotations
from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.config_entries import ConfigEntry
from homeassistant.core import HomeAssistant
from homeassistant.helpers.entity_platform import AddEntitiesCallback
from .const import (
DOMAIN,
SENSOR_DAILY_PROGRESS,
SENSOR_MEDICATION_TODAY,
SENSOR_SLEEP_QUALITY,
SENSOR_WATER_INTAKE,
SENSOR_CURRENT_STREAK,
SENSOR_TOTAL_POINTS,
)
SENSOR_TYPES: tuple[SensorEntityDescription, ...] = (
SensorEntityDescription(
key=SENSOR_MEDICATION_TODAY,
name="Medications Today",
icon="mdi:pill",
native_unit_of_measurement="taken",
),
SensorEntityDescription(
key=SENSOR_WATER_INTAKE,
name="Water Intake",
icon="mdi:water",
native_unit_of_measurement="mL",
),
SensorEntityDescription(
key=SENSOR_SLEEP_QUALITY,
name="Sleep Quality",
icon="mdi:sleep",
native_unit_of_measurement="%",
),
SensorEntityDescription(
key=SENSOR_DAILY_PROGRESS,
name="Daily Progress",
icon="mdi:chart-pie",
native_unit_of_measurement="%",
),
SensorEntityDescription(
key=SENSOR_CURRENT_STREAK,
name="Current Streak",
icon="mdi:fire",
native_unit_of_measurement="days",
),
SensorEntityDescription(
key=SENSOR_TOTAL_POINTS,
name="Total Points",
icon="mdi:star",
),
)
async def async_setup_entry(
hass: HomeAssistant,
entry: ConfigEntry,
async_add_entities: AddEntitiesCallback,
) -> None:
"""Set up LifeFlow sensors."""
entities = [LifeFlowSensor(entry, description) for description in SENSOR_TYPES]
async_add_entities(entities)
class LifeFlowSensor(SensorEntity):
"""LifeFlow sensor."""
def __init__(
self,
entry: ConfigEntry,
description: SensorEntityDescription,
) -> None:
"""Initialize the sensor."""
self.entity_description = description
self._entry = entry
self._attr_unique_id = f"{entry.entry_id}_{description.key}"
self._attr_name = f"LifeFlow {description.name}"
@property
def native_value(self) -> int | float | None:
"""Return the state of the sensor."""
# In production, this would fetch from the LifeFlow API/database
# For now, return example values
return {
SENSOR_MEDICATION_TODAY: 2,
SENSOR_WATER_INTAKE: 1500,
SENSOR_SLEEP_QUALITY: 85,
SENSOR_DAILY_PROGRESS: 65,
SENSOR_CURRENT_STREAK: 5,
SENSOR_TOTAL_POINTS: 2450,
}.get(self.entity_description.key, 0)