99import voluptuous as vol
1010
1111from homeassistant .components import websocket_api
12+ from homeassistant .components .sensor import CONF_STATE_CLASS , SensorStateClass
1213from homeassistant .const import CONF_ENTITY_ID , CONF_NAME , CONF_STATE , CONF_TYPE
1314from homeassistant .core import HomeAssistant , callback
1415from homeassistant .exceptions import HomeAssistantError
3940 CONF_PERIOD_KEYS ,
4041 CONF_START ,
4142 CONF_TYPE_KEYS ,
43+ CONF_TYPE_RATIO ,
4244 CONF_TYPE_TIME ,
4345 DEFAULT_NAME ,
4446 DOMAIN ,
@@ -101,10 +103,19 @@ async def get_state_schema(handler: SchemaCommonFlowHandler) -> vol.Schema:
101103async def get_options_schema (handler : SchemaCommonFlowHandler ) -> vol .Schema :
102104 """Return schema for options step."""
103105 entity_id = handler .options [CONF_ENTITY_ID ]
104- return _get_options_schema_with_entity_id (entity_id )
105-
106-
107- def _get_options_schema_with_entity_id (entity_id : str ) -> vol .Schema :
106+ conf_type = handler .options [CONF_TYPE ]
107+ return _get_options_schema_with_entity_id (entity_id , conf_type )
108+
109+
110+ def _get_options_schema_with_entity_id (entity_id : str , type : str ) -> vol .Schema :
111+ state_class_options = (
112+ [SensorStateClass .MEASUREMENT ]
113+ if type == CONF_TYPE_RATIO
114+ else [
115+ SensorStateClass .MEASUREMENT ,
116+ SensorStateClass .TOTAL_INCREASING ,
117+ ]
118+ )
108119 return vol .Schema (
109120 {
110121 vol .Optional (CONF_ENTITY_ID ): EntitySelector (
@@ -130,6 +141,13 @@ def _get_options_schema_with_entity_id(entity_id: str) -> vol.Schema:
130141 vol .Optional (CONF_DURATION ): DurationSelector (
131142 DurationSelectorConfig (enable_day = True , allow_negative = False )
132143 ),
144+ vol .Optional (CONF_STATE_CLASS ): SelectSelector (
145+ SelectSelectorConfig (
146+ options = state_class_options ,
147+ translation_key = CONF_STATE_CLASS ,
148+ mode = SelectSelectorMode .DROPDOWN ,
149+ ),
150+ ),
133151 }
134152 )
135153
@@ -158,7 +176,7 @@ def _get_options_schema_with_entity_id(entity_id: str) -> vol.Schema:
158176class HistoryStatsConfigFlowHandler (SchemaConfigFlowHandler , domain = DOMAIN ):
159177 """Handle a config flow for History stats."""
160178
161- MINOR_VERSION = 2
179+ MINOR_VERSION = 3
162180
163181 config_flow = CONFIG_FLOW
164182 options_flow = OPTIONS_FLOW
@@ -201,13 +219,15 @@ async def ws_start_preview(
201219 config_entry = hass .config_entries .async_get_entry (flow_status ["handler" ])
202220 entity_id = options [CONF_ENTITY_ID ]
203221 name = options [CONF_NAME ]
222+ conf_type = options [CONF_TYPE ]
204223 else :
205224 flow_status = hass .config_entries .options .async_get (msg ["flow_id" ])
206225 config_entry = hass .config_entries .async_get_entry (flow_status ["handler" ])
207226 if not config_entry :
208227 raise HomeAssistantError ("Config entry not found" )
209228 entity_id = config_entry .options [CONF_ENTITY_ID ]
210229 name = config_entry .options [CONF_NAME ]
230+ conf_type = config_entry .options [CONF_TYPE ]
211231
212232 @callback
213233 def async_preview_updated (
@@ -233,7 +253,7 @@ def async_preview_updated(
233253
234254 validated_data : Any = None
235255 try :
236- validated_data = (_get_options_schema_with_entity_id (entity_id ))(
256+ validated_data = (_get_options_schema_with_entity_id (entity_id , conf_type ))(
237257 msg ["user_input" ]
238258 )
239259 except vol .Invalid as ex :
@@ -255,6 +275,7 @@ def async_preview_updated(
255275 start = validated_data .get (CONF_START )
256276 end = validated_data .get (CONF_END )
257277 duration = validated_data .get (CONF_DURATION )
278+ state_class = validated_data .get (CONF_STATE_CLASS )
258279
259280 history_stats = HistoryStats (
260281 hass ,
@@ -274,6 +295,7 @@ def async_preview_updated(
274295 name = name ,
275296 unique_id = None ,
276297 source_entity_id = entity_id ,
298+ state_class = state_class ,
277299 )
278300 preview_entity .hass = hass
279301
0 commit comments