For AI agents: the complete documentation index is available at /llms.txt, the full documentation bundle is available at /llms-full.txt, and this page is available as Markdown at /developers/core/entity/number.md.

Number entity

number 是一种允许用户向集成输入任意值的 entity。从 homeassistant.components.number.NumberEntity 派生 entity platforms。

属性

Tip

Properties 应该只从内存返回信息,而不要执行 I/O(如网络请求)。请实现 update()async_update() 来获取数据。

NameTypeDefaultDescription
device_classstringNoneNumber 的类型。
modestringauto定义 number 在 UI 中的显示方式。建议使用默认值 auto。可以是 boxslider 以强制显示模式。
native_max_valuefloat100Number 的 native_unit_of_measurement 中接受的最大值(inclusive)
native_min_valuefloat0Number 的 native_unit_of_measurement 中接受的最小值(inclusive)
native_stepfloat见下文定义 values 的 resolution,即 number 的 native_unit_of_measurement 中最小的增量或减量。
native_valuefloat必需Number 的 native_unit_of_measurement 中的 value。
native_unit_of_measurementstringNoneNumber 的 value 所表示的计量单位。如果 native_unit_of_measurement 是 °C 或 °F,且其 device_class 是 temperature,则 number 的 unit_of_measurement 将为用户配置的 preferred temperature unit,number 的 state 将是可选单位转换后的 native_value。如果提供了 unit translation,则不应定义 native_unit_of_measurement

其他在所有 entity 中通用的 properties(如 iconname 等)也适用。

默认 step 值根据 range(max - min)值动态选择。如果 max_value 和 min_value 之间的差值大于 1.0,则默认 step 为 1.0。如果 range 较小,则 step 会迭代除以 10,直到小于 range。

可用的设备类型

如果指定 device class,你的 number entity 还需要返回正确的计量单位。

ConstantSupported unitsDescription
NumberDeviceClass.ABSOLUTE_HUMIDITYg/m³, mg/m³Absolute humidity
NumberDeviceClass.APPARENT_POWERmVA, VA, kVAApparent power
NumberDeviceClass.AQINoneAir Quality Index
NumberDeviceClass.AREAm², cm², km², mm², in², ft², yd², mi², ac, haArea
NumberDeviceClass.ATMOSPHERIC_PRESSUREcbar, bar, hPa, mmHG, inHg, inH₂O, kPa, mbar, Pa, psiAtmospheric pressure
NumberDeviceClass.BATTERY%剩余 battery 百分比
NumberDeviceClass.BLOOD_GLUCOSE_CONCENTRATIONmg/dL, mmol/LBlood glucose concentration```
NumberDeviceClass.CO2ppm二氧化碳浓度。
NumberDeviceClass.COppb, ppm, µg/m³, mg/m³一氧化碳浓度。
NumberDeviceClass.CONDUCTIVITYS/cm, mS/cm, µS/cmConductivity
NumberDeviceClass.CURRENTA, mA, µACurrent
NumberDeviceClass.DATA_RATEbit/s, kbit/s, Mbit/s, Gbit/s, B/s, kB/s, MB/s, GB/s, KiB/s, MiB/s, GiB/sData rate
NumberDeviceClass.DATA_SIZEbit, kbit, Mbit, Gbit, B, kB, MB, GB, TB, PB, EB, ZB, YB, KiB, MiB, GiB, TiB, PiB, EiB, ZiB, YiBData size
NumberDeviceClass.DISTANCEkm, m, cm, mm, mi, nmi, yd, inGeneric distance
NumberDeviceClass.DURATIONd, h, min, s, ms, µsTime period。不应仅因时间流逝而更新。Device 或 service 需要给出新的数据点才能更新。
NumberDeviceClass.ENERGYJ, kJ, MJ, GJ, mWh, Wh, kWh, MWh, GWh, TWh, cal, kcal, Mcal, GcalEnergy,此 device class 应用于表示 energy consumption,例如 electricity meter。表示 power over time。不要与 power 混淆。
NumberDeviceClass.ENERGY_DISTANCEkWh/100km, Wh/km, mi/kWh, km/kWhEnergy per distance,此 device class 应用于表示按距离的 energy consumption,例如 electric car 消耗的 electric energy 量。
NumberDeviceClass.ENERGY_STORAGEJ, kJ, MJ, GJ, mWh, Wh, kWh, MWh, GWh, TWh, cal, kcal, Mcal, GcalStored energy,此 device class 应用于表示 stored energy,例如 battery 中当前存储的 electric energy 量或 battery 的 capacity。表示 power over time。不要与 power 混淆。
NumberDeviceClass.FREQUENCYmHz, Hz, kHz, MHz, GHzFrequency
NumberDeviceClass.GASL, m³, ft³, CCF, MCFGas 的体积。以 kWh 的 energy 测量的 gas consumption 而不是体积,应归类为 energy。
NumberDeviceClass.HUMIDITY%Relative humidity
NumberDeviceClass.ILLUMINANCElxLight level
NumberDeviceClass.IRRADIANCEW/m², BTU/(h⋅ft²)Irradiance
NumberDeviceClass.MOISTURE%Moisture
NumberDeviceClass.MONETARYISO 4217带有 currency 的 Monetary value。
NumberDeviceClass.NITROGEN_DIOXIDEppb, ppm, µg/m³二氧化氮浓度
NumberDeviceClass.NITROGEN_MONOXIDEppb, µg/m³一氧化氮浓度
NumberDeviceClass.NITROUS_OXIDEµg/m³一氧化二氮浓度
NumberDeviceClass.OZONEppb, ppm, µg/m³臭氧浓度
NumberDeviceClass.PHNone水溶液的 Potential hydrogen(pH)
NumberDeviceClass.PM1µg/m³小于 1 微米的 particulate matter 浓度
NumberDeviceClass.PM25µg/m³小于 2.5 微米的 particulate matter 浓度
NumberDeviceClass.PM4µg/m³小于 4 微米的 particulate matter 浓度
NumberDeviceClass.PM10µg/m³小于 10 微米的 particulate matter 浓度
NumberDeviceClass.POWERmW, W, kW, MW, GW, TWPower。
NumberDeviceClass.POWER_FACTOR%, NonePower Factor
NumberDeviceClass.PRECIPITATIONcm, in, mmAccumulated precipitation
NumberDeviceClass.PRECIPITATION_INTENSITYin/d, in/h, mm/d, mm/hPrecipitation intensity
NumberDeviceClass.PRESSUREcbar, bar, hPa, mmHg, inHg, kPa, mbar, Pa, psi, mPaPressure。
NumberDeviceClass.RADONBq/m³, pCi/LRadon 浓度
NumberDeviceClass.REACTIVE_ENERGYvarh, kvarhReactive energy
NumberDeviceClass.REACTIVE_POWERmvar, var, kvarReactive power
NumberDeviceClass.SIGNAL_STRENGTHdB, dBmSignal strength
NumberDeviceClass.SOUND_PRESSUREdB, dBASound pressure
NumberDeviceClass.SPEEDft/s, in/d, in/h, in/s, km/h, kn, m/s, mph, mm/d, mm/sGeneric speed
NumberDeviceClass.SULPHUR_DIOXIDEppb, µg/m³二氧化硫浓度
NumberDeviceClass.TEMPERATURE°C, °F, KTemperature。
NumberDeviceClass.TEMPERATURE_DELTA°C, °F, K此 device class 表示 temperature interval(delta),即两个 temperature values 之间的差。
NumberDeviceClass.VOLATILE_ORGANIC_COMPOUNDSµg/m³, mg/m³Volatile organic compounds 浓度
NumberDeviceClass.VOLATILE_ORGANIC_COMPOUNDS_PARTSppm, ppbVolatile organic compounds 比率
NumberDeviceClass.VOLTAGEV, mV, µV, kV, MVVoltage
NumberDeviceClass.VOLUMEL, mL, gal, fl. oz., m³, ft³, CCF, MCFGeneric volume,此 device class 应用于表示 consumption,例如 vehicle 消耗的 fuel 量。
NumberDeviceClass.VOLUME_FLOW_RATEm³/h, m³/min, m³/s, ft³/min, L/h, L/min, L/s, gal/d, gal/h, gal/min, mL/sVolume flow rate,此 device class 应用于表示某些 volume 的 flow,例如瞬间消耗的水量。
NumberDeviceClass.VOLUME_STORAGEL, mL, gal, fl. oz., m³, ft³, CCF, MCFGeneric stored volume,此 device class 应用于表示 stored volume,例如 fuel tank 中的 fuel 量。
NumberDeviceClass.WATERL, gal, m³, ft³, CCF, MCFWater consumption
NumberDeviceClass.WEIGHTkg, g, mg, µg, oz, lb, stGeneric mass;weight 用于代替 mass 以符合日常语言。
NumberDeviceClass.WIND_DIRECTION°Wind direction
NumberDeviceClass.WIND_SPEEDft/s, km/h, kn, m/s, mphWind speed

恢复 number 状态

在 restart 或 reload 后恢复 state 的 numbers 不应扩展 RestoreEntity,因为它不存储 native_value,而是存储可能被 number base entity 修改的 state。恢复 state 的 numbers 应扩展 RestoreNumber,并从 async_added_to_hass 调用 await self.async_get_last_number_data,以访问存储的 native_min_valuenative_max_valuenative_stepnative_unit_of_measurementnative_value

方法

设置值

当用户或 automation 想要更新 value 时调用。

class MyNumber(NumberEntity):
    # 实现以下方法之一。

    def set_native_value(self, value: float) -> None:
        """Update the current value."""

    async def async_set_native_value(self, value: float) -> None:
        """Update the current value."""