Skip to content

Entities

EntityT = typing.TypeVar('EntityT', bound='BaseEntity', covariant=True) module-attribute

Represents a specific entity type, e.g., LightEntity, SensorEntity, etc.

BaseEntity

Bases: BaseModel, Generic[StateT, StateValueT]

Base class for all entities.

Source code in src/hassette/models/entities/base.py
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
67
68
69
70
71
72
class BaseEntity(BaseModel, Generic[StateT, StateValueT]):
    """Base class for all entities."""

    model_config = ConfigDict(extra="allow", arbitrary_types_allowed=True)

    state: StateT
    _sync: "BaseEntitySyncFacade[StateT, StateValueT] | None" = PrivateAttr(default=None, init=False)

    async def refresh(self) -> StateT:
        self.state = cast("StateT", await self.hassette.api.get_state(self.entity_id))
        return self.state

    @property
    def value(self) -> StateValueT:
        return cast("StateValueT", self.state.value)

    @property
    def entity_id(self) -> str:
        return self.state.entity_id

    @property
    def domain(self) -> str:
        return self.state.domain

    @property
    def hassette(self) -> "Hassette":
        """Get the HassAPI instance for this state."""

        inst = context.HASSETTE_INSTANCE.get(None)
        if inst is None:
            raise RuntimeError("Hassette instance not set in context")

        return inst

    @property
    def api(self) -> "Api":
        """Get the Hassette API instance for this state."""
        return self.hassette.api

    @property
    def sync(self) -> "BaseEntitySyncFacade[StateT, StateValueT]":
        if self._sync is None:
            self._sync = BaseEntitySyncFacade(entity=self)
        return self._sync

    async def turn_off(self):
        """Turn off the entity."""
        return await self.api.turn_off(self.entity_id, self.domain)

    async def turn_on(self, **data):
        """Turn on the entity."""
        return await self.api.turn_on(self.entity_id, self.domain, **data)

    async def toggle(self):
        """Toggle the entity."""
        return await self.api.toggle_service(self.entity_id, self.domain)

hassette: Hassette property

Get the HassAPI instance for this state.

api: Api property

Get the Hassette API instance for this state.

turn_off() async

Turn off the entity.

Source code in src/hassette/models/entities/base.py
62
63
64
async def turn_off(self):
    """Turn off the entity."""
    return await self.api.turn_off(self.entity_id, self.domain)

turn_on(**data) async

Turn on the entity.

Source code in src/hassette/models/entities/base.py
66
67
68
async def turn_on(self, **data):
    """Turn on the entity."""
    return await self.api.turn_on(self.entity_id, self.domain, **data)

toggle() async

Toggle the entity.

Source code in src/hassette/models/entities/base.py
70
71
72
async def toggle(self):
    """Toggle the entity."""
    return await self.api.toggle_service(self.entity_id, self.domain)

BaseEntitySyncFacade

Bases: Generic[StateT, StateValueT]

Synchronous facade for BaseEntity to allow easier access to properties without async/await.

Source code in src/hassette/models/entities/base.py
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
class BaseEntitySyncFacade(Generic[StateT, StateValueT]):
    """Synchronous facade for BaseEntity to allow easier access to properties without async/await."""

    entity: BaseEntity[StateT, StateValueT]

    def __init__(self, entity: BaseEntity[StateT, StateValueT]) -> None:
        self.entity = entity

    def turn_off(self):
        """Turn off the entity."""
        return self.entity.api.sync.turn_off(self.entity.entity_id, self.entity.domain)

    def turn_on(self, **data):
        """Turn on the entity."""
        return self.entity.api.sync.turn_on(self.entity.entity_id, self.entity.domain, **data)

    def toggle(self):
        """Toggle the entity."""
        return self.entity.api.sync.toggle_service(self.entity.entity_id, self.entity.domain)

turn_off()

Turn off the entity.

Source code in src/hassette/models/entities/base.py
83
84
85
def turn_off(self):
    """Turn off the entity."""
    return self.entity.api.sync.turn_off(self.entity.entity_id, self.entity.domain)

turn_on(**data)

Turn on the entity.

Source code in src/hassette/models/entities/base.py
87
88
89
def turn_on(self, **data):
    """Turn on the entity."""
    return self.entity.api.sync.turn_on(self.entity.entity_id, self.entity.domain, **data)

toggle()

Toggle the entity.

Source code in src/hassette/models/entities/base.py
91
92
93
def toggle(self):
    """Toggle the entity."""
    return self.entity.api.sync.toggle_service(self.entity.entity_id, self.entity.domain)