From 82058f0b5021b801d162bb9218521b0dbf9c9f33 Mon Sep 17 00:00:00 2001 From: Oscar Calvo <2091582+ocalvo@users.noreply.github.com> Date: Mon, 22 Jun 2020 20:41:55 -0700 Subject: [PATCH] Part 2: Add signal sensor (#34406) --- homeassistant/components/sms/__init__.py | 23 ++++++- homeassistant/components/sms/gateway.py | 4 ++ homeassistant/components/sms/sensor.py | 76 +++++++++++++++++++++++ homeassistant/components/sms/strings.json | 1 - 4 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 homeassistant/components/sms/sensor.py diff --git a/homeassistant/components/sms/__init__.py b/homeassistant/components/sms/__init__.py index e78fda305da..0b8f9d986e3 100644 --- a/homeassistant/components/sms/__init__.py +++ b/homeassistant/components/sms/__init__.py @@ -1,4 +1,5 @@ """The sms component.""" +import asyncio import logging import voluptuous as vol @@ -13,6 +14,8 @@ from .gateway import create_sms_gateway _LOGGER = logging.getLogger(__name__) +PLATFORMS = ["sensor"] + CONFIG_SCHEMA = vol.Schema( {DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.isdevice})}, extra=vol.ALLOW_EXTRA, @@ -44,13 +47,27 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry): if not gateway: return False hass.data[DOMAIN][SMS_GATEWAY] = gateway + for component in PLATFORMS: + hass.async_create_task( + hass.config_entries.async_forward_entry_setup(entry, component) + ) return True async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry): """Unload a config entry.""" + unload_ok = all( + await asyncio.gather( + *[ + hass.config_entries.async_forward_entry_unload(entry, component) + for component in PLATFORMS + ] + ) + ) - gateway = hass.data[DOMAIN].pop(SMS_GATEWAY) - await gateway.terminate_async() - return True + if unload_ok: + gateway = hass.data[DOMAIN].pop(SMS_GATEWAY) + await gateway.terminate_async() + + return unload_ok diff --git a/homeassistant/components/sms/gateway.py b/homeassistant/components/sms/gateway.py index 8a75808f751..7a2c86d8ba9 100644 --- a/homeassistant/components/sms/gateway.py +++ b/homeassistant/components/sms/gateway.py @@ -24,6 +24,10 @@ class Gateway: """Get the IMEI of the device.""" return await self._worker.get_imei_async() + async def get_signal_quality_async(self): + """Get the current signal level of the modem.""" + return await self._worker.get_signal_quality_async() + async def terminate_async(self): """Terminate modem connection.""" return await self._worker.terminate_async() diff --git a/homeassistant/components/sms/sensor.py b/homeassistant/components/sms/sensor.py new file mode 100644 index 00000000000..08168994b07 --- /dev/null +++ b/homeassistant/components/sms/sensor.py @@ -0,0 +1,76 @@ +"""Support for SMS dongle sensor.""" +import logging + +import gammu # pylint: disable=import-error, no-member + +from homeassistant.const import DEVICE_CLASS_SIGNAL_STRENGTH +from homeassistant.helpers.entity import Entity + +from .const import DOMAIN, SMS_GATEWAY + +_LOGGER = logging.getLogger(__name__) + + +async def async_setup_entry(hass, config_entry, async_add_entities): + """Set up the GSM Signal Sensor sensor.""" + gateway = hass.data[DOMAIN][SMS_GATEWAY] + entities = [] + imei = await gateway.get_imei_async() + name = f"gsm_signal_imei_{imei}" + entities.append(GSMSignalSensor(hass, gateway, name,)) + async_add_entities(entities, True) + + +class GSMSignalSensor(Entity): + """Implementation of a GSM Signal sensor.""" + + def __init__( + self, hass, gateway, name, + ): + """Initialize the GSM Signal sensor.""" + self._hass = hass + self._gateway = gateway + self._name = name + self._state = None + + @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 "dB" + + @property + def device_class(self): + """Return the class of this sensor.""" + return DEVICE_CLASS_SIGNAL_STRENGTH + + @property + def available(self): + """Return if the sensor data are available.""" + return self._state is not None + + @property + def state(self): + """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() + except gammu.GSMError as exc: # pylint: disable=no-member + _LOGGER.error("Failed to read signal quality: %s", exc) + + @property + def device_state_attributes(self): + """Return the sensor attributes.""" + 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 diff --git a/homeassistant/components/sms/strings.json b/homeassistant/components/sms/strings.json index 6f92631e2e1..872cb17cbea 100644 --- a/homeassistant/components/sms/strings.json +++ b/homeassistant/components/sms/strings.json @@ -14,6 +14,5 @@ "already_configured": "[%key:common::config_flow::abort::already_configured_device%]", "single_instance_allowed": "[%key:common::config_flow::abort::single_instance_allowed%]" } - } }