-
Notifications
You must be signed in to change notification settings - Fork 1
Entity Provider Extension Methods
The IEntityStateProvider
interface has methods for retrieving entity states from either the cache or Home Assistant. The methods described here give access to strongly typed versions of entity states. Many of these methods are counterparts to extension methods of IHaEntityStateChange
. These methods will deserialize the State
and Attributes
properties into types of your choosing.
First, are three methods where you can specify the types.
public static Task<HaEntityState<T, JsonElement>?> GetStateTypedEntity<T>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<string, T>?> GetAttributeTypedEntity<T>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<Tstate, TAttributes>?> GetEntity<Tstate, TAttributes>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
- Use the
GetStateTypedEntity<T>
method when you only care about theState
property and don't need anything from theAttributes
. - Use the
GetAttributeTypedEntity<T>
when you're ok with theState
being a string and you need data from theAttributes
property. - Use the
GetEntity<Tstate, TAttributes>
method when you wan to specify bothState
andAttribuetes
properties.
These methods will get you access to commonly used enumerations in Home Assistant.
public static Task<HaEntityState<OnOff, JsonElement>?> GetOnOffEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<OnOff, Tatt>?> GetOnOffEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<BatteryState, JsonElement>?> GetBatteryStateEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<BatteryState, Tatt>?> GetBatteryStateEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<SunModel?> GetSun(this IEntityStateProvider provider, CancellationToken cancellationToken = default)
There are two methods each for OnOff
and BatteryState
enumerations, one generic and one non-generic. Use the generic version when you also want to specify the Attributes
type. The GetSun
method provides strongly typed access to the "sun.sun" entity in Home Assistant.
These methods work just like the enumeration methods with both a generic and non-generic version.
public static Task<HaEntityState<int?, JsonElement>?> GetIntegerEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<int?, Tatt>?> GetIntegerEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<float?, JsonElement>?> GetFloatEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<float?, Tatt>?> GetFloatEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<double?, JsonElement>?> GetDoubleEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<double?, Tatt>?> GetDoubleEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<DateTime?, JsonElement>?> GetDateTimeEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<DateTime?, Tatt>?> GetDateTimeEntity<Tatt>(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
These methods will also convert the Attributes
property to a built-in type. If you have a non-standard light in your setup, you can create your own class to specify the Attributes
property and call the GetOnOffEntity<Tatt>
method described above.
public static Task<HaEntityState<OnOff, LightModel>?> GetLightEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<OnOff, ColorLightModel>?> GetColorLightEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
There is one method for regular lights that support brightness and one for color lights. Several of the properties in the ColorLightModel
are of a built-in type for RGB tuples or other. See Utility classes for more information.
There are three methods for working with geolocation entities
public static Task<HaEntityState<string, PersonModel>?> GetPersonEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<string, DeviceTrackerModel>?> GetDeviceTrackerEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<int?, ZoneModel>?> GetZoneEntity(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken = default)
public static Task<HaEntityState<OnOff, CalendarModel>?> GetCalendar(this IEntityStateProvider provider, string entityId, CancellationToken cancellationToken)
Note: this method will only retrieve the next event on the calendar.