Skip to content

Commit f761630

Browse files
committed
Merge branch 'feature/migrate-sensor-entities-to-configs' into dev
2 parents 92753b2 + 78b035a commit f761630

4 files changed

Lines changed: 79 additions & 9 deletions

File tree

custom_components/recalbox/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import hashlib
2020

2121
_LOGGER = logging.getLogger(__name__)
22-
PLATFORMS = ["switch", "sensor"]
22+
PLATFORMS = ["switch", "sensor", "number"]
2323

2424

2525
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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)}}

custom_components/recalbox/sensor.py

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,13 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
99
# On crée une liste d'entités à ajouter
1010
entities = [
1111
RecalboxDiagnosticSensor(config_entry, "host", "Host", "mdi:ip-network"),
12-
RecalboxDiagnosticSensor(config_entry, "api_port_os", "Port API OS", "mdi:api", 80),
13-
RecalboxDiagnosticSensor(config_entry, "api_port_gamesmanager", "Port API Games", "mdi:api", 81),
14-
RecalboxDiagnosticSensor(config_entry, "udp_recalbox", "Port UDP Recalbox", "mdi:remote", 1337),
15-
RecalboxDiagnosticSensor(config_entry, "udp_retroarch", "Port UDP RetroArch", "mdi:remote", 55355),
16-
RecalboxDiagnosticSensor(config_entry, "api_port_kodi", "Port API Kodi", "mdi:kodi", 8081),
17-
RecalboxDiagnosticSensor(config_entry, "only_ip_v4", "Force mDNS IP v4 only", "mdi:dns", True),
1812
]
1913
async_add_entities(entities)
2014

2115

2216
class RecalboxDiagnosticSensor(SensorEntity):
2317
"""Classe générique pour les diagnostics de la Recalbox."""
24-
_attr_entity_category = EntityCategory.CONFIG
18+
_attr_entity_category = EntityCategory.DIAGNOSTIC
2519
_attr_has_entity_name = True
2620

2721
def __init__(self, config_entry, key, name, icon, default=None):

custom_components/recalbox/switch.py

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from homeassistant.helpers.update_coordinator import DataUpdateCoordinator, UpdateFailed, CoordinatorEntity
44
from homeassistant.helpers import device_registry as dr
55
from homeassistant.exceptions import HomeAssistantError
6+
from homeassistant.helpers.entity import EntityCategory
67
from .const import DOMAIN
78
from .translations_service import RecalboxTranslator
89
from .api import RecalboxAPI
@@ -30,7 +31,7 @@ async def async_setup_entry(hass, config_entry, async_add_entities):
3031
# On crée l'entité en lui passant l'objet config_entry (qui contient l'IP)
3132
new_recalbox_entity = RecalboxEntity(hass, config_entry, api, coordinator)
3233
hass.data[DOMAIN]["instances"][config_entry.entry_id]["sensor_entity"] = new_recalbox_entity # pour la retrouver ailleurs plus facilement
33-
async_add_entities([new_recalbox_entity])
34+
async_add_entities([new_recalbox_entity, RecalboxForceV4Switch(hass, config_entry)])
3435

3536

3637

@@ -397,3 +398,36 @@ async def async_added_to_hass(self):
397398
_LOGGER.debug("Premier ping échoué au démarrage : on laisse la recalbox sur OFF")
398399

399400

401+
402+
403+
# --------- SWITCH ON CONFIG -----------
404+
405+
class RecalboxForceV4Switch(SwitchEntity):
406+
_attr_entity_category = EntityCategory.CONFIG
407+
_attr_has_entity_name = True
408+
_attr_name = "Force mDNS IP v4 only"
409+
_attr_icon = "mdi:dns"
410+
411+
def __init__(self, hass, config_entry):
412+
self.hass = hass
413+
self._config_entry = config_entry
414+
self._attr_unique_id = f"{config_entry.entry_id}_only_ip_v4"
415+
416+
@property
417+
def is_on(self):
418+
return self._config_entry.options.get("only_ip_v4", True)
419+
420+
async def async_turn_on(self, **kwargs):
421+
await self._update_config(True)
422+
423+
async def async_turn_off(self, **kwargs):
424+
await self._update_config(False)
425+
426+
async def _update_config(self, value):
427+
new_options = dict(self._config_entry.options)
428+
new_options["only_ip_v4"] = value
429+
self.hass.config_entries.async_update_entry(self._config_entry, options=new_options)
430+
431+
@property
432+
def device_info(self):
433+
return {"identifiers": {(DOMAIN, self._config_entry.entry_id)}}

0 commit comments

Comments
 (0)