1313from ohme import OhmeAPI
1414from octopus import OctopusAPI
1515from web import WebInterface
16+ from ha import HAInterface
17+ from db_manager import DatabaseManager
18+ import os
1619
1720COMPONENT_LIST = {
18- "web" : {"class" : WebInterface , "name" : "Web Interface" , "args" : {"port" : {"required" : False , "config" : "web_port" , "default" : 5052 }}},
21+ "db" : {
22+ "class" : DatabaseManager ,
23+ "name" : "Database Manager" ,
24+ "args" : {
25+ "db_enable" : {"required" : True , "config" : "db_enable" },
26+ "db_days" : {"required" : False , "config" : "db_days" , "default" : 30 },
27+ }
28+ },
29+ "ha" : {
30+ "class" : HAInterface ,
31+ "name" : "Home Assistant Interface" ,
32+ "args" : {
33+ "ha_url" : {"required" : False , "config" : "ha_url" , "default" : "http://supervisor/core" },
34+ "ha_key" : {"required" : False , "config" : "ha_key" , "default" : os .environ .get ("SUPERVISOR_TOKEN" , None )},
35+ "db_enable" : {"required" : False , "config" : "db_enable" , "default" : False },
36+ "db_mirror_ha" : {"required" : False , "config" : "db_mirror_ha" , "default" : False },
37+ "db_primary" : {"required" : False , "config" : "db_primary" , "default" : False },
38+ }
39+ },
40+ "web" : {
41+ "class" : WebInterface ,
42+ "name" : "Web Interface" ,
43+ "args" : {
44+ "port" : {"required" : False , "config" : "web_port" , "default" : 5052 }
45+ }
46+ },
1947 "gecloud" : {
2048 "class" : GECloudDirect ,
2149 "name" : "GivEnergy Cloud Direct" ,
@@ -101,9 +129,11 @@ def start(self):
101129 raise ValueError (f"{ component_info ['name' ]} API failed to start" )
102130
103131 async def stop (self ):
104- for component_name , component in self .components .items ():
132+ for component_name , component_info in reversed (list (self .components .items ())):
133+ component = self .components [component_name ]
105134 if component :
106135 self .log (f"Stopping { component_name } interface" )
136+ print (component_name , component )
107137 await component .stop ()
108138 self .component_tasks [component_name ] = None
109139 self .components [component_name ] = None
@@ -114,17 +144,20 @@ async def stop(self):
114144
115145 async def select_event (self , entity_id , value ):
116146 for component_name , component in self .components .items ():
117- if component and COMPONENT_LIST [component_name ].get ("event_filter" , "" ) in entity_id :
147+ event_filter = COMPONENT_LIST [component_name ].get ("event_filter" , None )
148+ if component and event_filter and (event_filter in entity_id ):
118149 await component .select_event (entity_id , value )
119150
120151 async def switch_event (self , entity_id , service ):
121152 for component_name , component in self .components .items ():
122- if component and COMPONENT_LIST [component_name ].get ("event_filter" , "" ) in entity_id :
153+ event_filter = COMPONENT_LIST [component_name ].get ("event_filter" , None )
154+ if component and event_filter and (event_filter in entity_id ):
123155 await component .switch_event (entity_id , service )
124156
125157 async def number_event (self , entity_id , value ):
126158 for component_name , component in self .components .items ():
127- if component and COMPONENT_LIST [component_name ].get ("event_filter" , "" ) in entity_id :
159+ event_filter = COMPONENT_LIST [component_name ].get ("event_filter" , None )
160+ if component and event_filter and (event_filter in entity_id ):
128161 await component .number_event (entity_id , value )
129162
130163 def is_all_alive (self ):
@@ -135,9 +168,14 @@ def is_alive(self, name):
135168 """Check if a single component is alive"""
136169 if name not in self .components :
137170 return False
171+ if not self .components [name ]:
172+ # Disabled components can be ignored
173+ return True
138174 if not self .component_tasks [name ] or not self .component_tasks [name ].is_alive ():
139175 return False
140- return self .components [name ].is_alive ()
176+ if not self .components [name ].is_alive ():
177+ return False
178+ return True
141179
142180 def get_active (self ):
143181 active_components = [name for name , comp in self .components .items () if comp ]
0 commit comments