Skip to content

Entity Provider Extension Methods

Leonard Sperry edited this page Apr 2, 2024 · 3 revisions

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 the State property and don't need anything from the Attributes.
  • Use the GetAttributeTypedEntity<T> when you're ok with the State being a string and you need data from the Attributes property.
  • Use the GetEntity<Tstate, TAttributes> method when you wan to specify both State and Attribuetes properties.

Enumerations

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.

Numbers and Dates

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)

Lights

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.

Geolocation

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)

Calendar

   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.