2020-06-23 03:41:55 +00:00
|
|
|
"""Support for SMS dongle sensor."""
|
|
|
|
import logging
|
|
|
|
|
2021-03-02 08:02:04 +00:00
|
|
|
import gammu # pylint: disable=import-error
|
2020-06-23 03:41:55 +00:00
|
|
|
|
2021-12-20 12:54:10 +00:00
|
|
|
from homeassistant.components.sensor import (
|
|
|
|
SensorDeviceClass,
|
|
|
|
SensorEntity,
|
|
|
|
SensorEntityDescription,
|
|
|
|
)
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-12-20 12:54:10 +00:00
|
|
|
from homeassistant.const import SIGNAL_STRENGTH_DECIBELS
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-10-27 11:16:59 +00:00
|
|
|
from homeassistant.helpers.entity import DeviceInfo
|
2022-01-03 18:06:08 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2020-06-23 03:41:55 +00:00
|
|
|
|
|
|
|
from .const import DOMAIN, SMS_GATEWAY
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
|
2022-01-03 18:06:08 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2020-06-23 03:41:55 +00:00
|
|
|
"""Set up the GSM Signal Sensor sensor."""
|
|
|
|
gateway = hass.data[DOMAIN][SMS_GATEWAY]
|
|
|
|
imei = await gateway.get_imei_async()
|
2021-08-09 06:15:39 +00:00
|
|
|
async_add_entities(
|
|
|
|
[
|
|
|
|
GSMSignalSensor(
|
|
|
|
hass,
|
|
|
|
gateway,
|
|
|
|
imei,
|
|
|
|
SensorEntityDescription(
|
|
|
|
key="signal",
|
|
|
|
name=f"gsm_signal_imei_{imei}",
|
2021-12-20 12:54:10 +00:00
|
|
|
device_class=SensorDeviceClass.SIGNAL_STRENGTH,
|
2021-08-12 15:40:55 +00:00
|
|
|
native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS,
|
2021-08-09 06:15:39 +00:00
|
|
|
entity_registry_enabled_default=False,
|
|
|
|
),
|
|
|
|
)
|
|
|
|
],
|
|
|
|
True,
|
2020-08-27 11:56:20 +00:00
|
|
|
)
|
2020-06-23 03:41:55 +00:00
|
|
|
|
|
|
|
|
2021-03-22 18:54:14 +00:00
|
|
|
class GSMSignalSensor(SensorEntity):
|
2020-06-23 03:41:55 +00:00
|
|
|
"""Implementation of a GSM Signal sensor."""
|
|
|
|
|
2021-08-09 06:15:39 +00:00
|
|
|
def __init__(self, hass, gateway, imei, description):
|
2020-06-23 03:41:55 +00:00
|
|
|
"""Initialize the GSM Signal sensor."""
|
2021-10-27 11:16:59 +00:00
|
|
|
self._attr_device_info = DeviceInfo(
|
|
|
|
identifiers={(DOMAIN, str(imei))},
|
|
|
|
name="SMS Gateway",
|
|
|
|
)
|
2021-08-09 06:15:39 +00:00
|
|
|
self._attr_unique_id = str(imei)
|
2020-06-23 03:41:55 +00:00
|
|
|
self._hass = hass
|
|
|
|
self._gateway = gateway
|
|
|
|
self._state = None
|
2021-08-09 06:15:39 +00:00
|
|
|
self.entity_description = description
|
2020-06-23 03:41:55 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def available(self):
|
|
|
|
"""Return if the sensor data are available."""
|
|
|
|
return self._state is not None
|
|
|
|
|
|
|
|
@property
|
2021-08-12 15:40:55 +00:00
|
|
|
def native_value(self):
|
2020-06-23 03:41:55 +00:00
|
|
|
"""Return the state of the device."""
|
|
|
|
return self._state["SignalStrength"]
|
|
|
|
|
|
|
|
async def async_update(self):
|
|
|
|
"""Get the latest data from the modem."""
|
|
|
|
try:
|
|
|
|
self._state = await self._gateway.get_signal_quality_async()
|
2021-03-02 08:02:04 +00:00
|
|
|
except gammu.GSMError as exc:
|
2020-06-23 03:41:55 +00:00
|
|
|
_LOGGER.error("Failed to read signal quality: %s", exc)
|
|
|
|
|
|
|
|
@property
|
2021-03-11 20:23:20 +00:00
|
|
|
def extra_state_attributes(self):
|
2020-06-23 03:41:55 +00:00
|
|
|
"""Return the sensor attributes."""
|
|
|
|
return self._state
|