Skip to content

Binary Sensor

binary_sensor

binary_sensor domain implementation (read-only).

SPEC module-attribute

SPEC: DomainSpec[BinarySensor] = register_domain(DomainSpec(name='binary_sensor', entity_cls=BinarySensor))

The DomainSpec registered with the shared DomainRegistry.

BinarySensor

Bases: Entity

A read-only Home Assistant binary sensor entity.

Binary sensors represent boolean detection states (e.g. motion, door open). They are read-only; no actions are exposed. Listener names use on_activate / on_deactivate.

Source code in src/haclient/domains/binary_sensor.py
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
class BinarySensor(Entity):
    """A read-only Home Assistant binary sensor entity.

    Binary sensors represent boolean detection states (e.g. motion,
    door open). They are read-only; no actions are exposed. Listener
    names use ``on_activate`` / ``on_deactivate``.
    """

    domain = "binary_sensor"

    # -- Listener decorators ------------------------------------------

    def on_activate(self, func: ValueChangeHandler) -> ValueChangeHandler:
        """Register a listener for when the sensor activates (state ``on``).

        Parameters
        ----------
        func : callable
            Sync or async callable invoked with ``(old_state, new_state)``
            on every transition into the ``on`` state.

        Returns
        -------
        callable
            The same *func*, returned so the method can be used as a
            decorator.
        """
        return self._register_state_transition_listener("on", func)

    def on_deactivate(self, func: ValueChangeHandler) -> ValueChangeHandler:
        """Register a listener for when the sensor deactivates (state ``off``).

        Parameters
        ----------
        func : callable
            Sync or async callable invoked with ``(old_state, new_state)``
            on every transition into the ``off`` state.

        Returns
        -------
        callable
            The same *func*, returned so the method can be used as a
            decorator.
        """
        return self._register_state_transition_listener("off", func)

    # -- State properties ---------------------------------------------

    @property
    def is_on(self) -> bool:
        """Whether the binary sensor is in the ``on`` state."""
        return self.state == "on"

    @property
    def device_class(self) -> str | None:
        """Device class (e.g. ``"motion"``, ``"door"``)."""
        value = self.attributes.get("device_class")
        return str(value) if value is not None else None

is_on property

is_on: bool

Whether the binary sensor is in the on state.

device_class property

device_class: str | None

Device class (e.g. "motion", "door").

on_activate

on_activate(func: ValueChangeHandler) -> ValueChangeHandler

Register a listener for when the sensor activates (state on).

Parameters:

Name Type Description Default
func callable

Sync or async callable invoked with (old_state, new_state) on every transition into the on state.

required

Returns:

Type Description
callable

The same func, returned so the method can be used as a decorator.

Source code in src/haclient/domains/binary_sensor.py
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
def on_activate(self, func: ValueChangeHandler) -> ValueChangeHandler:
    """Register a listener for when the sensor activates (state ``on``).

    Parameters
    ----------
    func : callable
        Sync or async callable invoked with ``(old_state, new_state)``
        on every transition into the ``on`` state.

    Returns
    -------
    callable
        The same *func*, returned so the method can be used as a
        decorator.
    """
    return self._register_state_transition_listener("on", func)

on_deactivate

on_deactivate(func: ValueChangeHandler) -> ValueChangeHandler

Register a listener for when the sensor deactivates (state off).

Parameters:

Name Type Description Default
func callable

Sync or async callable invoked with (old_state, new_state) on every transition into the off state.

required

Returns:

Type Description
callable

The same func, returned so the method can be used as a decorator.

Source code in src/haclient/domains/binary_sensor.py
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
def on_deactivate(self, func: ValueChangeHandler) -> ValueChangeHandler:
    """Register a listener for when the sensor deactivates (state ``off``).

    Parameters
    ----------
    func : callable
        Sync or async callable invoked with ``(old_state, new_state)``
        on every transition into the ``off`` state.

    Returns
    -------
    callable
        The same *func*, returned so the method can be used as a
        decorator.
    """
    return self._register_state_transition_listener("off", func)