4040)
4141from .coordinator import ShellyBlockCoordinator , ShellyConfigEntry , ShellyRpcCoordinator
4242from .entity import (
43+ BlockEntityDescription ,
4344 RpcEntityDescription ,
44- ShellyBlockEntity ,
45+ ShellyBlockAttributeEntity ,
4546 ShellyRpcAttributeEntity ,
47+ async_setup_entry_attribute_entities ,
4648 async_setup_entry_rpc ,
4749)
4850from .utils import (
4951 async_remove_orphaned_entities ,
50- async_remove_shelly_entity ,
5152 brightness_to_percentage ,
5253 get_device_entry_gen ,
5354 is_block_channel_type_light ,
5859PARALLEL_UPDATES = 0
5960
6061
62+ @dataclass (frozen = True , kw_only = True )
63+ class BlockLightDescription (BlockEntityDescription , LightEntityDescription ):
64+ """Description for a Shelly BLOCK light entity."""
65+
66+
67+ BLOCK_LIGHTS = {
68+ ("light" , "output" ): BlockLightDescription (
69+ key = "light|output" ,
70+ ),
71+ ("relay" , "output" ): BlockLightDescription (
72+ key = "relay|output" ,
73+ removal_condition = lambda settings , block : not is_block_channel_type_light (
74+ settings , block
75+ ),
76+ ),
77+ }
78+
79+
6180async def async_setup_entry (
6281 hass : HomeAssistant ,
6382 config_entry : ShellyConfigEntry ,
@@ -79,36 +98,29 @@ def async_setup_block_entry(
7998 """Set up entities for block device."""
8099 coordinator = config_entry .runtime_data .block
81100 assert coordinator
82- blocks = []
83- assert coordinator .device .blocks
84- for block in coordinator .device .blocks :
85- if block .type == "light" :
86- blocks .append (block )
87- elif block .type == "relay" and block .channel is not None :
88- if not is_block_channel_type_light (
89- coordinator .device .settings , int (block .channel )
90- ):
91- continue
92101
93- blocks .append (block )
94- unique_id = f"{ coordinator .mac } -{ block .type } _{ block .channel } "
95- async_remove_shelly_entity (hass , "switch" , unique_id )
96-
97- if not blocks :
98- return
99-
100- async_add_entities (BlockShellyLight (coordinator , block ) for block in blocks )
102+ async_setup_entry_attribute_entities (
103+ hass , config_entry , async_add_entities , BLOCK_LIGHTS , BlockShellyLight
104+ )
101105
102106
103- class BlockShellyLight (ShellyBlockEntity , LightEntity ):
107+ class BlockShellyLight (ShellyBlockAttributeEntity , LightEntity ):
104108 """Entity that controls a light on block based Shelly devices."""
105109
110+ entity_description : BlockLightDescription
106111 _attr_supported_color_modes : set [str ]
107112
108- def __init__ (self , coordinator : ShellyBlockCoordinator , block : Block ) -> None :
109- """Initialize light."""
110- super ().__init__ (coordinator , block )
113+ def __init__ (
114+ self ,
115+ coordinator : ShellyBlockCoordinator ,
116+ block : Block ,
117+ attribute : str ,
118+ description : BlockLightDescription ,
119+ ) -> None :
120+ """Initialize block light."""
121+ super ().__init__ (coordinator , block , attribute , description )
111122 self .control_result : dict [str , Any ] | None = None
123+ self ._attr_unique_id : str = f"{ coordinator .mac } -{ block .description } "
112124 self ._attr_supported_color_modes = set ()
113125 self ._attr_min_color_temp_kelvin = KELVIN_MIN_VALUE_WHITE
114126 self ._attr_max_color_temp_kelvin = KELVIN_MAX_VALUE
@@ -347,7 +359,7 @@ def _update_callback(self) -> None:
347359
348360@dataclass (frozen = True , kw_only = True )
349361class RpcLightDescription (RpcEntityDescription , LightEntityDescription ):
350- """Description for a Shelly RPC number entity."""
362+ """Description for a Shelly RPC light entity."""
351363
352364
353365class RpcShellyLightBase (ShellyRpcAttributeEntity , LightEntity ):
0 commit comments