diff --git a/custom_components/npm_switches/entity.py b/custom_components/npm_switches/entity.py index 9f61528..66c1f71 100644 --- a/custom_components/npm_switches/entity.py +++ b/custom_components/npm_switches/entity.py @@ -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 @@ -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, + ) diff --git a/custom_components/npm_switches/sensor.py b/custom_components/npm_switches/sensor.py index 5abcab4..f4525be 100644 --- a/custom_components/npm_switches/sensor.py +++ b/custom_components/npm_switches/sensor.py @@ -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 @@ -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): @@ -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): @@ -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): @@ -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): diff --git a/custom_components/npm_switches/switch.py b/custom_components/npm_switches/switch.py index 82f8a06..fb04014 100644 --- a/custom_components/npm_switches/switch.py +++ b/custom_components/npm_switches/switch.py @@ -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 @@ -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.""" @@ -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.""" @@ -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.""" @@ -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.""" @@ -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.""" @@ -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.""" @@ -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."""