MySensors: Code style improvements

This commit is contained in:
functionpointer
2021-01-27 22:03:42 +01:00
parent 0ddd9bae05
commit 6d2abbfe31
6 changed files with 55 additions and 59 deletions

View File

@@ -569,19 +569,19 @@ omit =
homeassistant/components/mycroft/*
homeassistant/components/mycroft/notify.py
homeassistant/components/mysensors/__init__.py
homeassistant/components/mysensors/binary_sensor.py
homeassistant/components/mysensors/climate.py
homeassistant/components/mysensors/const.py
homeassistant/components/mysensors/cover.py
homeassistant/components/mysensors/device.py
homeassistant/components/mysensors/device_tracker.py
homeassistant/components/mysensors/gateway.py
homeassistant/components/mysensors/handler.py
homeassistant/components/mysensors/helpers.py
homeassistant/components/mysensors/notify.py
homeassistant/components/mysensors/binary_sensor.py
homeassistant/components/mysensors/light.py
homeassistant/components/mysensors/climate.py
homeassistant/components/mysensors/cover.py
homeassistant/components/mysensors/device_tracker.py
homeassistant/components/mysensors/notify.py
homeassistant/components/mysensors/sensor.py
homeassistant/components/mysensors/switch.py
homeassistant/components/mysensors/gateway.py
homeassistant/components/mystrom/binary_sensor.py
homeassistant/components/mystrom/light.py
homeassistant/components/mystrom/switch.py

View File

@@ -194,12 +194,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
"""Remove an instance of the MySensors integration."""
_LOGGER.debug("Unload entry: %s (id: %s)", entry.title, entry.entry_id)
gateway = get_mysensors_gateway(hass, entry.entry_id)
if not gateway:
_LOGGER.error("Can't unload configentry %s, no gateway found", entry.entry_id)
return False
unload_ok = all(
await asyncio.gather(

View File

@@ -4,6 +4,8 @@ from typing import Dict, Optional
import voluptuous as vol
from homeassistant import config_entries
from homeassistant.components.mqtt import valid_publish_topic, valid_subscribe_topic
from homeassistant.components.mysensors import (
CONF_DEVICE,
DEFAULT_BAUD_RATE,
@@ -13,8 +15,8 @@ from homeassistant.components.mysensors import (
import homeassistant.helpers.config_validation as cv
from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION
from ... import config_entries
from ..mqtt import valid_publish_topic, valid_subscribe_topic
# pylint: disable=unused-import
from .const import (
CONF_BAUD_RATE,
CONF_GATEWAY_TYPE,
@@ -22,12 +24,12 @@ from .const import (
CONF_GATEWAY_TYPE_MQTT,
CONF_GATEWAY_TYPE_SERIAL,
CONF_GATEWAY_TYPE_TCP,
CONF_GATEWAY_TYPE_TYPE,
CONF_PERSISTENCE_FILE,
CONF_TCP_PORT,
CONF_TOPIC_IN_PREFIX,
CONF_TOPIC_OUT_PREFIX,
DOMAIN,
ConfGatewayType,
)
from .gateway import MQTT_COMPONENT, is_serial_port, is_socket_address, try_connect
@@ -71,7 +73,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
entered from the frontend.
Therefore we process it as though it came from the frontend.
"""
if user_input.get(CONF_DEVICE) == MQTT_COMPONENT:
if user_input[CONF_DEVICE] == MQTT_COMPONENT:
user_input[CONF_GATEWAY_TYPE] = CONF_GATEWAY_TYPE_MQTT
else:
try:
@@ -87,18 +89,17 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def async_step_user(self, user_input: Optional[Dict[str, str]] = None):
"""Create a config entry from frontend user input."""
schema = dict()
schema[vol.Required(CONF_GATEWAY_TYPE)] = vol.In(CONF_GATEWAY_TYPE_ALL)
schema = {vol.Required(CONF_GATEWAY_TYPE): vol.In(CONF_GATEWAY_TYPE_ALL)}
schema = vol.Schema(schema)
if user_input is not None and CONF_GATEWAY_TYPE in user_input:
if user_input is not None:
gw_type = user_input[CONF_GATEWAY_TYPE]
input_pass = user_input if CONF_DEVICE in user_input else None
if gw_type == CONF_GATEWAY_TYPE_MQTT:
return await self.async_step_gw_mqtt(input_pass)
elif gw_type == CONF_GATEWAY_TYPE_TCP:
if gw_type == CONF_GATEWAY_TYPE_TCP:
return await self.async_step_gw_tcp(input_pass)
elif gw_type == CONF_GATEWAY_TYPE_SERIAL:
if gw_type == CONF_GATEWAY_TYPE_SERIAL:
return await self.async_step_gw_serial(input_pass)
return self.async_show_form(step_id="user", data_schema=schema)
@@ -189,7 +190,7 @@ class MySensorsConfigFlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
async def validate_common(
self,
gw_type: CONF_GATEWAY_TYPE_TYPE,
gw_type: ConfGatewayType,
user_input: Optional[Dict[str, str]] = None,
errors: Optional[Dict[str, str]] = None,
) -> Dict[str, str]:

View File

@@ -16,10 +16,10 @@ CONF_TOPIC_IN_PREFIX: str = "topic_in_prefix"
CONF_TOPIC_OUT_PREFIX: str = "topic_out_prefix"
CONF_VERSION: str = "version"
CONF_GATEWAY_TYPE: str = "gateway_type"
CONF_GATEWAY_TYPE_TYPE = Literal["Serial", "TCP", "MQTT"]
CONF_GATEWAY_TYPE_SERIAL: CONF_GATEWAY_TYPE_TYPE = "Serial"
CONF_GATEWAY_TYPE_TCP: CONF_GATEWAY_TYPE_TYPE = "TCP"
CONF_GATEWAY_TYPE_MQTT: CONF_GATEWAY_TYPE_TYPE = "MQTT"
ConfGatewayType = Literal["Serial", "TCP", "MQTT"]
CONF_GATEWAY_TYPE_SERIAL: ConfGatewayType = "Serial"
CONF_GATEWAY_TYPE_TCP: ConfGatewayType = "TCP"
CONF_GATEWAY_TYPE_MQTT: ConfGatewayType = "MQTT"
CONF_GATEWAY_TYPE_ALL: List[str] = [
CONF_GATEWAY_TYPE_MQTT,
CONF_GATEWAY_TYPE_SERIAL,

View File

@@ -71,34 +71,33 @@ async def try_connect(hass: HomeAssistantType, user_input: Dict[str, str]) -> bo
)
if gateway is None:
return False
else:
gateway_ready = asyncio.Future()
gateway_ready = asyncio.Future()
def gateway_ready_callback(msg):
msg_type = msg.gateway.const.MessageType(msg.type)
_LOGGER.debug("Received MySensors msg type %s: %s", msg_type.name, msg)
if msg_type.name != "internal":
return
internal = msg.gateway.const.Internal(msg.sub_type)
if internal.name != "I_GATEWAY_READY":
return
_LOGGER.debug("Received gateway ready")
gateway_ready.set_result(True)
def gateway_ready_callback(msg):
msg_type = msg.gateway.const.MessageType(msg.type)
_LOGGER.debug("Received MySensors msg type %s: %s", msg_type.name, msg)
if msg_type.name != "internal":
return
internal = msg.gateway.const.Internal(msg.sub_type)
if internal.name != "I_GATEWAY_READY":
return
_LOGGER.debug("Received gateway ready")
gateway_ready.set_result(True)
gateway.event_callback = gateway_ready_callback
connect_task = None
try:
connect_task = asyncio.create_task(gateway.start())
with async_timeout.timeout(5):
await gateway_ready
return True
except asyncio.TimeoutError:
_LOGGER.info("Try gateway connect failed with timeout")
return False
finally:
if connect_task is not None and not connect_task.done():
connect_task.cancel()
asyncio.create_task(gateway.stop())
gateway.event_callback = gateway_ready_callback
connect_task = None
try:
connect_task = asyncio.create_task(gateway.start())
with async_timeout.timeout(5):
await gateway_ready
return True
except asyncio.TimeoutError:
_LOGGER.info("Try gateway connect failed with timeout")
return False
finally:
if connect_task is not None and not connect_task.done():
connect_task.cancel()
asyncio.create_task(gateway.stop())
except OSError as err:
_LOGGER.info("Try gateway connect failed with exception", exc_info=err)
return False
@@ -144,12 +143,12 @@ async def _get_gateway(
persistence_file = data.get(CONF_PERSISTENCE_FILE, f"mysensors_{unique_id}.json")
# interpret relative paths to be in hass config folder. absolute paths will be left as they are
persistence_file = hass.config.path(persistence_file)
version = data.get(CONF_VERSION)
device = data.get(CONF_DEVICE)
baud_rate = data.get(CONF_BAUD_RATE)
tcp_port = data.get(CONF_TCP_PORT)
in_prefix = data.get(CONF_TOPIC_IN_PREFIX, "")
out_prefix = data.get(CONF_TOPIC_OUT_PREFIX, "")
version: str = data[CONF_VERSION]
device: str = data[CONF_DEVICE]
baud_rate: Optional[int] = data.get(CONF_BAUD_RATE)
tcp_port: Optional[int] = data.get(CONF_TCP_PORT)
in_prefix: str = data.get(CONF_TOPIC_IN_PREFIX, "")
out_prefix: str = data.get(CONF_TOPIC_OUT_PREFIX, "")
if device == MQTT_COMPONENT:
# what is the purpose of this?

View File

@@ -14,7 +14,6 @@ from homeassistant.components.mysensors.const import (
CONF_GATEWAY_TYPE_MQTT,
CONF_GATEWAY_TYPE_SERIAL,
CONF_GATEWAY_TYPE_TCP,
CONF_GATEWAY_TYPE_TYPE,
CONF_GATEWAYS,
CONF_PERSISTENCE,
CONF_PERSISTENCE_FILE,
@@ -24,13 +23,14 @@ from homeassistant.components.mysensors.const import (
CONF_TOPIC_OUT_PREFIX,
CONF_VERSION,
DOMAIN,
ConfGatewayType,
)
from homeassistant.components.mysensors.gateway import is_serial_port
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
async def get_form(
hass: HomeAssistantType, gatway_type: CONF_GATEWAY_TYPE_TYPE, expected_step_id: str
hass: HomeAssistantType, gatway_type: ConfGatewayType, expected_step_id: str
):
"""Get a form for the given gateway type."""
await setup.async_setup_component(hass, "persistent_notification", {})
@@ -220,7 +220,7 @@ async def test_fail_to_connect(hass: HomeAssistantType):
async def config_invalid(
hass: HomeAssistantType,
gatway_type: CONF_GATEWAY_TYPE_TYPE,
gatway_type: ConfGatewayType,
expected_step_id: str,
user_input: Dict[str, any],
err_field,