77from homeassistant .helpers .entity import DeviceInfo
88from homeassistant .helpers .entity_platform import AddEntitiesCallback
99
10- from custom_components .dmx .const import DOMAIN
10+ from custom_components .dmx .const import DOMAIN , CONF_FIXTURE_ENTITIES
1111from custom_components .dmx .io .dmx_io import DmxUniverse
1212
13- _LOGGER = logging .getLogger (__name__ )
13+ log = logging .getLogger (__name__ )
1414
1515
1616class DmxUniverseSwitch (SwitchEntity ):
1717 """Switch entity to control DMX universe output."""
1818
19- def __init__ (self , universe : DmxUniverse , universe_name : str ):
19+ def __init__ (self , universe : DmxUniverse , universe_name : str , hass : HomeAssistant , config_entry : ConfigEntry ):
2020 """Initialize the switch."""
2121 self ._universe = universe
2222 self ._universe_name = universe_name
2323 self ._is_on = True
24+ self ._hass = hass
25+ self ._config_entry = config_entry
2426 self ._attr_unique_id = f"{ DOMAIN } _dmx_universe_{ universe .port_address .port_address } "
2527 self ._attr_name = f"DMX Universe { universe_name } "
2628
2729 # Set device info
2830 self ._attr_device_info = DeviceInfo (
2931 identifiers = {(DOMAIN , f"universe_{ universe .port_address .port_address } " )},
3032 name = f"DMX Universe { universe_name } " ,
31- manufacturer = "ArtNet DMX" ,
33+ manufacturer = "DMX" ,
3234 model = "DMX Universe Controller" ,
3335 )
3436
@@ -42,15 +44,41 @@ async def async_turn_on(self, **kwargs: Any) -> None:
4244 self ._is_on = True
4345 self ._universe .set_output_enabled (True )
4446 self ._universe .send_universe_data ()
47+ await self ._set_universe_entities_availability (True )
4548 self .async_write_ha_state ()
46- _LOGGER .debug ("Enabled output for universe %s" , self ._universe_name )
49+ log .debug ("Enabled output for universe %s" , self ._universe_name )
4750
4851 async def async_turn_off (self , ** kwargs : Any ) -> None :
4952 """Turn the switch off."""
5053 self ._is_on = False
5154 self ._universe .set_output_enabled (False )
55+ await self ._set_universe_entities_availability (False )
5256 self .async_write_ha_state ()
53- _LOGGER .debug ("Disabled output for universe %s" , self ._universe_name )
57+ log .debug ("Disabled output for universe %s" , self ._universe_name )
58+
59+ async def _set_universe_entities_availability (self , available : bool ) -> None :
60+ """Set availability of all entities in this universe."""
61+ domain_data = self ._hass .data [DOMAIN ][self ._config_entry .entry_id ]
62+ entities = domain_data .get (CONF_FIXTURE_ENTITIES , [])
63+
64+ for entity in entities :
65+ # Check if entity belongs to this universe
66+ entity_universe = None
67+ if hasattr (entity , '_universe' ):
68+ entity_universe = entity ._universe
69+ elif hasattr (entity , 'universe' ):
70+ entity_universe = entity .universe
71+
72+ if entity_universe == self ._universe :
73+ if hasattr (entity , 'available' ):
74+ entity .available = available
75+ if entity .hass : # Only update state if entity is added to hass
76+ entity .async_write_ha_state ()
77+
78+ log .debug (
79+ "Set availability to %s for entities in universe %s" ,
80+ available , self ._universe_name
81+ )
5482
5583 @property
5684 def icon (self ) -> str :
@@ -71,7 +99,7 @@ async def async_setup_entry(
7199 switches = []
72100 for port_address , universe in universes .items ():
73101 universe_name = f"{ port_address .net } /{ port_address .sub_net } /{ port_address .universe } "
74- switch = DmxUniverseSwitch (universe , universe_name )
102+ switch = DmxUniverseSwitch (universe , universe_name , hass , config_entry )
75103 switches .append (switch )
76104
77105 async_add_entities (switches )
0 commit comments