Skip to content

Commit c06f999

Browse files
include npm url in entity id but not in entity name
1 parent 68092ec commit c06f999

File tree

3 files changed

+43
-86
lines changed

3 files changed

+43
-86
lines changed
Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
"""BlueprintEntity class"""
22
from homeassistant.helpers.update_coordinator import CoordinatorEntity
3+
from homeassistant.helpers.device_registry import DeviceInfo
34
from homeassistant.util import slugify
45

56
from .const import DOMAIN, NAME, VERSION, ATTRIBUTION
@@ -8,37 +9,18 @@
89
class NpmSwitchesEntity(CoordinatorEntity):
910
"""Init NPM user device."""
1011

12+
_attr_has_entity_name = True
13+
1114
def __init__(self, coordinator, config_entry):
1215
super().__init__(coordinator)
16+
self.host = None
17+
self.name = None
18+
self.entity_id = None
1319
self.config_entry = config_entry
1420
self.host_id = None
15-
self.friendly_name = None
1621
self.coordinator = coordinator
17-
18-
@property
19-
def unique_id(self):
20-
"""Return a unique ID to use for this entity."""
21-
return slugify(f"{self.config_entry.entry_id} {self.friendly_name}")
22-
23-
@property
24-
def device_info(self):
25-
return {
26-
"identifiers": {(DOMAIN, self.config_entry.entry_id)},
27-
"name": self.config_entry.title,
28-
"model": VERSION,
29-
"manufacturer": NAME,
30-
}
31-
32-
@property
33-
def extra_state_attributes(self):
34-
"""Return the state attributes."""
35-
return {
36-
"attribution": ATTRIBUTION,
37-
# "id": str(self.coordinator.data.get("id")),
38-
"integration": DOMAIN,
39-
}
40-
41-
@property
42-
def name(self):
43-
"""Return the name of the switch."""
44-
return self.friendly_name
22+
self._attr_unique_id = None
23+
self._attr_device_info = DeviceInfo(
24+
identifiers={(DOMAIN, self.config_entry.entry_id)},
25+
name=self.config_entry.title,
26+
)

custom_components/npm_switches/sensor.py

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Sensor platform for NPM Switches."""
22
from homeassistant.components.sensor import SensorEntity
33
from homeassistant.config_entries import ConfigEntry
4+
from homeassistant.util import slugify
45

56
from .const import DOMAIN
67
from .entity import NpmSwitchesEntity
@@ -39,14 +40,11 @@ def __init__(
3940
) -> None:
4041
"""Initialize proxy switch entity."""
4142
super().__init__(coordinator, entry)
42-
self.proxy_id = name # Unique ID relies on self.proxy_id
43-
self.sensor_name = self.proxy_id
44-
self.friendly_name = "NPM " + self.sensor_name.capitalize() + " Proxy Hosts"
45-
46-
# @property
47-
# def name(self):
48-
# """Return the name of the sensor."""
49-
# return "npm_" + self.sensor_name + "_proxy_hosts"
43+
self.host_id = name
44+
self.sensor_name = self.host_id
45+
self.name = "Proxy Hosts " + self.sensor_name.capitalize()
46+
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
47+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
5048

5149
@property
5250
def native_value(self):
@@ -71,14 +69,11 @@ def __init__(
7169
) -> None:
7270
"""Initialize proxy switch entity."""
7371
super().__init__(coordinator, entry)
74-
self.host_id = name # Unique ID relies on self.host_id
72+
self.host_id = name
7573
self.sensor_name = self.host_id
76-
self.friendly_name = "NPM Redirection Hosts " + self.sensor_name.capitalize()
77-
78-
# @property
79-
# def name(self):
80-
# """Return the name of the sensor."""
81-
# return "npm_" + self.sensor_name + "_proxy_hosts"
74+
self.name = "Redirection Hosts " + self.sensor_name.capitalize()
75+
self.entity_id = "sensor." + slugify(f"{entry.title} {self.name}")
76+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
8277

8378
@property
8479
def native_value(self):
@@ -103,14 +98,11 @@ def __init__(
10398
) -> None:
10499
"""Initialize proxy switch entity."""
105100
super().__init__(coordinator, entry)
106-
self.host_id = name # Unique ID relies on self.host_id
101+
self.host_id = name
107102
self.sensor_name = self.host_id
108-
self.friendly_name = "NPM Steam Hosts " + self.sensor_name.capitalize()
109-
110-
# @property
111-
# def name(self):
112-
# """Return the name of the sensor."""
113-
# return "npm_" + self.sensor_name + "_proxy_hosts"
103+
self.name = "Stream Hosts " + self.sensor_name.capitalize()
104+
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
105+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
114106

115107
@property
116108
def native_value(self):
@@ -135,14 +127,11 @@ def __init__(
135127
) -> None:
136128
"""Initialize proxy switch entity."""
137129
super().__init__(coordinator, entry)
138-
self.host_id = name # Unique ID relies on self.host_id
130+
self.host_id = name
139131
self.sensor_name = self.host_id
140-
self.friendly_name = "NPM Dead Hosts " + self.sensor_name.capitalize()
141-
142-
# @property
143-
# def name(self):
144-
# """Return the name of the sensor."""
145-
# return "npm_" + self.sensor_name + "_proxy_hosts"
132+
self.name = "404 Hosts " + self.sensor_name.capitalize()
133+
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
134+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
146135

147136
@property
148137
def native_value(self):

custom_components/npm_switches/switch.py

Lines changed: 14 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Switch platform for integration_blueprint."""
22
import logging
3-
from homeassistant.components.switch import SwitchEntity
3+
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
4+
from homeassistant.util import slugify
45

56
# from homeassistant.core import HomeAssistant
67
from homeassistant.config_entries import ConfigEntry
@@ -51,11 +52,11 @@ def __init__(
5152
"""Initialize proxy switch entity."""
5253
super().__init__(coordinator, entry)
5354
self.host = host
55+
self.name = "Proxy " + self.host["domain_names"][0].replace(".", " ").capitalize()
56+
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
57+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
5458
self.host_id = str(host["id"])
5559
self.host_type = "proxy-hosts"
56-
self.friendly_name = (
57-
"NPM Proxy " + self.host["domain_names"][0].replace(".", " ").capitalize()
58-
)
5960

6061
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
6162
"""Turn on the switch."""
@@ -106,11 +107,11 @@ def __init__(
106107
"""Initialize redir switch entity."""
107108
super().__init__(coordinator, entry)
108109
self.host = host
110+
self.name = "Redirect " + self.host["domain_names"][0].replace(".", " ").capitalize()
111+
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
112+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
109113
self.host_type = "redirection-hosts"
110114
self.host_id = str(host["id"])
111-
self.friendly_name = (
112-
"NPM Redir " + self.host["domain_names"][0].replace(".", " ").capitalize()
113-
)
114115

115116
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
116117
"""Turn on the switch."""
@@ -124,11 +125,6 @@ async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
124125
self.async_write_ha_state()
125126
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
126127

127-
# @property
128-
# def name(self):
129-
# """Return the name of the switch."""
130-
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
131-
132128
@property
133129
def icon(self):
134130
"""Return the icon of this switch."""
@@ -162,11 +158,11 @@ def __init__(
162158
"""Initialize steam switch entity."""
163159
super().__init__(coordinator, entry)
164160
self.host = host
161+
self.name = "Stream " + str(self.host["incoming_port"])
162+
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
163+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
165164
self.host_type = "streams"
166165
self.host_id = str(host["id"])
167-
self.friendly_name = (
168-
"NPM Stream " + str(self.host["incoming_port"])
169-
)
170166

171167
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
172168
"""Turn on the switch."""
@@ -180,11 +176,6 @@ async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
180176
self.async_write_ha_state()
181177
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
182178

183-
# @property
184-
# def name(self):
185-
# """Return the name of the switch."""
186-
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
187-
188179
@property
189180
def icon(self):
190181
"""Return the icon of this switch."""
@@ -219,11 +210,11 @@ def __init__(
219210
"""Initialize redir switch entity."""
220211
super().__init__(coordinator, entry)
221212
self.host = host
213+
self.name = "404 " + self.host["domain_names"][0].replace(".", " ").capitalize()
214+
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
215+
self._attr_unique_id = f"{entry.entry_id} {self.name}"
222216
self.host_type = "dead-hosts"
223217
self.host_id = str(host["id"])
224-
self.friendly_name = (
225-
"NPM Dead " + self.host["domain_names"][0].replace(".", " ").capitalize()
226-
)
227218

228219
async def async_turn_on(self, **kwargs): # pylint: disable=unused-argument
229220
"""Turn on the switch."""
@@ -237,11 +228,6 @@ async def async_turn_off(self, **kwargs): # pylint: disable=unused-argument
237228
self.async_write_ha_state()
238229
self.host = await self.coordinator.api.get_host(self.host_id, self.host_type)
239230

240-
# @property
241-
# def name(self):
242-
# """Return the name of the switch."""
243-
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()
244-
245231
@property
246232
def icon(self):
247233
"""Return the icon of this switch."""

0 commit comments

Comments
 (0)