Skip to content

ESPHome Native API

PHOS supports explicitly configured ESPHome devices over the encrypted Native API. It reads supported sensor inventory and states; it does not proxy through Home Assistant or expose ESPHome device-control services. Numeric and supported binary entities become PHOS observations only after an administrator creates a mapping and separately authorizes it.

This guide is the current reference for the ESPHome Native API path. The earlier 05A foundation was extended by mapping, authorization, Web Admin, Physical Context, and MCP work. The separate ESPHome over MQTT walkthrough describes the MQTT transport.

For the task-oriented Web Admin workflow and a device example, see the ESPHome Native API user guide.

Install and configure

Install the optional ESPHome dependencies (Python 3.11 or later):

pip install 'phos-robot[esphome]'

From a source checkout:

.venv/bin/python -m pip install -e '.[esphome]'

ESPHome is disabled by default. In Connectivity → ESPHome, use Add device to enter a name, optional zone, host/IP, port, and encryption key. The key field is write-only: PHOS validates and stores the Noise key through its existing encrypted secret service and does not return it to the page. A new device starts disabled. Enable it and use Test connection to check the encrypted endpoint; a successful test is not a claim that the long-lived provider is connected.

The canonical esphome configuration remains secret-free. For example, the following is a documentation-only device shape using a reserved example address and fictitious IDs. No encryption-key value appears in configuration:

esphome:
  enabled: true
  devices:
    - id: example-room
      display_name: Example room
      zone: salotto
      enabled: true
      host: 192.0.2.10
      port: 6053
      encryption_key_ref: esphome.example_room_key
      timeout_seconds: 10
      retry_initial_seconds: 1
      retry_max_seconds: 30
      mappings: []

zone is optional and backward-compatible. Missing, null, or blank input means no configured zone; non-empty values are trimmed and validated as descriptive text (up to 128 characters). PHOS does not impose a room taxonomy or infer a zone from a device name, host, MAC address, or discovery metadata. Changing the zone does not change provider, entity, source, mapping, or authorization identity, and does not require a Native API connection restart. The same free-form descriptive convention is used for MQTT source zones; there is no separate ESPHome zone taxonomy.

Configuration supports up to eight explicitly configured devices and up to 64 mappings per device. A mapping records its stable PHOS mapping ID, Native API entity key and type, human-readable name, semantic quantity, canonical unit (for numeric entities), freshness threshold, authorization state, and binary value map where applicable. Numeric mappings use the canonical quantity/unit pairs:

Quantity Unit Quantity Unit
temperature_c c humidity_percent %rh
pressure_hpa hpa eco2_ppm ppm
co2_ppm ppm tvoc_ppb ppb
aqi_uba index aht21_temperature_c c
illuminance_lux lx distance_m m
particulate_ug_m3 ug/m3 voc_ppb ppb
pm1_ug_m3 ug/m3 pm2_5_ug_m3 ug/m3
pm10_ug_m3 ug/m3 acceleration_m_s2 m/s2
angular_velocity_dps deg/s vibration_m_s2 m/s2

The mapping service validates and derives canonical units; clients cannot authorize arbitrary quantity/unit pairs. Supported binary quantities are presence, occupancy, motion, contact, and proximity. Binary Native API states use fixed source identities true and false; their PHOS meanings are selected in the mapping editor. Unsupported inventory remains visible to administration but cannot be mapped into observations.

Web Admin workflow

The ESPHome page is a manually managed configured-device list; it does not present dynamic mDNS or MQTT-announcement discovery candidates or proposal controls. Internal discovery/candidate services do not constitute a supported user-facing workflow in this page. Add a device with its Noise key, enable it, and test the connection. In Measurements, select Edit measurement for an inventory entity, choose its semantic mapping, and save selected mappings. Saving creates or edits the mapping but does not authorize it. Use the separate Authorize action to expose eligible values; Revoke removes that permission without deleting the mapping. New or edited mappings remain unauthorized until separately approved. Authorization checks are enforced again server-side against the persisted configuration and current provider lifecycle state.

The entity editor and device configuration use revision-checked, CSRF-protected administrative forms. Mapping save is one native HTML POST per selected batch; unselected entities are not committed. Device removal, enable/disable, key replacement, authorization, and revocation remain distinct operations. Shared credential replacement discloses and confirms the affected device scope.

Saved mappings remain visible in Measurements while a device is offline. The table's observation value is shown only when the device is enabled and connected and the authorized observation is available and fresh. Reapproving a revoked mapping requires a new eligible observation; revocation does not reveal the old value again. Disabled or disconnected ESPHome devices do not contribute a current observation to MCP; the persisted mapping remains visible in Web Admin.

Observation and MCP projection

Authorized ESPHome observations enter the shared physical observation ingress and appear in phos_get_physical_context and the phos://physical-context Resource under external_observations. They do not overwrite native sensor snapshots or enter native measurement history. The external projection includes provider/source/entity identity, quantity, value, unit, status, availability, freshness, timing and provenance fields. ESPHome records also include device_name from the configured device display name; display_name is the configured mapping name. zone comes only from the configured device zone and is null when not configured.

For example, the following is a sanitized, illustrative subset of one fresh authorized observation (the actual document also includes source, provider, entity, freshness and timing metadata):

{
  "device_name": "Example room sensor",
  "display_name": "Room temperature",
  "zone": "salotto",
  "quantity": "temperature_c",
  "value": 21.0,
  "unit": "c"
}

Only persisted authorized mappings from enabled devices are projected. A value is included only when the observation is currently available and fresh. Stale or unavailable observations never expose a value; stale observations have value: null and available: false. Unapproved, revoked, removed, and retired sources are excluded. The configuration authorization snapshot is read once per context projection, not once per observation. Revocation and lifecycle fencing remain fail-closed.

There is no new ESPHome-specific MCP tool or Resource. Agents use the existing permission-checked physical-context interfaces. MCP does not expose a raw device-control API, encryption key, host or Native API action operation.

The projection keeps four names/identities distinct: provider.id is the configured PHOS device ID; device_id, when supplied by ESPHome, is its protocol-reported device identity; device_name is the administrator's configured device display name; and display_name is the measurement mapping name. entity_id is the mapped Native API entity key. source_id identifies the provider, mapping and active generation/authorization epoch. The optional zone is descriptive device metadata only.

Provider and security boundaries

The optional provider uses aioesphomeapi through PHOS's existing provider lifecycle and requires a Noise encryption-key reference resolved by trusted runtime code through SecretsService. The secret is not stored in phos.yaml, rendered by Web Admin, returned in API payloads, or written to diagnostics. Missing dependencies, credentials, or connectivity do not authorize any mapping. Devices and mappings are explicit; discovery metadata is not an authorization identity or onboarding requirement.

Connections are observation-only. PHOS does not call ESPHome switch, light, climate, cover, button, firmware, or other command/service APIs. Native API connection tests retrieve sanitized device metadata and bounded entity inventory, then disconnect; they do not authorize a mapping or establish the long-lived provider's operational state.

Verification status and limits

Focused automated tests cover configuration validation, encrypted onboarding, mapping save and authorization, lifecycle reconciliation, freshness and fail-closed MCP projection. The recorded ESPHome, application-service, Physical Context and MCP verification command and result were:

.venv/bin/pytest -q tests/test_esphome_provider.py tests/test_esphome_web_device_management.py tests/test_esphome_web_entity_mapping.py tests/test_esphome_web_onboarding.py tests/test_application_service.py tests/test_external_physical_context.py tests/test_physical_context.py tests/test_mcp_server.py -k 'not test_esphome_discovery_admin_contract_exposes_candidates_and_queues_mdns_action and not test_streamable_http_listen_delivers_presence_and_attention_resource_cues'
# 159 passed, 2 deselected, 1 warning

The separately run tests/test_mcp_server.py::test_streamable_http_listen_delivers_presence_and_attention_resource_cues passed. The excluded discovery-admin test has an existing assertion mismatch: it expects the older mDNS status shape without current service counters. This is not live mDNS LAN verification. The project-wide test suite was not run after the latest ESPHome/zone work.

The maintainer has reported successful real ESP32 Native API → mapping → authorization → MCP workflow acceptance. The recorded report does not enumerate the tested entity types or confirm that device name and zone metadata were checked on hardware. No claim is made here for every entity type, connection recovery case, mDNS LAN behavior, or hardware zone propagation. The roadmap records these evidence limits.