1+ from homeassistant .components .number import NumberEntity
2+ from homeassistant .helpers .entity import EntityCategory
3+ from .const import DOMAIN
4+
5+ async def async_setup_entry (hass , config_entry , async_add_entities ):
6+ entities = [
7+ RecalboxPortNumber (hass , config_entry , "api_port_os" , "Port API OS" , "mdi:api" , 80 ),
8+ RecalboxPortNumber (hass , config_entry , "api_port_gamesmanager" , "Port API Games" , "mdi:api" , 81 ),
9+ RecalboxPortNumber (hass , config_entry , "udp_recalbox" , "Port UDP Recalbox" , "mdi:remote" , 1337 ),
10+ RecalboxPortNumber (hass , config_entry , "udp_retroarch" , "Port UDP RetroArch" , "mdi:remote" , 55355 ),
11+ RecalboxPortNumber (hass , config_entry , "api_port_kodi" , "Port API Kodi" , "mdi:kodi" , 8081 ),
12+ ]
13+ async_add_entities (entities )
14+
15+ class RecalboxPortNumber (NumberEntity ):
16+ _attr_entity_category = EntityCategory .CONFIG
17+ _attr_has_entity_name = True
18+ _attr_native_min_value = 1
19+ _attr_native_max_value = 65535
20+ _attr_native_step = 1
21+
22+ def __init__ (self , hass , config_entry , key , name , icon , defaultValue ):
23+ self .hass = hass
24+ self ._config_entry = config_entry
25+ self ._key = key
26+ self ._attr_name = name
27+ self ._attr_icon = icon
28+ self ._attr_unique_id = f"{ config_entry .entry_id } _port_{ key } "
29+ self ._default = defaultValue
30+
31+ @property
32+ def native_value (self ):
33+ return self ._config_entry .options .get (self ._key , self ._default )
34+
35+ async def async_set_native_value (self , value : float ):
36+ new_options = dict (self ._config_entry .options )
37+ new_options [self ._key ] = int (value )
38+ self .hass .config_entries .async_update_entry (self ._config_entry , options = new_options )
39+
40+ @property
41+ def device_info (self ):
42+ return {"identifiers" : {(DOMAIN , self ._config_entry .entry_id )}}
0 commit comments