Support custom baud speed (#68320)

Co-authored-by: Franck Nijhof <frenck@frenck.nl>
This commit is contained in:
Oscar Calvo 2022-05-09 03:06:29 -07:00 committed by GitHub
parent 5d9dc8252b
commit 1cc9800a93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 66 additions and 8 deletions

View file

@ -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()