Open-source home automation platform running on Python 3.
69
Base classes and utilities for creating entities that represent devices, sensors, switches, and other controllable objects in Home Assistant. The entity framework provides standardized interfaces for state management, device information, and platform integration.
The fundamental base class that all Home Assistant entities inherit from, providing core functionality for state management, device integration, and entity lifecycle.
class Entity:
"""Base class for all Home Assistant entities."""
def __init__(self):
"""Initialize entity."""
@property
def entity_id(self) -> str:
"""Return entity ID."""
@property
def name(self) -> str:
"""Return name of entity."""
@property
def state(self) -> str:
"""Return state of the entity."""
@property
def should_poll(self) -> bool:
"""Return True if entity should be polled for updates."""
@property
def unique_id(self) -> str:
"""Return unique ID for entity."""
@property
def device_info(self) -> dict:
"""Return device information for entity."""
@property
def available(self) -> bool:
"""Return True if entity is available."""
@property
def assumed_state(self) -> bool:
"""Return True if unable to access real state of entity."""
@property
def force_update(self) -> bool:
"""Return True if state updates should be forced."""
@property
def supported_features(self) -> int:
"""Flag supported features."""
@property
def device_class(self) -> str:
"""Return device class of entity."""
@property
def unit_of_measurement(self) -> str:
"""Return unit of measurement of entity."""
@property
def icon(self) -> str:
"""Return icon to use in frontend."""
@property
def entity_picture(self) -> str:
"""Return picture to use for entity."""
@property
def hidden(self) -> bool:
"""Return True if entity should be hidden from UI."""
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if entity should be enabled by default in entity registry."""
@property
def entity_category(self) -> str:
"""Return category of entity."""
@property
def extra_state_attributes(self) -> dict:
"""Return entity specific state attributes."""
@property
def state_attributes(self) -> dict:
"""Return state attributes."""
async def async_added_to_hass(self) -> None:
"""Run when entity about to be added to hass."""
async def async_will_remove_from_hass(self) -> None:
"""Run when entity will be removed from hass."""
async def async_update(self) -> None:
"""Update the entity."""
def update(self) -> None:
"""Update the entity (sync version)."""
def schedule_update_ha_state(self, force_refresh: bool = False) -> None:
"""Schedule update of Home Assistant state.
Args:
force_refresh: Force entity to refresh
"""
async def async_schedule_update_ha_state(self, force_refresh: bool = False) -> None:
"""Schedule async update of Home Assistant state.
Args:
force_refresh: Force entity to refresh
"""Base class for entities that can be turned on and off, such as switches, lights, and other controllable devices.
class ToggleEntity(Entity):
"""Base class for entities that can be turned on/off."""
@property
def is_on(self) -> bool:
"""Return True if entity is on."""
def turn_on(self, **kwargs) -> None:
"""Turn entity on."""
def turn_off(self, **kwargs) -> None:
"""Turn entity off."""
def toggle(self, **kwargs) -> None:
"""Toggle entity."""
async def async_turn_on(self, **kwargs) -> None:
"""Turn entity on asynchronously."""
async def async_turn_off(self, **kwargs) -> None:
"""Turn entity off asynchronously."""
async def async_toggle(self, **kwargs) -> None:
"""Toggle entity asynchronously."""Specialized entity base classes for different device types and platforms.
# Sensor entities
class SensorEntity(Entity):
"""Base class for sensor entities."""
@property
def native_value(self) -> Union[str, int, float, None]:
"""Return native value of sensor."""
@property
def native_unit_of_measurement(self) -> str:
"""Return native unit of measurement."""
class BinarySensorEntity(Entity):
"""Base class for binary sensor entities."""
@property
def is_on(self) -> bool:
"""Return True if binary sensor is on."""
# Control entities
class SwitchEntity(ToggleEntity):
"""Base class for switch entities."""
class LightEntity(ToggleEntity):
"""Base class for light entities."""
@property
def brightness(self) -> int:
"""Return brightness of light (0-255)."""
@property
def hs_color(self) -> tuple[float, float]:
"""Return hue and saturation color value."""
@property
def color_temp(self) -> int:
"""Return color temperature in mireds."""
class FanEntity(ToggleEntity):
"""Base class for fan entities."""
@property
def speed(self) -> str:
"""Return current speed."""
@property
def speed_list(self) -> list[str]:
"""Return list of available speeds."""
class CoverEntity(Entity):
"""Base class for cover entities."""
@property
def current_cover_position(self) -> int:
"""Return current position (0-100)."""
@property
def is_closed(self) -> bool:
"""Return True if cover is closed."""
def open_cover(self, **kwargs) -> None:
"""Open cover."""
def close_cover(self, **kwargs) -> None:
"""Close cover."""
def stop_cover(self, **kwargs) -> None:
"""Stop cover."""
class ClimateEntity(Entity):
"""Base class for climate entities."""
@property
def current_temperature(self) -> float:
"""Return current temperature."""
@property
def target_temperature(self) -> float:
"""Return target temperature."""
@property
def hvac_mode(self) -> str:
"""Return current operation mode."""
@property
def hvac_modes(self) -> list[str]:
"""Return available operation modes."""Utilities for generating and managing entity IDs and entity creation.
def generate_entity_id(entity_id_format: str, name: str,
current_ids: list[str] = None, hass: HomeAssistant = None) -> str:
"""Generate unique entity ID.
Args:
entity_id_format: Format string for entity ID (e.g., 'sensor.{}')
name: Name to base entity ID on
current_ids: List of existing entity IDs
hass: Home Assistant instance
Returns:
Generated entity ID
"""
async def async_generate_entity_id(entity_id_format: str, name: str,
current_ids: list[str] = None,
hass: HomeAssistant = None) -> str:
"""Generate unique entity ID asynchronously.
Args:
entity_id_format: Format string for entity ID (e.g., 'sensor.{}')
name: Name to base entity ID on
current_ids: List of existing entity IDs
hass: Home Assistant instance
Returns:
Generated entity ID
"""
def get_capability(hass: HomeAssistant, entity_id: str, capability: str) -> Any:
"""Get capability of an entity.
Args:
hass: Home Assistant instance
entity_id: Entity ID
capability: Capability name
Returns:
Capability value or None
"""
def get_device_class(hass: HomeAssistant, entity_id: str) -> str:
"""Get device class of an entity.
Args:
hass: Home Assistant instance
entity_id: Entity ID
Returns:
Device class or None
"""
def get_supported_features(hass: HomeAssistant, entity_id: str) -> int:
"""Get supported features of an entity.
Args:
hass: Home Assistant instance
entity_id: Entity ID
Returns:
Supported features bitmask
"""Base class for entity platforms that manage collections of related entities.
class EntityPlatform:
"""Platform for managing a group of entities."""
def __init__(self, hass: HomeAssistant, logger: logging.Logger, domain: str,
platform_name: str, platform: Any, scan_interval: timedelta,
entity_namespace: str):
"""Initialize entity platform.
Args:
hass: Home Assistant instance
logger: Logger for platform
domain: Entity domain
platform_name: Platform name
platform: Platform module
scan_interval: Update scan interval
entity_namespace: Entity namespace
"""
async def async_setup(self, platform_config: dict,
discovery_info: dict = None) -> None:
"""Set up entity platform.
Args:
platform_config: Platform configuration
discovery_info: Discovery information
"""
async def async_reset(self) -> None:
"""Reset platform."""
async def async_shutdown(self) -> None:
"""Shutdown platform."""
async def async_add_entities(self, new_entities: list[Entity],
update_before_add: bool = False) -> None:
"""Add entities to platform.
Args:
new_entities: List of entities to add
update_before_add: Update entities before adding
"""
async def async_remove_entity(self, entity_id: str) -> None:
"""Remove entity from platform.
Args:
entity_id: Entity ID to remove
"""from typing import Any, Dict, List, Optional, Union
from enum import Enum
# Entity categories
class EntityCategory(Enum):
"""Category of entity."""
CONFIG = "config"
DIAGNOSTIC = "diagnostic"
# Device classes (examples for major domains)
class SensorDeviceClass(Enum):
"""Device class for sensors."""
TEMPERATURE = "temperature"
HUMIDITY = "humidity"
PRESSURE = "pressure"
BATTERY = "battery"
ENERGY = "energy"
POWER = "power"
class BinarySensorDeviceClass(Enum):
"""Device class for binary sensors."""
BATTERY = "battery"
COLD = "cold"
CONNECTIVITY = "connectivity"
DOOR = "door"
GARAGE_DOOR = "garage_door"
HEAT = "heat"
LIGHT = "light"
LOCK = "lock"
MOISTURE = "moisture"
MOTION = "motion"
MOVING = "moving"
OCCUPANCY = "occupancy"
OPENING = "opening"
PLUG = "plug"
POWER = "power"
PRESENCE = "presence"
PROBLEM = "problem"
RUNNING = "running"
SAFETY = "safety"
SMOKE = "smoke"
SOUND = "sound"
UPDATE = "update"
VIBRATION = "vibration"
WINDOW = "window"
# Type aliases
EntityIdType = str
StateType = Union[None, str, int, float, bool]
AttributesType = Dict[str, Any]
DeviceInfoType = Dict[str, Any]
SupportedFeaturesType = intInstall with Tessl CLI
npx tessl i tessl/pypi-homeassistantdocs
evals
scenario-1
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
scenario-10