Home Assistant Unofficial Reference 2024.12.1
homeassistant.core.State Class Reference
Inheritance diagram for homeassistant.core.State:
[legend]

Public Member Functions

None __init__ (self, str entity_id, str state, Mapping[str, Any]|None attributes=None, datetime.datetime|None last_changed=None, datetime.datetime|None last_reported=None, datetime.datetime|None last_updated=None, Context|None context=None, bool|None validate_entity_id=True, StateInfo|None state_info=None, float|None last_updated_timestamp=None)
 
str __repr__ (self)
 
CompressedState as_compressed_state (self)
 
bytes as_compressed_state_json (self)
 
ReadOnlyDict[str, datetime.datetime|Collection[Any]] as_dict (self)
 
bytes as_dict_json (self)
 
None expire (self)
 
Self|None from_dict (cls, dict[str, Any] json_dict)
 
json_fragment json_fragment (self)
 
float last_changed_timestamp (self)
 
float last_reported_timestamp (self)
 
str name (self)
 

Public Attributes

 attributes
 
 context
 
 entity_id
 
 last_changed
 
 last_reported
 
 last_updated
 
 last_updated_timestamp
 
 object_id
 
 state
 
 state_info
 

Private Member Functions

dict[str, Any] _as_dict (self)
 
ReadOnlyDict[str, datetime.datetime|Collection[Any]] _as_read_only_dict (self)
 

Static Private Attributes

 __slots__
 

Detailed Description

Object to represent a state within the state machine.

entity_id: the entity that is represented.
state: the state of the entity
attributes: extra information on entity and state
last_changed: last time the state was changed.
last_reported: last time the state was reported.
last_updated: last time the state or attributes were changed.
context: Context in which it was created
domain: Domain of this state.
object_id: Object id of this state.

Definition at line 1765 of file core.py.

Constructor & Destructor Documentation

◆ __init__()

None homeassistant.core.State.__init__ (   self,
str  entity_id,
str  state,
Mapping[str, Any] | None   attributes = None,
datetime.datetime | None   last_changed = None,
datetime.datetime | None   last_reported = None,
datetime.datetime | None   last_updated = None,
Context | None   context = None,
bool | None   validate_entity_id = True,
StateInfo | None   state_info = None,
float | None   last_updated_timestamp = None 
)
Initialize a new state.

Definition at line 1794 of file core.py.

Member Function Documentation

◆ __repr__()

str homeassistant.core.State.__repr__ (   self)
Return the representation of the states.

Reimplemented in homeassistant.helpers.template.TemplateStateFromEntityId, and homeassistant.helpers.template.TemplateState.

Definition at line 2024 of file core.py.

◆ _as_dict()

dict[str, Any] homeassistant.core.State._as_dict (   self)
private
Return a dict representation of the State.

Callers should be careful to not mutate the returned dictionary
as it will mutate the cached version.

Definition at line 1867 of file core.py.

◆ _as_read_only_dict()

ReadOnlyDict[str, datetime.datetime | Collection[Any]] homeassistant.core.State._as_read_only_dict (   self)
private
Return a ReadOnlyDict representation of the State.

Definition at line 1908 of file core.py.

◆ as_compressed_state()

CompressedState homeassistant.core.State.as_compressed_state (   self)
Build a compressed dict of a state for adds.

Omits the lu (last_updated) if it matches (lc) last_changed.

Sends c (context) as a string if it only contains an id.

Definition at line 1933 of file core.py.

◆ as_compressed_state_json()

bytes homeassistant.core.State.as_compressed_state_json (   self)
Build a compressed JSON key value pair of a state for adds.

The JSON string is a key value pair of the entity_id and the compressed state.

It is used for sending multiple states in a single message.

Definition at line 1961 of file core.py.

◆ as_dict()

ReadOnlyDict[str, datetime.datetime | Collection[Any]] homeassistant.core.State.as_dict (   self)
Return a ReadOnlyDict representation of the State.

Async friendly.

Can be used for JSON serialization.
Ensures: state == State.from_dict(state.as_dict())

Reimplemented in homeassistant.components.recorder.models.state.LazyState, and homeassistant.components.recorder.models.legacy.LegacyLazyState.

Definition at line 1895 of file core.py.

◆ as_dict_json()

bytes homeassistant.core.State.as_dict_json (   self)
Return a JSON string of the State.

Definition at line 1923 of file core.py.

◆ expire()

None homeassistant.core.State.expire (   self)
Mark the state as old.

We give up the original reference to the context to ensure
the context can be garbage collected by replacing it with
a new one with the same id to ensure the old state
can still be examined for comparison against the new state.

Since we are always going to fire a EVENT_STATE_CHANGED event
after we remove a state from the state machine we need to make
sure we don't end up holding a reference to the original context
since it can never be garbage collected as each event would
reference the previous one.

Definition at line 2006 of file core.py.

◆ from_dict()

Self | None homeassistant.core.State.from_dict (   cls,
dict[str, Any]  json_dict 
)
Initialize a state from a dict.

Async friendly.

Ensures: state == State.from_json_dict(state.to_json_dict())

Definition at line 1971 of file core.py.

◆ json_fragment()

json_fragment homeassistant.core.State.json_fragment (   self)
Return a JSON fragment of the State.

Definition at line 1928 of file core.py.

◆ last_changed_timestamp()

float homeassistant.core.State.last_changed_timestamp (   self)
Timestamp of last change.

Definition at line 1857 of file core.py.

◆ last_reported_timestamp()

float homeassistant.core.State.last_reported_timestamp (   self)
Timestamp of last report.

Definition at line 1862 of file core.py.

◆ name()

str homeassistant.core.State.name (   self)
Name of this state.

Reimplemented in homeassistant.helpers.template.TemplateStateBase.

Definition at line 1850 of file core.py.

Member Data Documentation

◆ __slots__

homeassistant.core.State.__slots__
staticprivate

Definition at line 1779 of file core.py.

◆ attributes

homeassistant.core.State.attributes

Definition at line 1825 of file core.py.

◆ context

homeassistant.core.State.context

Definition at line 1831 of file core.py.

◆ entity_id

homeassistant.core.State.entity_id

Definition at line 1819 of file core.py.

◆ last_changed

homeassistant.core.State.last_changed

Definition at line 1830 of file core.py.

◆ last_reported

homeassistant.core.State.last_reported

Definition at line 1828 of file core.py.

◆ last_updated

homeassistant.core.State.last_updated

Definition at line 1829 of file core.py.

◆ last_updated_timestamp

homeassistant.core.State.last_updated_timestamp

Definition at line 1840 of file core.py.

◆ object_id

homeassistant.core.State.object_id

Definition at line 1833 of file core.py.

◆ state

homeassistant.core.State.state

Definition at line 1820 of file core.py.

◆ state_info

homeassistant.core.State.state_info

Definition at line 1832 of file core.py.


The documentation for this class was generated from the following file: