mirror of
https://github.com/home-assistant/core
synced 2024-09-03 08:14:07 +02:00
Support custom baud speed (#68320)
Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
parent
5d9dc8252b
commit
1cc9800a93
@ -1,4 +1,6 @@
|
||||
"""The sms component."""
|
||||
import logging
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
|
||||
@ -7,13 +9,24 @@ from homeassistant.core import HomeAssistant
|
||||
from homeassistant.helpers import config_validation as cv
|
||||
from homeassistant.helpers.typing import ConfigType
|
||||
|
||||
from .const import DOMAIN, SMS_GATEWAY
|
||||
from .const import CONF_BAUD_SPEED, DEFAULT_BAUD_SPEED, DOMAIN, SMS_GATEWAY
|
||||
from .gateway import create_sms_gateway
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
PLATFORMS = [Platform.SENSOR]
|
||||
|
||||
SMS_CONFIG_SCHEMA = {vol.Required(CONF_DEVICE): cv.isdevice}
|
||||
|
||||
CONFIG_SCHEMA = vol.Schema(
|
||||
{DOMAIN: vol.Schema({vol.Required(CONF_DEVICE): cv.isdevice})},
|
||||
{
|
||||
DOMAIN: vol.Schema(
|
||||
vol.All(
|
||||
cv.deprecated(CONF_DEVICE),
|
||||
SMS_CONFIG_SCHEMA,
|
||||
),
|
||||
)
|
||||
},
|
||||
extra=vol.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
@ -39,7 +52,12 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
||||
"""Configure Gammu state machine."""
|
||||
|
||||
device = entry.data[CONF_DEVICE]
|
||||
config = {"Device": device, "Connection": "at"}
|
||||
connection_mode = "at"
|
||||
baud_speed = entry.data.get(CONF_BAUD_SPEED, DEFAULT_BAUD_SPEED)
|
||||
if baud_speed != DEFAULT_BAUD_SPEED:
|
||||
connection_mode += baud_speed
|
||||
config = {"Device": device, "Connection": connection_mode}
|
||||
_LOGGER.debug("Connecting mode:%s", connection_mode)
|
||||
gateway = await create_sms_gateway(config, hass)
|
||||
if not gateway:
|
||||
return False
|
||||
|
@ -6,13 +6,21 @@ import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries, core, exceptions
|
||||
from homeassistant.const import CONF_DEVICE
|
||||
from homeassistant.helpers import selector
|
||||
|
||||
from .const import DOMAIN
|
||||
from .const import CONF_BAUD_SPEED, DEFAULT_BAUD_SPEED, DEFAULT_BAUD_SPEEDS, DOMAIN
|
||||
from .gateway import create_sms_gateway
|
||||
|
||||
_LOGGER = logging.getLogger(__name__)
|
||||
|
||||
DATA_SCHEMA = vol.Schema({vol.Required(CONF_DEVICE): str})
|
||||
DATA_SCHEMA = vol.Schema(
|
||||
{
|
||||
vol.Required(CONF_DEVICE): str,
|
||||
vol.Optional(CONF_BAUD_SPEED, default=DEFAULT_BAUD_SPEED): selector.selector(
|
||||
{"select": {"options": DEFAULT_BAUD_SPEEDS}}
|
||||
),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def get_imei_from_config(hass: core.HomeAssistant, data):
|
||||
@ -21,13 +29,17 @@ async def get_imei_from_config(hass: core.HomeAssistant, data):
|
||||
Data has the keys from DATA_SCHEMA with values provided by the user.
|
||||
"""
|
||||
device = data[CONF_DEVICE]
|
||||
config = {"Device": device, "Connection": "at"}
|
||||
connection_mode = "at"
|
||||
baud_speed = data.get(CONF_BAUD_SPEED, DEFAULT_BAUD_SPEED)
|
||||
if baud_speed != DEFAULT_BAUD_SPEED:
|
||||
connection_mode += baud_speed
|
||||
config = {"Device": device, "Connection": connection_mode}
|
||||
gateway = await create_sms_gateway(config, hass)
|
||||
if not gateway:
|
||||
raise CannotConnect
|
||||
try:
|
||||
imei = await gateway.get_imei_async()
|
||||
except gammu.GSMError as err:
|
||||
except gammu.GSMError as err: # pylint: disable=no-member
|
||||
raise CannotConnect from err
|
||||
finally:
|
||||
await gateway.terminate_async()
|
||||
|
@ -3,3 +3,27 @@
|
||||
DOMAIN = "sms"
|
||||
SMS_GATEWAY = "SMS_GATEWAY"
|
||||
SMS_STATE_UNREAD = "UnRead"
|
||||
CONF_BAUD_SPEED = "baud_speed"
|
||||
DEFAULT_BAUD_SPEED = "0"
|
||||
DEFAULT_BAUD_SPEEDS = [
|
||||
{"value": DEFAULT_BAUD_SPEED, "label": "Auto"},
|
||||
{"value": "50", "label": "50"},
|
||||
{"value": "75", "label": "75"},
|
||||
{"value": "110", "label": "110"},
|
||||
{"value": "134", "label": "134"},
|
||||
{"value": "150", "label": "150"},
|
||||
{"value": "200", "label": "200"},
|
||||
{"value": "300", "label": "300"},
|
||||
{"value": "600", "label": "600"},
|
||||
{"value": "1200", "label": "1200"},
|
||||
{"value": "1800", "label": "1800"},
|
||||
{"value": "2400", "label": "2400"},
|
||||
{"value": "4800", "label": "4800"},
|
||||
{"value": "9600", "label": "9600"},
|
||||
{"value": "19200", "label": "19200"},
|
||||
{"value": "28800", "label": "28800"},
|
||||
{"value": "38400", "label": "38400"},
|
||||
{"value": "57600", "label": "57600"},
|
||||
{"value": "76800", "label": "76800"},
|
||||
{"value": "115200", "label": "115200"},
|
||||
]
|
||||
|
@ -16,6 +16,7 @@ class Gateway:
|
||||
|
||||
def __init__(self, config, hass):
|
||||
"""Initialize the sms gateway."""
|
||||
_LOGGER.debug("Init with connection mode:%s", config["Connection"])
|
||||
self._worker = GammuAsyncWorker(self.sms_pull)
|
||||
self._worker.configure(config)
|
||||
self._hass = hass
|
||||
|
@ -3,7 +3,10 @@
|
||||
"step": {
|
||||
"user": {
|
||||
"title": "Connect to the modem",
|
||||
"data": { "device": "Device" }
|
||||
"data": {
|
||||
"device": "Device",
|
||||
"baud_speed": "Baud Speed"
|
||||
}
|
||||
}
|
||||
},
|
||||
"error": {
|
||||
|
Loading…
Reference in New Issue
Block a user