Add unique_id and device_info for SMS sensor (#54257)

This commit is contained in:
Maciej Bieniek 2021-08-09 08:15:39 +02:00 committed by GitHub
parent 2e903c92c4
commit 0f68ebe92b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -3,7 +3,7 @@ import logging
import gammu # pylint: disable=import-error import gammu # pylint: disable=import-error
from homeassistant.components.sensor import SensorEntity from homeassistant.components.sensor import SensorEntity, SensorEntityDescription
from homeassistant.const import DEVICE_CLASS_SIGNAL_STRENGTH, SIGNAL_STRENGTH_DECIBELS from homeassistant.const import DEVICE_CLASS_SIGNAL_STRENGTH, SIGNAL_STRENGTH_DECIBELS
from .const import DOMAIN, SMS_GATEWAY from .const import DOMAIN, SMS_GATEWAY
@ -14,48 +14,40 @@ _LOGGER = logging.getLogger(__name__)
async def async_setup_entry(hass, config_entry, async_add_entities): async def async_setup_entry(hass, config_entry, async_add_entities):
"""Set up the GSM Signal Sensor sensor.""" """Set up the GSM Signal Sensor sensor."""
gateway = hass.data[DOMAIN][SMS_GATEWAY] gateway = hass.data[DOMAIN][SMS_GATEWAY]
entities = []
imei = await gateway.get_imei_async() imei = await gateway.get_imei_async()
name = f"gsm_signal_imei_{imei}" async_add_entities(
entities.append( [
GSMSignalSensor( GSMSignalSensor(
hass, hass,
gateway, gateway,
name, imei,
SensorEntityDescription(
key="signal",
name=f"gsm_signal_imei_{imei}",
device_class=DEVICE_CLASS_SIGNAL_STRENGTH,
unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
entity_registry_enabled_default=False,
),
) )
],
True,
) )
async_add_entities(entities, True)
class GSMSignalSensor(SensorEntity): class GSMSignalSensor(SensorEntity):
"""Implementation of a GSM Signal sensor.""" """Implementation of a GSM Signal sensor."""
def __init__( def __init__(self, hass, gateway, imei, description):
self,
hass,
gateway,
name,
):
"""Initialize the GSM Signal sensor.""" """Initialize the GSM Signal sensor."""
self._attr_device_info = {
"identifiers": {(DOMAIN, str(imei))},
"name": "SMS Gateway",
}
self._attr_unique_id = str(imei)
self._hass = hass self._hass = hass
self._gateway = gateway self._gateway = gateway
self._name = name
self._state = None self._state = None
self.entity_description = description
@property
def name(self):
"""Return the name of the sensor."""
return self._name
@property
def unit_of_measurement(self):
"""Return the unit the value is expressed in."""
return SIGNAL_STRENGTH_DECIBELS
@property
def device_class(self):
"""Return the class of this sensor."""
return DEVICE_CLASS_SIGNAL_STRENGTH
@property @property
def available(self): def available(self):
@ -78,8 +70,3 @@ class GSMSignalSensor(SensorEntity):
def extra_state_attributes(self): def extra_state_attributes(self):
"""Return the sensor attributes.""" """Return the sensor attributes."""
return self._state return self._state
@property
def entity_registry_enabled_default(self) -> bool:
"""Return if the entity should be enabled when first added to the entity registry."""
return False