Device bindings

Publish Kubernetes objects to Home Assistant, and write its commands back.

Beta. The CRDs are v1, but until 1.0 a change that cannot be additive is named in the changelog. What that means.

A binding selects objects of one kind by label. Each matching object becomes one Home Assistant device, and each entity of the device maps to a field of the object: a Deployment's spec.replicas, a CronJob's spec.suspend, a custom resource's status. Commands from Home Assistant are written back into the field. Nothing is added to the target object: no label, annotation, finalizer or owner reference.

apiVersion: kubeassistant.taumatix.dev/v1
kind: DeviceBinding
metadata: {name: media, namespace: home}
spec:
  target:
    apiVersion: apps/v1
    kind: Deployment
    selector: {matchLabels: {tier: media}}   # {} selects every object of the kind
  device:                                     # every string is a Go template over the object
    name: "{{ .metadata.name }}"
    manufacturer: Kubernetes
    model: "{{ .kind }}"
  entities:
    replicas:
      platform: number
      path: .spec.replicas
      options: {number: {min: 0, max: 3}}
    ready:
      platform: sensor
      path: .status.readyReplicas

The Deployment home/jellyfin labelled tier: media becomes the device ns_home_media_jellyfin, with a number entity that scales it and a sensor reporting its ready replicas.

  • path names one field: .-separated keys, with ["a.b"] for a key that contains a dot, such as .metadata.labels["app.kubernetes.io/name"]. List elements cannot be addressed.
  • channels replaces path for a domain with several controls, such as climate. It maps each control, spelled as Home Assistant spells it (mode, temperature, fan_mode), to a path. Only the controls listed are announced.
  • readOnly: true publishes a controllable domain without accepting commands. A path under .status or .metadata is always read-only, and the API server rejects a controllable entity with no path outside them. Home Assistant requires a command topic for a controllable domain's plain control, so one is still announced and Home Assistant still shows the control; what it sends is dropped, and the entity keeps showing the field's value.
  • options carries the full Home Assistant configuration of the entity's domain, generated from homeassistant-go for all 32 domains. name, deviceClass, unitOfMeasurement, stateClass, icon and entityCategory sit directly on the entity as shorthand.

A device template that names an absent key, such as {{ .metadata.labels.version }} on an object without that label, is an error reported on the binding rather than a device named <no value>.

Two scopes

A DeviceBinding is namespaced and selects objects in its own namespace only. A ClusterDeviceBinding is cluster-scoped: it selects across namespaces, narrowed by target.namespaceSelector (unset selects every namespace), and can target cluster-scoped kinds such as Node.

apiVersion: kubeassistant.taumatix.dev/v1
kind: ClusterDeviceBinding
metadata: {name: media}
spec:
  target:
    apiVersion: apps/v1
    kind: Deployment
    selector: {matchLabels: {tier: media}}
    namespaceSelector: {matchLabels: {home: "true"}}
  device: {name: "{{ .metadata.namespace }}/{{ .metadata.name }}"}
  entities:
    replicas: {platform: number, path: .spec.replicas}

Device ids are ns_<namespace>_<binding>_<object> for a DeviceBinding and cluster_<binding>_<namespace>_<object> (or cluster_<binding>_<object> for a cluster-scoped object) for a ClusterDeviceBinding. Kubernetes names never hold _, so ids do not collide, and two bindings on one object are two devices. A . in a name becomes __, since a discovery id cannot hold a dot.

status.devices lists what the binding published. A target that stops matching, or is deleted, is retracted on the next reconcile by diffing against it, including one deleted while the operator was down. Deleting the binding retracts all of its devices before its finalizer is released.

When nothing appears

The conditions say what is wrong: TargetWatched=False with reason Forbidden, KindNotFound or ClusterScopedKind, and Ready=False with InvalidEntities, InvalidPath or RenderFailed. InvalidPath is a path the target kind's published schema does not declare, such as a misspelling, or a writable entity on an object or a list. The reasons are constants in api/v1 (ReasonInvalidPath and the rest).

status.entities says what the operator made of each entity, so every broken path shows at once rather than the first:

status:
  entities:
  - name: replicas
    platform: number
    fields:
    - path: .spec.replicas
      type: integer        # from the target kind's published schema
      writable: true       # Home Assistant commands write it
  - name: replicaz
    platform: number
    fields:
    - path: .spec.replicaz
      writable: true
      reason: InvalidPath
      message: Deployment has no field .spec.replicaz

An entity its platform cannot serve carries reason: InvalidEntity itself; a path not checked yet, because the API server publishes no schema for a CRD installed a moment ago, carries SchemaUnpublished and is checked again 30 seconds later. Each change of Ready's status or reason is also recorded as an Event on the binding (kubectl events --for devicebinding/<name>), once per transition, beside the CommandRefused and CommandFailed events a command that is not written leaves.

State and commands

On any change to a target, the value at each path is published retained: a string as is, a number in decimal, a bool as the domain's payloadOn or payloadOff (default ON and OFF), and a map or list as JSON. An absent field publishes nothing, so Home Assistant shows the entity as unknown.

A command is converted to the type the target kind's schema declares for the field, read from the OpenAPI v3 schema the API server publishes: a number field holding 20 accepts 20.5, a text entity on an absent integer field writes an integer, and an int-or-string field such as maxSurge takes 3 as an integer and 25% as a string. Where the schema says nothing, as under x-kubernetes-preserve-unknown-fields, the value the field holds decides, and where it is absent too, the domain does: number is numeric, switch is a bool, anything else a string.

A payload that does not convert, such as 2.5 for an integer field, is refused with a Warning event CommandRefused on the binding and is not written. A write the API server rejects is a CommandFailed event.

How ownership works

A command is written as a JSON merge patch of that one field, with field manager home-assistant. The API server records home-assistant as the field's owner, taking it from whichever manager held it before, as a forced apply would:

kubectl get deployment jellyfin -n home -o yaml --show-managed-fields

It is not a Server-Side Apply: an apply declares every field its manager owns, so applying one field at a time would delete the fields earlier commands set.

This races with GitOps, deliberately. ArgoCD will re-apply its declared value and take the field back. That is the ordinary operator-versus-GitOps race, the same one HPA has with replicas. The alternative, a wall switch that does not work, is worse. Scope ignoreDifferences to the bound paths if you want Home Assistant to win, or bind them readOnly if you want Kubernetes to.