mirror of
https://github.com/home-assistant/core.git
synced 2025-08-05 13:45:12 +02:00
MySensors: Code style improvements
This commit is contained in:
12
.coveragerc
12
.coveragerc
@@ -569,19 +569,19 @@ omit =
|
|||||||
homeassistant/components/mycroft/*
|
homeassistant/components/mycroft/*
|
||||||
homeassistant/components/mycroft/notify.py
|
homeassistant/components/mycroft/notify.py
|
||||||
homeassistant/components/mysensors/__init__.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/const.py
|
||||||
|
homeassistant/components/mysensors/cover.py
|
||||||
homeassistant/components/mysensors/device.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/handler.py
|
||||||
homeassistant/components/mysensors/helpers.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/light.py
|
||||||
homeassistant/components/mysensors/climate.py
|
homeassistant/components/mysensors/notify.py
|
||||||
homeassistant/components/mysensors/cover.py
|
|
||||||
homeassistant/components/mysensors/device_tracker.py
|
|
||||||
homeassistant/components/mysensors/sensor.py
|
homeassistant/components/mysensors/sensor.py
|
||||||
homeassistant/components/mysensors/switch.py
|
homeassistant/components/mysensors/switch.py
|
||||||
homeassistant/components/mysensors/gateway.py
|
|
||||||
homeassistant/components/mystrom/binary_sensor.py
|
homeassistant/components/mystrom/binary_sensor.py
|
||||||
homeassistant/components/mystrom/light.py
|
homeassistant/components/mystrom/light.py
|
||||||
homeassistant/components/mystrom/switch.py
|
homeassistant/components/mystrom/switch.py
|
||||||
|
@@ -194,12 +194,8 @@ async def async_setup_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool
|
|||||||
|
|
||||||
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
async def async_unload_entry(hass: HomeAssistantType, entry: ConfigEntry) -> bool:
|
||||||
"""Remove an instance of the MySensors integration."""
|
"""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)
|
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(
|
unload_ok = all(
|
||||||
await asyncio.gather(
|
await asyncio.gather(
|
||||||
|
@@ -4,6 +4,8 @@ from typing import Dict, Optional
|
|||||||
|
|
||||||
import voluptuous as vol
|
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 (
|
from homeassistant.components.mysensors import (
|
||||||
CONF_DEVICE,
|
CONF_DEVICE,
|
||||||
DEFAULT_BAUD_RATE,
|
DEFAULT_BAUD_RATE,
|
||||||
@@ -13,8 +15,8 @@ from homeassistant.components.mysensors import (
|
|||||||
import homeassistant.helpers.config_validation as cv
|
import homeassistant.helpers.config_validation as cv
|
||||||
|
|
||||||
from . import CONF_RETAIN, CONF_VERSION, DEFAULT_VERSION
|
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 (
|
from .const import (
|
||||||
CONF_BAUD_RATE,
|
CONF_BAUD_RATE,
|
||||||
CONF_GATEWAY_TYPE,
|
CONF_GATEWAY_TYPE,
|
||||||
@@ -22,12 +24,12 @@ from .const import (
|
|||||||
CONF_GATEWAY_TYPE_MQTT,
|
CONF_GATEWAY_TYPE_MQTT,
|
||||||
CONF_GATEWAY_TYPE_SERIAL,
|
CONF_GATEWAY_TYPE_SERIAL,
|
||||||
CONF_GATEWAY_TYPE_TCP,
|
CONF_GATEWAY_TYPE_TCP,
|
||||||
CONF_GATEWAY_TYPE_TYPE,
|
|
||||||
CONF_PERSISTENCE_FILE,
|
CONF_PERSISTENCE_FILE,
|
||||||
CONF_TCP_PORT,
|
CONF_TCP_PORT,
|
||||||
CONF_TOPIC_IN_PREFIX,
|
CONF_TOPIC_IN_PREFIX,
|
||||||
CONF_TOPIC_OUT_PREFIX,
|
CONF_TOPIC_OUT_PREFIX,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
ConfGatewayType,
|
||||||
)
|
)
|
||||||
from .gateway import MQTT_COMPONENT, is_serial_port, is_socket_address, try_connect
|
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.
|
entered from the frontend.
|
||||||
Therefore we process it as though it came 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
|
user_input[CONF_GATEWAY_TYPE] = CONF_GATEWAY_TYPE_MQTT
|
||||||
else:
|
else:
|
||||||
try:
|
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):
|
async def async_step_user(self, user_input: Optional[Dict[str, str]] = None):
|
||||||
"""Create a config entry from frontend user input."""
|
"""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)
|
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]
|
gw_type = user_input[CONF_GATEWAY_TYPE]
|
||||||
input_pass = user_input if CONF_DEVICE in user_input else None
|
input_pass = user_input if CONF_DEVICE in user_input else None
|
||||||
if gw_type == CONF_GATEWAY_TYPE_MQTT:
|
if gw_type == CONF_GATEWAY_TYPE_MQTT:
|
||||||
return await self.async_step_gw_mqtt(input_pass)
|
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)
|
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 await self.async_step_gw_serial(input_pass)
|
||||||
|
|
||||||
return self.async_show_form(step_id="user", data_schema=schema)
|
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(
|
async def validate_common(
|
||||||
self,
|
self,
|
||||||
gw_type: CONF_GATEWAY_TYPE_TYPE,
|
gw_type: ConfGatewayType,
|
||||||
user_input: Optional[Dict[str, str]] = None,
|
user_input: Optional[Dict[str, str]] = None,
|
||||||
errors: Optional[Dict[str, str]] = None,
|
errors: Optional[Dict[str, str]] = None,
|
||||||
) -> Dict[str, str]:
|
) -> Dict[str, str]:
|
||||||
|
@@ -16,10 +16,10 @@ CONF_TOPIC_IN_PREFIX: str = "topic_in_prefix"
|
|||||||
CONF_TOPIC_OUT_PREFIX: str = "topic_out_prefix"
|
CONF_TOPIC_OUT_PREFIX: str = "topic_out_prefix"
|
||||||
CONF_VERSION: str = "version"
|
CONF_VERSION: str = "version"
|
||||||
CONF_GATEWAY_TYPE: str = "gateway_type"
|
CONF_GATEWAY_TYPE: str = "gateway_type"
|
||||||
CONF_GATEWAY_TYPE_TYPE = Literal["Serial", "TCP", "MQTT"]
|
ConfGatewayType = Literal["Serial", "TCP", "MQTT"]
|
||||||
CONF_GATEWAY_TYPE_SERIAL: CONF_GATEWAY_TYPE_TYPE = "Serial"
|
CONF_GATEWAY_TYPE_SERIAL: ConfGatewayType = "Serial"
|
||||||
CONF_GATEWAY_TYPE_TCP: CONF_GATEWAY_TYPE_TYPE = "TCP"
|
CONF_GATEWAY_TYPE_TCP: ConfGatewayType = "TCP"
|
||||||
CONF_GATEWAY_TYPE_MQTT: CONF_GATEWAY_TYPE_TYPE = "MQTT"
|
CONF_GATEWAY_TYPE_MQTT: ConfGatewayType = "MQTT"
|
||||||
CONF_GATEWAY_TYPE_ALL: List[str] = [
|
CONF_GATEWAY_TYPE_ALL: List[str] = [
|
||||||
CONF_GATEWAY_TYPE_MQTT,
|
CONF_GATEWAY_TYPE_MQTT,
|
||||||
CONF_GATEWAY_TYPE_SERIAL,
|
CONF_GATEWAY_TYPE_SERIAL,
|
||||||
|
@@ -71,34 +71,33 @@ async def try_connect(hass: HomeAssistantType, user_input: Dict[str, str]) -> bo
|
|||||||
)
|
)
|
||||||
if gateway is None:
|
if gateway is None:
|
||||||
return False
|
return False
|
||||||
else:
|
gateway_ready = asyncio.Future()
|
||||||
gateway_ready = asyncio.Future()
|
|
||||||
|
|
||||||
def gateway_ready_callback(msg):
|
def gateway_ready_callback(msg):
|
||||||
msg_type = msg.gateway.const.MessageType(msg.type)
|
msg_type = msg.gateway.const.MessageType(msg.type)
|
||||||
_LOGGER.debug("Received MySensors msg type %s: %s", msg_type.name, msg)
|
_LOGGER.debug("Received MySensors msg type %s: %s", msg_type.name, msg)
|
||||||
if msg_type.name != "internal":
|
if msg_type.name != "internal":
|
||||||
return
|
return
|
||||||
internal = msg.gateway.const.Internal(msg.sub_type)
|
internal = msg.gateway.const.Internal(msg.sub_type)
|
||||||
if internal.name != "I_GATEWAY_READY":
|
if internal.name != "I_GATEWAY_READY":
|
||||||
return
|
return
|
||||||
_LOGGER.debug("Received gateway ready")
|
_LOGGER.debug("Received gateway ready")
|
||||||
gateway_ready.set_result(True)
|
gateway_ready.set_result(True)
|
||||||
|
|
||||||
gateway.event_callback = gateway_ready_callback
|
gateway.event_callback = gateway_ready_callback
|
||||||
connect_task = None
|
connect_task = None
|
||||||
try:
|
try:
|
||||||
connect_task = asyncio.create_task(gateway.start())
|
connect_task = asyncio.create_task(gateway.start())
|
||||||
with async_timeout.timeout(5):
|
with async_timeout.timeout(5):
|
||||||
await gateway_ready
|
await gateway_ready
|
||||||
return True
|
return True
|
||||||
except asyncio.TimeoutError:
|
except asyncio.TimeoutError:
|
||||||
_LOGGER.info("Try gateway connect failed with timeout")
|
_LOGGER.info("Try gateway connect failed with timeout")
|
||||||
return False
|
return False
|
||||||
finally:
|
finally:
|
||||||
if connect_task is not None and not connect_task.done():
|
if connect_task is not None and not connect_task.done():
|
||||||
connect_task.cancel()
|
connect_task.cancel()
|
||||||
asyncio.create_task(gateway.stop())
|
asyncio.create_task(gateway.stop())
|
||||||
except OSError as err:
|
except OSError as err:
|
||||||
_LOGGER.info("Try gateway connect failed with exception", exc_info=err)
|
_LOGGER.info("Try gateway connect failed with exception", exc_info=err)
|
||||||
return False
|
return False
|
||||||
@@ -144,12 +143,12 @@ async def _get_gateway(
|
|||||||
persistence_file = data.get(CONF_PERSISTENCE_FILE, f"mysensors_{unique_id}.json")
|
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
|
# interpret relative paths to be in hass config folder. absolute paths will be left as they are
|
||||||
persistence_file = hass.config.path(persistence_file)
|
persistence_file = hass.config.path(persistence_file)
|
||||||
version = data.get(CONF_VERSION)
|
version: str = data[CONF_VERSION]
|
||||||
device = data.get(CONF_DEVICE)
|
device: str = data[CONF_DEVICE]
|
||||||
baud_rate = data.get(CONF_BAUD_RATE)
|
baud_rate: Optional[int] = data.get(CONF_BAUD_RATE)
|
||||||
tcp_port = data.get(CONF_TCP_PORT)
|
tcp_port: Optional[int] = data.get(CONF_TCP_PORT)
|
||||||
in_prefix = data.get(CONF_TOPIC_IN_PREFIX, "")
|
in_prefix: str = data.get(CONF_TOPIC_IN_PREFIX, "")
|
||||||
out_prefix = data.get(CONF_TOPIC_OUT_PREFIX, "")
|
out_prefix: str = data.get(CONF_TOPIC_OUT_PREFIX, "")
|
||||||
|
|
||||||
if device == MQTT_COMPONENT:
|
if device == MQTT_COMPONENT:
|
||||||
# what is the purpose of this?
|
# what is the purpose of this?
|
||||||
|
@@ -14,7 +14,6 @@ from homeassistant.components.mysensors.const import (
|
|||||||
CONF_GATEWAY_TYPE_MQTT,
|
CONF_GATEWAY_TYPE_MQTT,
|
||||||
CONF_GATEWAY_TYPE_SERIAL,
|
CONF_GATEWAY_TYPE_SERIAL,
|
||||||
CONF_GATEWAY_TYPE_TCP,
|
CONF_GATEWAY_TYPE_TCP,
|
||||||
CONF_GATEWAY_TYPE_TYPE,
|
|
||||||
CONF_GATEWAYS,
|
CONF_GATEWAYS,
|
||||||
CONF_PERSISTENCE,
|
CONF_PERSISTENCE,
|
||||||
CONF_PERSISTENCE_FILE,
|
CONF_PERSISTENCE_FILE,
|
||||||
@@ -24,13 +23,14 @@ from homeassistant.components.mysensors.const import (
|
|||||||
CONF_TOPIC_OUT_PREFIX,
|
CONF_TOPIC_OUT_PREFIX,
|
||||||
CONF_VERSION,
|
CONF_VERSION,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
|
ConfGatewayType,
|
||||||
)
|
)
|
||||||
from homeassistant.components.mysensors.gateway import is_serial_port
|
from homeassistant.components.mysensors.gateway import is_serial_port
|
||||||
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
from homeassistant.helpers.typing import ConfigType, HomeAssistantType
|
||||||
|
|
||||||
|
|
||||||
async def get_form(
|
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."""
|
"""Get a form for the given gateway type."""
|
||||||
await setup.async_setup_component(hass, "persistent_notification", {})
|
await setup.async_setup_component(hass, "persistent_notification", {})
|
||||||
@@ -220,7 +220,7 @@ async def test_fail_to_connect(hass: HomeAssistantType):
|
|||||||
|
|
||||||
async def config_invalid(
|
async def config_invalid(
|
||||||
hass: HomeAssistantType,
|
hass: HomeAssistantType,
|
||||||
gatway_type: CONF_GATEWAY_TYPE_TYPE,
|
gatway_type: ConfGatewayType,
|
||||||
expected_step_id: str,
|
expected_step_id: str,
|
||||||
user_input: Dict[str, any],
|
user_input: Dict[str, any],
|
||||||
err_field,
|
err_field,
|
||||||
|
Reference in New Issue
Block a user