@@ -27,7 +27,7 @@ def __init__(self, path: list[str] | None = None) -> None:
2727 if not hasattr (self , "attributes" ):
2828 self .attributes = {}
2929 self ._path : list [str ] = path or []
30- self .__sub_controller_tree : dict [str , BaseController ] = {}
30+ self .__sub_controller_tree : dict [str , SubController ] = {}
3131
3232 self ._bind_attrs ()
3333
@@ -60,7 +60,8 @@ def _bind_attrs(self) -> None:
6060 new_attribute = copy (attr )
6161 setattr (self , attr_name , new_attribute )
6262
63- self .attributes [attr_name ] = new_attribute
63+ if attr_name != "root_attribute" :
64+ self .attributes [attr_name ] = new_attribute
6465
6566 def register_sub_controller (self , name : str , sub_controller : SubController ):
6667 if name in self .__sub_controller_tree .keys ():
@@ -71,7 +72,16 @@ def register_sub_controller(self, name: str, sub_controller: SubController):
7172 self .__sub_controller_tree [name ] = sub_controller
7273 sub_controller .set_path (self .path + [name ])
7374
74- def get_sub_controllers (self ) -> dict [str , BaseController ]:
75+ if isinstance (sub_controller .root_attribute , Attribute ):
76+ if name in self .attributes :
77+ raise TypeError (
78+ f"Cannot set SubController `{ name } ` root attribute "
79+ f"on the parent controller `{ type (self ).__name__ } ` "
80+ f"as it already has an attribute of that name."
81+ )
82+ self .attributes [name ] = sub_controller .root_attribute
83+
84+ def get_sub_controllers (self ) -> dict [str , SubController ]:
7585 return self .__sub_controller_tree
7686
7787 def get_controller_mappings (self ) -> list [SingleMapping ]:
@@ -103,6 +113,7 @@ def _get_single_mapping(controller: BaseController) -> SingleMapping:
103113 for name , attribute in controller .attributes .items ()
104114 if attribute .enabled
105115 }
116+
106117 return SingleMapping (
107118 controller , scan_methods , put_methods , command_methods , enabled_attributes
108119 )
@@ -134,5 +145,7 @@ class SubController(BaseController):
134145 it as part of a larger device.
135146 """
136147
148+ root_attribute : Attribute | None = None
149+
137150 def __init__ (self ) -> None :
138151 super ().__init__ ()
0 commit comments