@@ -29,11 +29,7 @@ async def async_setup(hass, config):
29
29
conf = config [DOMAIN ]
30
30
controller_id = 0
31
31
ihc_controller = hass .data [f"ihc{ controller_id } " ][IHC_CONTROLLER ]
32
- hass .http .register_view (IHCLogView (ihc_controller , hass ))
33
- hass .http .register_view (IHCProjectView (ihc_controller , hass ))
34
- hass .http .register_view (IHCGetValue (ihc_controller , hass ))
35
- hass .http .register_view (IHCMapping (hass ))
36
- hass .http .register_view (NpmView (hass ))
32
+ hass .http .register_view (IhcViewerApiView (ihc_controller , hass ))
37
33
38
34
register_frontend (hass )
39
35
add_side_panel (hass , conf )
@@ -80,98 +76,47 @@ def register_frontend(hass):
80
76
)
81
77
82
78
83
- class IHCLogView (HomeAssistantView ):
84
- """Get ihc user log ."""
79
+ class IhcViewerApiView (HomeAssistantView ):
80
+ """IHCViewer api requests ."""
85
81
86
- from ihcsdk .ihccontroller import IHCController
87
- from ihcsdk .ihcclient import IHCSoapClient
88
-
89
- url = "/api/ihcviewer/log"
90
- name = "api:ihcviewer:log"
82
+ requires_auth = False
83
+ name = "api:ihcviewer"
84
+ url = r"/api/ihcviewer/{requested_file:.+}"
91
85
92
86
def __init__ (self , ihc_controller , hass ):
93
- """Initilalize the IHCLog view."""
94
- self .ihc_controller = ihc_controller
87
+ """Initilalize the IHCGetValue."""
95
88
self .hass = hass
96
-
97
- @ha .callback
98
- async def get (self , request ):
99
- """Retrieve IHC log."""
100
- log = await self .hass .async_add_executor_job (self .get_user_log )
101
- return self .json (log )
102
-
103
- ihcns = {
104
- "SOAP-ENV" : "http://schemas.xmlsoap.org/soap/envelope/" ,
105
- "ns1" : "utcs" ,
106
- "ns2" : "utcs.values" ,
107
- "ns3" : "utcs.values" ,
108
- }
109
-
110
- def get_user_log (self , language = "en" ):
111
- """Get the controller state."""
112
- payload = """<getUserLog1 xmlns="utcs" />
113
- <getUserLog2 xmlns="utcs">0</getUserLog2>
114
- <getUserLog3 xmlns="utcs">{language}</getUserLog3>
115
- """ .format (
116
- language = language
117
- )
118
- xdoc = self .ihc_controller .client .connection .soap_action (
119
- "/ws/ConfigurationService" , "getUserLog" , payload
120
- )
121
- if xdoc is not None :
122
- base64data = xdoc .find (
123
- "./SOAP-ENV:Body/ns1:getUserLog4/ns1:data" , IHCLogView .ihcns
124
- ).text
125
- if not base64data :
126
- return ""
127
- return base64 .b64decode (base64data ).decode ("UTF-8" )
128
- return ""
129
-
130
-
131
- class IHCProjectView (HomeAssistantView ):
132
- """Get ihc user project."""
133
-
134
- from ihcsdk .ihccontroller import IHCController
135
- from ihcsdk .ihcclient import IHCSoapClient
136
-
137
- url = "/api/ihcviewer/project"
138
- name = "api:ihcviewer:project"
139
-
140
- def __init__ (self , ihc_controller , hass ):
141
- """Initilalize the IHCProjectView."""
142
89
self .ihc_controller = ihc_controller
143
- self .hass = hass
144
90
145
91
@ha .callback
146
- async def get (self , request ):
147
- """Retrieve IHC project."""
148
- project = await self .hass .async_add_executor_job (
149
- self .ihc_controller .get_project
150
- )
151
- return web .Response (
152
- body = project , content_type = "text/xml" , charset = "utf-8" , status = 200
153
- )
92
+ async def get (self , request , requested_file ): # pylint: disable=unused-argument
93
+ """Handle api Web requests."""
154
94
95
+ if requested_file == "log" :
96
+ log = await self .hass .async_add_executor_job (self .get_user_log )
97
+ return web .Response (
98
+ body = log , content_type = "text/plain" , charset = "utf-8" , status = 200
99
+ )
155
100
156
- class IHCGetValue (HomeAssistantView ):
157
- """Get ihc user project."""
101
+ if requested_file == "project" :
102
+ project = await self .hass .async_add_executor_job (
103
+ self .ihc_controller .get_project
104
+ )
105
+ return web .Response (
106
+ body = project , content_type = "text/xml" , charset = "utf-8" , status = 200
107
+ )
158
108
159
- from ihcsdk .ihccontroller import IHCController
160
- from ihcsdk .ihcclient import IHCSoapClient
109
+ if requested_file == "getvalue" :
110
+ data = request .query
111
+ id = int (data .get ("id" ))
112
+ return await self .get_value (id )
161
113
162
- url = "/api/ihcviewer/getvalue"
163
- name = "api:ihcviewer:getvalue"
114
+ if requested_file == "mapping" :
115
+ return await self . get_mapping ()
164
116
165
- def __init__ (self , ihc_controller , hass ):
166
- """Initilalize the IHCGetValue."""
167
- self .ihc_controller = ihc_controller
168
- self .hass = hass
117
+ return web .Response (status = 404 )
169
118
170
- @ha .callback
171
- async def get (self , request ):
172
- """Get runtime value from IHC controller."""
173
- data = request .query
174
- id = int (data .get ("id" ))
119
+ async def get_value (self , id ):
175
120
value = await self .hass .async_add_executor_job (
176
121
self .ihc_controller .client .get_runtime_value , id
177
122
)
@@ -182,57 +127,37 @@ async def get(self, request):
182
127
json = {"value" : value , "type" : type (value ).__name__ , "entity" : entity_id }
183
128
return self .json (json )
184
129
185
-
186
- class IHCMapping (HomeAssistantView ):
187
- """Get mapping of ihc id's to entities."""
188
-
189
- url = "/api/ihcviewer/mapping"
190
- name = "api:ihcviewer:mapping"
191
-
192
- def __init__ (self , hass ):
193
- """Initilalize the IHCGetValue."""
194
- self .hass = hass
195
-
196
- @ha .callback
197
- async def get (self , request ):
198
- """Get ihc mapping."""
199
-
200
- allstates = await self .hass .async_add_executor_job (self .hass .states .all )
130
+ async def get_mapping (self ):
201
131
global ihcmapping
202
132
ihcmapping = {}
133
+ allstates = await self .hass .async_add_executor_job (self .hass .states .all )
203
134
for state in allstates :
204
135
if "ihc_id" in state .attributes :
205
136
ihcmapping [state .attributes ["ihc_id" ]] = state .entity_id
206
137
return self .json (ihcmapping )
207
138
139
+ ihcns = {
140
+ "SOAP-ENV" : "http://schemas.xmlsoap.org/soap/envelope/" ,
141
+ "ns1" : "utcs" ,
142
+ "ns2" : "utcs.values" ,
143
+ "ns3" : "utcs.values" ,
144
+ }
208
145
209
- class NpmView (HomeAssistantView ):
210
- """npm modules"""
211
-
212
- requires_auth = False
213
- name = "ihcviewer_npm"
214
- url = r"/ihcviewer_npm/{requested_file:.+}"
215
-
216
- def __init__ (self , hass ):
217
- """Initilalize the IHCGetValue."""
218
- self .hass = hass
219
-
220
- @ha .callback
221
- async def get (self , request , requested_file ): # pylint: disable=unused-argument
222
- """Handle module Web requests."""
223
- response = await self .hass .async_add_executor_job (
224
- get_file_response , requested_file
146
+ def get_user_log (self , language = "en" ):
147
+ payload = """<getUserLog1 xmlns="utcs" />
148
+ <getUserLog2 xmlns="utcs">0</getUserLog2>
149
+ <getUserLog3 xmlns="utcs">{language}</getUserLog3>
150
+ """ .format (
151
+ language = language
225
152
)
226
- return response
227
-
228
-
229
- def get_file_response (requested_file ):
230
- """Get file."""
231
-
232
- dir = os .path .dirname (__file__ )
233
- servefile = f"{ dir } /node_modules/{ requested_file } "
234
- if os .path .exists (servefile ):
235
- response = web .FileResponse (servefile )
236
- return response
237
-
238
- return web .Response (status = 404 )
153
+ xdoc = self .ihc_controller .client .connection .soap_action (
154
+ "/ws/ConfigurationService" , "getUserLog" , payload
155
+ )
156
+ if xdoc is not None :
157
+ base64data = xdoc .find (
158
+ "./SOAP-ENV:Body/ns1:getUserLog4/ns1:data" , IhcViewerApiView .ihcns
159
+ ).text
160
+ if not base64data :
161
+ return ""
162
+ return base64 .b64decode (base64data ).decode ("UTF-8" )
163
+ return ""
0 commit comments