2
2
from __future__ import annotations
3
3
4
4
import logging
5
- from typing import Self
5
+ from typing import Any , Self
6
6
7
7
import voluptuous as vol
8
8
61
61
}
62
62
63
63
64
- def _cv_input_text (cfg ) :
64
+ def _cv_input_text (config : dict [ str , Any ]) -> dict [ str , Any ] :
65
65
"""Configure validation helper for input box (voluptuous)."""
66
- minimum = cfg . get ( CONF_MIN )
67
- maximum = cfg . get ( CONF_MAX )
66
+ minimum : int = config [ CONF_MIN ]
67
+ maximum : int = config [ CONF_MAX ]
68
68
if minimum > maximum :
69
69
raise vol .Invalid (
70
70
f"Max len ({ minimum } ) is not greater than min len ({ maximum } )"
71
71
)
72
- state = cfg .get (CONF_INITIAL )
72
+ state : str | None = config .get (CONF_INITIAL )
73
73
if state is not None and (len (state ) < minimum or len (state ) > maximum ):
74
74
raise vol .Invalid (
75
75
f"Initial value { state } length not in range { minimum } -{ maximum } "
76
76
)
77
- return cfg
77
+ return config
78
78
79
79
80
80
CONFIG_SCHEMA = vol .Schema (
@@ -86,7 +86,7 @@ def _cv_input_text(cfg):
86
86
vol .Optional (CONF_NAME ): cv .string ,
87
87
vol .Optional (CONF_MIN , default = CONF_MIN_VALUE ): vol .Coerce (int ),
88
88
vol .Optional (CONF_MAX , default = CONF_MAX_VALUE ): vol .Coerce (int ),
89
- vol .Optional (CONF_INITIAL , "" ): cv .string ,
89
+ vol .Optional (CONF_INITIAL ): cv .string ,
90
90
vol .Optional (CONF_ICON ): cv .icon ,
91
91
vol .Optional (CONF_UNIT_OF_MEASUREMENT ): cv .string ,
92
92
vol .Optional (CONF_PATTERN ): cv .string ,
@@ -162,16 +162,18 @@ class InputTextStorageCollection(collection.DictStorageCollection):
162
162
163
163
CREATE_UPDATE_SCHEMA = vol .Schema (vol .All (STORAGE_FIELDS , _cv_input_text ))
164
164
165
- async def _process_create_data (self , data : dict ) -> dict :
165
+ async def _process_create_data (self , data : dict [ str , Any ] ) -> vol . Schema :
166
166
"""Validate the config is valid."""
167
167
return self .CREATE_UPDATE_SCHEMA (data )
168
168
169
169
@callback
170
- def _get_suggested_id (self , info : dict ) -> str :
170
+ def _get_suggested_id (self , info : dict [ str , Any ] ) -> str :
171
171
"""Suggest an ID based on the config."""
172
- return info [CONF_NAME ]
172
+ return info [CONF_NAME ] # type: ignore[no-any-return]
173
173
174
- async def _update_data (self , item : dict , update_data : dict ) -> dict :
174
+ async def _update_data (
175
+ self , item : dict [str , Any ], update_data : dict [str , Any ]
176
+ ) -> dict [str , Any ]:
175
177
"""Return a new updated data object."""
176
178
update_data = self .CREATE_UPDATE_SCHEMA (update_data )
177
179
return {CONF_ID : item [CONF_ID ]} | update_data
@@ -185,6 +187,7 @@ class InputText(collection.CollectionEntity, RestoreEntity):
185
187
)
186
188
187
189
_attr_should_poll = False
190
+ _current_value : str | None
188
191
editable : bool
189
192
190
193
def __init__ (self , config : ConfigType ) -> None :
@@ -195,55 +198,55 @@ def __init__(self, config: ConfigType) -> None:
195
198
@classmethod
196
199
def from_storage (cls , config : ConfigType ) -> Self :
197
200
"""Return entity instance initialized from storage."""
198
- input_text = cls (config )
201
+ input_text : Self = cls (config )
199
202
input_text .editable = True
200
203
return input_text
201
204
202
205
@classmethod
203
206
def from_yaml (cls , config : ConfigType ) -> Self :
204
207
"""Return entity instance initialized from yaml."""
205
- input_text = cls (config )
208
+ input_text : Self = cls (config )
206
209
input_text .entity_id = f"{ DOMAIN } .{ config [CONF_ID ]} "
207
210
input_text .editable = False
208
211
return input_text
209
212
210
213
@property
211
- def name (self ):
214
+ def name (self ) -> str | None :
212
215
"""Return the name of the text input entity."""
213
216
return self ._config .get (CONF_NAME )
214
217
215
218
@property
216
- def icon (self ):
219
+ def icon (self ) -> str | None :
217
220
"""Return the icon to be used for this entity."""
218
221
return self ._config .get (CONF_ICON )
219
222
220
223
@property
221
224
def _maximum (self ) -> int :
222
225
"""Return max len of the text."""
223
- return self ._config [CONF_MAX ]
226
+ return self ._config [CONF_MAX ] # type: ignore[no-any-return]
224
227
225
228
@property
226
229
def _minimum (self ) -> int :
227
230
"""Return min len of the text."""
228
- return self ._config [CONF_MIN ]
231
+ return self ._config [CONF_MIN ] # type: ignore[no-any-return]
229
232
230
233
@property
231
- def state (self ):
234
+ def state (self ) -> str | None :
232
235
"""Return the state of the component."""
233
236
return self ._current_value
234
237
235
238
@property
236
- def unit_of_measurement (self ):
239
+ def unit_of_measurement (self ) -> str | None :
237
240
"""Return the unit the value is expressed in."""
238
241
return self ._config .get (CONF_UNIT_OF_MEASUREMENT )
239
242
240
243
@property
241
- def unique_id (self ) -> str | None :
244
+ def unique_id (self ) -> str :
242
245
"""Return unique id for the entity."""
243
- return self ._config [CONF_ID ]
246
+ return self ._config [CONF_ID ] # type: ignore[no-any-return]
244
247
245
248
@property
246
- def extra_state_attributes (self ):
249
+ def extra_state_attributes (self ) -> dict [ str , Any ] :
247
250
"""Return the state attributes."""
248
251
return {
249
252
ATTR_EDITABLE : self .editable ,
@@ -253,20 +256,20 @@ def extra_state_attributes(self):
253
256
ATTR_MODE : self ._config [CONF_MODE ],
254
257
}
255
258
256
- async def async_added_to_hass (self ):
259
+ async def async_added_to_hass (self ) -> None :
257
260
"""Run when entity about to be added to hass."""
258
261
await super ().async_added_to_hass ()
259
262
if self ._current_value is not None :
260
263
return
261
264
262
265
state = await self .async_get_last_state ()
263
- value = state and state .state
266
+ value : str | None = state and state .state # type: ignore[assignment]
264
267
265
268
# Check against None because value can be 0
266
269
if value is not None and self ._minimum <= len (value ) <= self ._maximum :
267
270
self ._current_value = value
268
271
269
- async def async_set_value (self , value ) :
272
+ async def async_set_value (self , value : str ) -> None :
270
273
"""Select new value."""
271
274
if len (value ) < self ._minimum or len (value ) > self ._maximum :
272
275
_LOGGER .warning (
0 commit comments