Strictly type modbus __init__.py, validator.py (#56378)

* strictly type: __init__.py, validator.py
This commit is contained in:
jan iversen 2021-09-21 13:43:41 +02:00 committed by GitHub
parent 518c99c8b7
commit c7c789f618
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 7 additions and 7 deletions

View File

@ -2,6 +2,7 @@
from __future__ import annotations
import logging
from typing import cast
import voluptuous as vol
@ -46,6 +47,7 @@ from homeassistant.const import (
)
from homeassistant.core import HomeAssistant
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.typing import ConfigType
from .const import (
ATTR_ADDRESS,
@ -378,10 +380,10 @@ SERVICE_STOP_START_SCHEMA = vol.Schema(
def get_hub(hass: HomeAssistant, name: str) -> ModbusHub:
"""Return modbus hub with name."""
return hass.data[DOMAIN][name]
return cast(ModbusHub, hass.data[DOMAIN][name])
async def async_setup(hass, config):
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Set up Modbus component."""
return await async_modbus_setup(
hass,

View File

@ -84,7 +84,7 @@ DEFAULT_STRUCT_FORMAT = {
}
def struct_validator(config):
def struct_validator(config: dict[str, Any]) -> dict[str, Any]:
"""Sensor schema validator."""
data_type = config[CONF_DATA_TYPE]
@ -154,13 +154,11 @@ def number_validator(value: Any) -> int | float:
return value
try:
value = int(value)
return value
return int(value)
except (TypeError, ValueError):
pass
try:
value = float(value)
return value
return float(value)
except (TypeError, ValueError) as err:
raise vol.Invalid(f"invalid number {value}") from err