-
-
Notifications
You must be signed in to change notification settings - Fork 37.1k
More sensors for SMS integration #70486
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
2b60f55
5b7d549
366ff8d
19c8ff4
532d259
157bb6b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,17 @@ | ||
| """Constants for sms Component.""" | ||
| from typing import Final | ||
|
|
||
| from homeassistant.components.sensor import SensorDeviceClass, SensorEntityDescription | ||
| from homeassistant.const import PERCENTAGE, SIGNAL_STRENGTH_DECIBELS | ||
| from homeassistant.helpers.entity import EntityCategory | ||
|
|
||
| DOMAIN = "sms" | ||
| SMS_GATEWAY = "SMS_GATEWAY" | ||
| SMS_STATE_UNREAD = "UnRead" | ||
| SIGNAL_COORDINATOR = "signal_coordinator" | ||
| NETWORK_COORDINATOR = "network_coordinator" | ||
| GATEWAY = "gateway" | ||
| DEFAULT_SCAN_INTERVAL = 30 | ||
| CONF_BAUD_SPEED = "baud_speed" | ||
| DEFAULT_BAUD_SPEED = "0" | ||
| DEFAULT_BAUD_SPEEDS = [ | ||
|
|
@@ -27,3 +36,61 @@ | |
| {"value": "76800", "label": "76800"}, | ||
| {"value": "115200", "label": "115200"}, | ||
| ] | ||
|
|
||
| SIGNAL_SENSORS: Final[dict[str, SensorEntityDescription]] = { | ||
| "SignalStrength": SensorEntityDescription( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For a future PR, you can add state_class=MEASUREMENT for this and others so stats are being generated |
||
| key="SignalStrength", | ||
| name="Signal Strength", | ||
| device_class=SensorDeviceClass.SIGNAL_STRENGTH, | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| "SignalPercent": SensorEntityDescription( | ||
| key="SignalPercent", | ||
| icon="mdi:signal-cellular-3", | ||
| name="Signal Percent", | ||
| native_unit_of_measurement=PERCENTAGE, | ||
| entity_registry_enabled_default=True, | ||
| ), | ||
| "BitErrorRate": SensorEntityDescription( | ||
| key="BitErrorRate", | ||
| name="Bit Error Rate", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| native_unit_of_measurement=PERCENTAGE, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| } | ||
|
|
||
| NETWORK_SENSORS: Final[dict[str, SensorEntityDescription]] = { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the dict keys are not used, why are these dicts and not tuples? |
||
| "NetworkName": SensorEntityDescription( | ||
| key="NetworkName", | ||
| name="Network Name", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| "State": SensorEntityDescription( | ||
| key="State", | ||
| name="Network Status", | ||
| entity_registry_enabled_default=True, | ||
| ), | ||
| "NetworkCode": SensorEntityDescription( | ||
| key="NetworkCode", | ||
| name="GSM network code", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| "CID": SensorEntityDescription( | ||
| key="CID", | ||
| name="Cell ID", | ||
| icon="mdi:radio-tower", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| "LAC": SensorEntityDescription( | ||
| key="LAC", | ||
| name="Local Area Code", | ||
| entity_category=EntityCategory.DIAGNOSTIC, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,84 +1,67 @@ | ||
| """Support for SMS dongle sensor.""" | ||
| import logging | ||
|
|
||
| import gammu # pylint: disable=import-error | ||
|
|
||
| from homeassistant.components.sensor import ( | ||
| SensorDeviceClass, | ||
| SensorEntity, | ||
| SensorEntityDescription, | ||
| ) | ||
| from homeassistant.components.sensor import SensorEntity | ||
| from homeassistant.config_entries import ConfigEntry | ||
| from homeassistant.const import SIGNAL_STRENGTH_DECIBELS | ||
| from homeassistant.core import HomeAssistant | ||
| from homeassistant.helpers.entity import DeviceInfo | ||
| from homeassistant.helpers.entity_platform import AddEntitiesCallback | ||
| from homeassistant.helpers.update_coordinator import CoordinatorEntity | ||
|
|
||
| from .const import DOMAIN, SMS_GATEWAY | ||
|
|
||
| _LOGGER = logging.getLogger(__name__) | ||
| from .const import ( | ||
| DOMAIN, | ||
| GATEWAY, | ||
| NETWORK_COORDINATOR, | ||
| NETWORK_SENSORS, | ||
| SIGNAL_COORDINATOR, | ||
| SIGNAL_SENSORS, | ||
| SMS_GATEWAY, | ||
| ) | ||
|
|
||
|
|
||
| async def async_setup_entry( | ||
| hass: HomeAssistant, | ||
| config_entry: ConfigEntry, | ||
| async_add_entities: AddEntitiesCallback, | ||
| ) -> None: | ||
| """Set up the GSM Signal Sensor sensor.""" | ||
| gateway = hass.data[DOMAIN][SMS_GATEWAY] | ||
| imei = await gateway.get_imei_async() | ||
| async_add_entities( | ||
| [ | ||
| GSMSignalSensor( | ||
| hass, | ||
| gateway, | ||
| imei, | ||
| SensorEntityDescription( | ||
| key="signal", | ||
| name=f"gsm_signal_imei_{imei}", | ||
| device_class=SensorDeviceClass.SIGNAL_STRENGTH, | ||
| native_unit_of_measurement=SIGNAL_STRENGTH_DECIBELS, | ||
| entity_registry_enabled_default=False, | ||
| ), | ||
| """Set up all device sensors.""" | ||
| sms_data = hass.data[DOMAIN][SMS_GATEWAY] | ||
| signal_coordinator = sms_data[SIGNAL_COORDINATOR] | ||
| network_coordinator = sms_data[NETWORK_COORDINATOR] | ||
| gateway = sms_data[GATEWAY] | ||
| unique_id = str(await gateway.get_imei_async()) | ||
| entities = [] | ||
| for description in SIGNAL_SENSORS.values(): | ||
| entities.append( | ||
| DeviceSensor( | ||
| signal_coordinator, | ||
| description, | ||
| unique_id, | ||
| ) | ||
| ) | ||
| for description in NETWORK_SENSORS.values(): | ||
| entities.append( | ||
| DeviceSensor( | ||
| network_coordinator, | ||
| description, | ||
| unique_id, | ||
| ) | ||
| ], | ||
| True, | ||
| ) | ||
| ) | ||
| async_add_entities(entities, True) | ||
|
|
||
|
|
||
| class GSMSignalSensor(SensorEntity): | ||
| """Implementation of a GSM Signal sensor.""" | ||
| class DeviceSensor(CoordinatorEntity, SensorEntity): | ||
| """Implementation of a device sensor.""" | ||
|
|
||
| def __init__(self, hass, gateway, imei, description): | ||
| """Initialize the GSM Signal sensor.""" | ||
| def __init__(self, coordinator, description, unique_id): | ||
| """Initialize the device sensor.""" | ||
| super().__init__(coordinator) | ||
| self._attr_device_info = DeviceInfo( | ||
| identifiers={(DOMAIN, str(imei))}, | ||
| identifiers={(DOMAIN, unique_id)}, | ||
| name="SMS Gateway", | ||
| ) | ||
| self._attr_unique_id = str(imei) | ||
| self._hass = hass | ||
| self._gateway = gateway | ||
| self._state = None | ||
| self._attr_unique_id = f"{unique_id}_{description.key}" | ||
| self.entity_description = description | ||
|
|
||
| @property | ||
| def available(self): | ||
| """Return if the sensor data are available.""" | ||
| return self._state is not None | ||
|
|
||
| @property | ||
| def native_value(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: | ||
| _LOGGER.error("Failed to read signal quality: %s", exc) | ||
|
|
||
| @property | ||
| def extra_state_attributes(self): | ||
| """Return the sensor attributes.""" | ||
| return self._state | ||
| return self.coordinator.data.get(self.entity_description.key) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move the sensor entity descriptions to the sensor platform. These are platform details.