Skip to content

Commit

Permalink
include npm url in entity id but not in entity name
Browse files Browse the repository at this point in the history
  • Loading branch information
InTheDaylight14 committed Nov 13, 2024
1 parent 68092ec commit c06f999
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 86 deletions.
40 changes: 11 additions & 29 deletions custom_components/npm_switches/entity.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""BlueprintEntity class"""
from homeassistant.helpers.update_coordinator import CoordinatorEntity
from homeassistant.helpers.device_registry import DeviceInfo
from homeassistant.util import slugify

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

_attr_has_entity_name = True

def __init__(self, coordinator, config_entry):
super().__init__(coordinator)
self.host = None
self.name = None
self.entity_id = None
self.config_entry = config_entry
self.host_id = None
self.friendly_name = None
self.coordinator = coordinator

@property
def unique_id(self):
"""Return a unique ID to use for this entity."""
return slugify(f"{self.config_entry.entry_id} {self.friendly_name}")

@property
def device_info(self):
return {
"identifiers": {(DOMAIN, self.config_entry.entry_id)},
"name": self.config_entry.title,
"model": VERSION,
"manufacturer": NAME,
}

@property
def extra_state_attributes(self):
"""Return the state attributes."""
return {
"attribution": ATTRIBUTION,
# "id": str(self.coordinator.data.get("id")),
"integration": DOMAIN,
}

@property
def name(self):
"""Return the name of the switch."""
return self.friendly_name
self._attr_unique_id = None
self._attr_device_info = DeviceInfo(
identifiers={(DOMAIN, self.config_entry.entry_id)},
name=self.config_entry.title,
)
47 changes: 18 additions & 29 deletions custom_components/npm_switches/sensor.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Sensor platform for NPM Switches."""
from homeassistant.components.sensor import SensorEntity
from homeassistant.config_entries import ConfigEntry
from homeassistant.util import slugify

from .const import DOMAIN
from .entity import NpmSwitchesEntity
Expand Down Expand Up @@ -39,14 +40,11 @@ def __init__(
) -> None:
"""Initialize proxy switch entity."""
super().__init__(coordinator, entry)
self.proxy_id = name # Unique ID relies on self.proxy_id
self.sensor_name = self.proxy_id
self.friendly_name = "NPM " + self.sensor_name.capitalize() + " Proxy Hosts"

# @property
# def name(self):
# """Return the name of the sensor."""
# return "npm_" + self.sensor_name + "_proxy_hosts"
self.host_id = name
self.sensor_name = self.host_id
self.name = "Proxy Hosts " + self.sensor_name.capitalize()
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"

@property
def native_value(self):
Expand All @@ -71,14 +69,11 @@ def __init__(
) -> None:
"""Initialize proxy switch entity."""
super().__init__(coordinator, entry)
self.host_id = name # Unique ID relies on self.host_id
self.host_id = name
self.sensor_name = self.host_id
self.friendly_name = "NPM Redirection Hosts " + self.sensor_name.capitalize()

# @property
# def name(self):
# """Return the name of the sensor."""
# return "npm_" + self.sensor_name + "_proxy_hosts"
self.name = "Redirection Hosts " + self.sensor_name.capitalize()
self.entity_id = "sensor." + slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"

@property
def native_value(self):
Expand All @@ -103,14 +98,11 @@ def __init__(
) -> None:
"""Initialize proxy switch entity."""
super().__init__(coordinator, entry)
self.host_id = name # Unique ID relies on self.host_id
self.host_id = name
self.sensor_name = self.host_id
self.friendly_name = "NPM Steam Hosts " + self.sensor_name.capitalize()

# @property
# def name(self):
# """Return the name of the sensor."""
# return "npm_" + self.sensor_name + "_proxy_hosts"
self.name = "Stream Hosts " + self.sensor_name.capitalize()
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"

@property
def native_value(self):
Expand All @@ -135,14 +127,11 @@ def __init__(
) -> None:
"""Initialize proxy switch entity."""
super().__init__(coordinator, entry)
self.host_id = name # Unique ID relies on self.host_id
self.host_id = name
self.sensor_name = self.host_id
self.friendly_name = "NPM Dead Hosts " + self.sensor_name.capitalize()

# @property
# def name(self):
# """Return the name of the sensor."""
# return "npm_" + self.sensor_name + "_proxy_hosts"
self.name = "404 Hosts " + self.sensor_name.capitalize()
self.entity_id = "sensor."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"

@property
def native_value(self):
Expand Down
42 changes: 14 additions & 28 deletions custom_components/npm_switches/switch.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""Switch platform for integration_blueprint."""
import logging
from homeassistant.components.switch import SwitchEntity
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
from homeassistant.util import slugify

# from homeassistant.core import HomeAssistant
from homeassistant.config_entries import ConfigEntry
Expand Down Expand Up @@ -51,11 +52,11 @@ def __init__(
"""Initialize proxy switch entity."""
super().__init__(coordinator, entry)
self.host = host
self.name = "Proxy " + self.host["domain_names"][0].replace(".", " ").capitalize()
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"
self.host_id = str(host["id"])
self.host_type = "proxy-hosts"
self.friendly_name = (
"NPM Proxy " + self.host["domain_names"][0].replace(".", " ").capitalize()
)

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

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

# @property
# def name(self):
# """Return the name of the switch."""
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()

@property
def icon(self):
"""Return the icon of this switch."""
Expand Down Expand Up @@ -162,11 +158,11 @@ def __init__(
"""Initialize steam switch entity."""
super().__init__(coordinator, entry)
self.host = host
self.name = "Stream " + str(self.host["incoming_port"])
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"
self.host_type = "streams"
self.host_id = str(host["id"])
self.friendly_name = (
"NPM Stream " + str(self.host["incoming_port"])
)

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

# @property
# def name(self):
# """Return the name of the switch."""
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()

@property
def icon(self):
"""Return the icon of this switch."""
Expand Down Expand Up @@ -219,11 +210,11 @@ def __init__(
"""Initialize redir switch entity."""
super().__init__(coordinator, entry)
self.host = host
self.name = "404 " + self.host["domain_names"][0].replace(".", " ").capitalize()
self.entity_id = "switch."+slugify(f"{entry.title} {self.name}")
self._attr_unique_id = f"{entry.entry_id} {self.name}"
self.host_type = "dead-hosts"
self.host_id = str(host["id"])
self.friendly_name = (
"NPM Dead " + self.host["domain_names"][0].replace(".", " ").capitalize()
)

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

# @property
# def name(self):
# """Return the name of the switch."""
# return "NPM " + self.host["domain_names"][0].replace(".", " ").capitalize()

@property
def icon(self):
"""Return the icon of this switch."""
Expand Down

0 comments on commit c06f999

Please sign in to comment.