Skip to content

Commit 68092ec

Browse files
add stream and dead host switches
1 parent db2033a commit 68092ec

File tree

1 file changed

+153
-38
lines changed

1 file changed

+153
-38
lines changed

custom_components/npm_switches/switch.py

Lines changed: 153 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,22 @@ async def async_setup_entry(hass, entry, async_add_entities):
1818
api = hass.data[DOMAIN][entry.entry_id].api
1919
proxy_hosts = await api.get_proxy_hosts()
2020
redir_hosts = await api.get_redirection_hosts()
21+
stream_hosts = await api.get_stream_hosts()
22+
dead_hosts = await api.get_dead_hosts()
2123
entities = []
2224

2325
if entry.data["include_proxy_hosts"]:
24-
for proxy in proxy_hosts.values():
25-
entities.append(NpmProxyBinarySwitch(coordinator, entry, proxy))
26+
for proxy_host in proxy_hosts.values():
27+
entities.append(NpmProxyBinarySwitch(coordinator, entry, proxy_host))
2628
if entry.data["include_redirection_hosts"]:
27-
for redir in redir_hosts.values():
28-
entities.append(NpmRedirBinarySwitch(coordinator, entry, redir))
29-
# if entry.data["include_stream_hosts"]:
30-
# for stream in stream_hosts.values():
31-
# entities.append(NpmRedirBinarySwitch(coordinator, entry, stream))
32-
# if entry.data["include_dead_hosts"]:
33-
# for dead in dead_hosts.values():
34-
# entities.append(NpmRedirBinarySwitch(coordinator, entry, dead))
29+
for redir_host in redir_hosts.values():
30+
entities.append(NpmRedirBinarySwitch(coordinator, entry, redir_host))
31+
if entry.data["include_stream_hosts"]:
32+
for stream_host in stream_hosts.values():
33+
entities.append(NpmStreamBinarySwitch(coordinator, entry, stream_host))
34+
if entry.data["include_dead_hosts"]:
35+
for dead_host in dead_hosts.values():
36+
entities.append(NpmDeadBinarySwitch(coordinator, entry, dead_host))
3537

3638
async_add_entities(entities, True)
3739
# async_add_devices([NpmProxyBinarySwitch(coordinator, entry, "20")])
@@ -44,52 +46,52 @@ def __init__(
4446
self,
4547
coordinator: NpmSwitchesUpdateCoordinator,
4648
entry: ConfigEntry,
47-
proxy: dict,
49+
host: dict,
4850
) -> None:
4951
"""Initialize proxy switch entity."""
5052
super().__init__(coordinator, entry)
51-
self.proxy = proxy
52-
self.proxy_id = str(proxy["id"])
53+
self.host = host
54+
self.host_id = str(host["id"])
5355
self.host_type = "proxy-hosts"
5456
self.friendly_name = (
55-
"NPM " + self.proxy["domain_names"][0].replace(".", " ").capitalize()
57+
"NPM Proxy " + self.host["domain_names"][0].replace(".", " ").capitalize()
5658
)
5759

5860
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
5961
"""Turn on the switch."""
60-
await self.coordinator.api.enable_host(self.proxy_id, "proxy-hosts")
62+
await self.coordinator.api.enable_host(self.host_id, self.host_type)
6163
self.async_write_ha_state()
62-
self.proxy = await self.coordinator.api.get_host(self.proxy_id, self.host_type)
64+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
6365

6466
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
6567
"""Turn off the switch."""
66-
await self.coordinator.api.disable_host(self.proxy_id, "proxy-hosts")
68+
await self.coordinator.api.disable_host(self.host_id, self.host_type)
6769
self.async_write_ha_state()
68-
self.proxy = await self.coordinator.api.get_host(self.proxy_id, self.host_type)
70+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
6971

7072
# @property
7173
# def name(self):
7274
# """Return the name of the switch."""
73-
# return "NPM " + self.proxy["domain_names"][0].replace(".", " ").capitalize()
75+
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
7476

7577
@property
7678
def icon(self):
7779
"""Return the icon of this switch."""
78-
if self.coordinator.api.is_host_enabled(self.proxy_id, self.host_type):
80+
if self.coordinator.api.is_host_enabled(self.host_id, self.host_type):
7981
return "mdi:check-network"
8082
return "mdi:close-network"
8183

8284
@property
8385
def is_on(self):
8486
"""Return true if the switch is on."""
85-
return self.coordinator.api.is_host_enabled(self.proxy_id, self.host_type)
87+
return self.coordinator.api.is_host_enabled(self.host_id, self.host_type)
8688

8789
@property
8890
def extra_state_attributes(self):
8991
"""Return device state attributes."""
9092
return {
91-
"id": self.proxy["id"],
92-
"domain_names": self.proxy["domain_names"],
93+
"id": self.host["id"],
94+
"domain_names": self.host["domain_names"],
9395
}
9496

9597
class NpmRedirBinarySwitch(NpmSwitchesEntity, SwitchEntity):
@@ -99,51 +101,164 @@ def __init__(
99101
self,
100102
coordinator: NpmSwitchesUpdateCoordinator,
101103
entry: ConfigEntry,
102-
proxy: dict,
104+
host: dict,
103105
) -> None:
104-
"""Initialize proxy switch entity."""
106+
"""Initialize redir switch entity."""
105107
super().__init__(coordinator, entry)
106-
self.proxy = proxy
108+
self.host = host
107109
self.host_type = "redirection-hosts"
108-
self.proxy_id = str(proxy["id"])
110+
self.host_id = str(host["id"])
111+
self.friendly_name = (
112+
"NPM Redir " + self.host["domain_names"][0].replace(".", " ").capitalize()
113+
)
114+
115+
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
116+
"""Turn on the switch."""
117+
await self.coordinator.api.enable_host(self.host_id, self.host_type)
118+
self.async_write_ha_state()
119+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
120+
121+
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
122+
"""Turn off the switch."""
123+
await self.coordinator.api.disable_host(self.host_id, self.host_type)
124+
self.async_write_ha_state()
125+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
126+
127+
# @property
128+
# def name(self):
129+
# """Return the name of the switch."""
130+
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
131+
132+
@property
133+
def icon(self):
134+
"""Return the icon of this switch."""
135+
if self.coordinator.api.is_host_enabled(self.host_id, self.host_type):
136+
return "mdi:check-network"
137+
return "mdi:close-network"
138+
139+
@property
140+
def is_on(self):
141+
"""Return true if the switch is on."""
142+
return self.coordinator.api.is_host_enabled(self.host_id, self.host_type)
143+
144+
@property
145+
def extra_state_attributes(self):
146+
"""Return device state attributes."""
147+
return {
148+
"id": self.host["id"],
149+
"domain_names": self.host["domain_names"],
150+
# "forward_domain_name": self.host["forward_domain_names"],
151+
}
152+
153+
class NpmStreamBinarySwitch(NpmSwitchesEntity, SwitchEntity):
154+
"""Switches to enable/disable the Redir Host Type in NPM"""
155+
156+
def __init__(
157+
self,
158+
coordinator: NpmSwitchesUpdateCoordinator,
159+
entry: ConfigEntry,
160+
host: dict,
161+
) -> None:
162+
"""Initialize steam switch entity."""
163+
super().__init__(coordinator, entry)
164+
self.host = host
165+
self.host_type = "streams"
166+
self.host_id = str(host["id"])
167+
self.friendly_name = (
168+
"NPM Stream " + str(self.host["incoming_port"])
169+
)
170+
171+
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
172+
"""Turn on the switch."""
173+
await self.coordinator.api.enable_host(self.host_id, self.host_type)
174+
self.async_write_ha_state()
175+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
176+
177+
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
178+
"""Turn off the switch."""
179+
await self.coordinator.api.disable_host(self.host_id, self.host_type)
180+
self.async_write_ha_state()
181+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
182+
183+
# @property
184+
# def name(self):
185+
# """Return the name of the switch."""
186+
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
187+
188+
@property
189+
def icon(self):
190+
"""Return the icon of this switch."""
191+
if self.coordinator.api.is_host_enabled(self.host_id, self.host_type):
192+
return "mdi:check-network"
193+
return "mdi:close-network"
194+
195+
@property
196+
def is_on(self):
197+
"""Return true if the switch is on."""
198+
return self.coordinator.api.is_host_enabled(self.host_id, self.host_type)
199+
200+
@property
201+
def extra_state_attributes(self):
202+
"""Return device state attributes."""
203+
return {
204+
"id": self.host["id"],
205+
"forwarding_host": self.host["forwarding_host"],
206+
"forwarding_port": self.host["forwarding_port"],
207+
# "forward_domain_name": self.host["forward_domain_names"],
208+
}
209+
210+
class NpmDeadBinarySwitch(NpmSwitchesEntity, SwitchEntity):
211+
"""Switches to enable/disable the Dead Host Type in NPM"""
212+
213+
def __init__(
214+
self,
215+
coordinator: NpmSwitchesUpdateCoordinator,
216+
entry: ConfigEntry,
217+
host: dict,
218+
) -> None:
219+
"""Initialize redir switch entity."""
220+
super().__init__(coordinator, entry)
221+
self.host = host
222+
self.host_type = "dead-hosts"
223+
self.host_id = str(host["id"])
109224
self.friendly_name = (
110-
"NPM Redir " + self.proxy["domain_names"][0].replace(".", " ").capitalize()
225+
"NPM Dead " + self.host["domain_names"][0].replace(".", " ").capitalize()
111226
)
112227

113228
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
114229
"""Turn on the switch."""
115-
await self.coordinator.api.enable_host(self.proxy_id, "redirection-hosts")
230+
await self.coordinator.api.enable_host(self.host_id, self.host_type)
116231
self.async_write_ha_state()
117-
self.proxy = await self.coordinator.api.get_host(self.proxy_id, self.host_type)
232+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
118233

119234
async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
120235
"""Turn off the switch."""
121-
await self.coordinator.api.disable_host(self.proxy_id, "redirection-hosts")
236+
await self.coordinator.api.disable_host(self.host_id, self.host_type)
122237
self.async_write_ha_state()
123-
self.proxy = await self.coordinator.api.get_host(self.proxy_id, self.host_type)
238+
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
124239

125240
# @property
126241
# def name(self):
127242
# """Return the name of the switch."""
128-
# return "NPM " + self.proxy["domain_names"][0].replace(".", " ").capitalize()
243+
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
129244

130245
@property
131246
def icon(self):
132247
"""Return the icon of this switch."""
133-
if self.coordinator.api.is_host_enabled(self.proxy_id, self.host_type):
248+
if self.coordinator.api.is_host_enabled(self.host_id, self.host_type):
134249
return "mdi:check-network"
135250
return "mdi:close-network"
136251

137252
@property
138253
def is_on(self):
139254
"""Return true if the switch is on."""
140-
return self.coordinator.api.is_host_enabled(self.proxy_id, self.host_type)
255+
return self.coordinator.api.is_host_enabled(self.host_id, self.host_type)
141256

142257
@property
143258
def extra_state_attributes(self):
144259
"""Return device state attributes."""
145260
return {
146-
"id": self.proxy["id"],
147-
"domain_names": self.proxy["domain_names"],
148-
# "forward_domain_name": self.proxy["forward_domain_names"],
261+
"id": self.host["id"],
262+
"domain_names": self.host["domain_names"],
263+
# "forward_domain_name": self.host["forward_domain_names"],
149264
}

0 commit comments

Comments
 (0)