mirror of
https://github.com/home-assistant/core.git
synced 2025-07-26 22:57:17 +00:00
Improve type hints in mqtt (#74247)
This commit is contained in:
parent
9a9fea4423
commit
8ef87205f9
@ -10,6 +10,7 @@ import voluptuous as vol
|
|||||||
from homeassistant.components import binary_sensor
|
from homeassistant.components import binary_sensor
|
||||||
from homeassistant.components.binary_sensor import (
|
from homeassistant.components.binary_sensor import (
|
||||||
DEVICE_CLASSES_SCHEMA,
|
DEVICE_CLASSES_SCHEMA,
|
||||||
|
BinarySensorDeviceClass,
|
||||||
BinarySensorEntity,
|
BinarySensorEntity,
|
||||||
)
|
)
|
||||||
from homeassistant.config_entries import ConfigEntry
|
from homeassistant.config_entries import ConfigEntry
|
||||||
@ -291,12 +292,12 @@ class MqttBinarySensor(MqttEntity, BinarySensorEntity, RestoreEntity):
|
|||||||
return self._state
|
return self._state
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def device_class(self):
|
def device_class(self) -> BinarySensorDeviceClass | None:
|
||||||
"""Return the class of this sensor."""
|
"""Return the class of this sensor."""
|
||||||
return self._config.get(CONF_DEVICE_CLASS)
|
return self._config.get(CONF_DEVICE_CLASS)
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def force_update(self):
|
def force_update(self) -> bool:
|
||||||
"""Force update."""
|
"""Force update."""
|
||||||
return self._config[CONF_FORCE_UPDATE]
|
return self._config[CONF_FORCE_UPDATE]
|
||||||
|
|
||||||
|
@ -3,6 +3,7 @@ from __future__ import annotations
|
|||||||
|
|
||||||
from collections import OrderedDict
|
from collections import OrderedDict
|
||||||
import queue
|
import queue
|
||||||
|
from typing import Any
|
||||||
|
|
||||||
import voluptuous as vol
|
import voluptuous as vol
|
||||||
|
|
||||||
@ -55,14 +56,18 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
"""Get the options flow for this handler."""
|
"""Get the options flow for this handler."""
|
||||||
return MQTTOptionsFlowHandler(config_entry)
|
return MQTTOptionsFlowHandler(config_entry)
|
||||||
|
|
||||||
async def async_step_user(self, user_input=None):
|
async def async_step_user(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Handle a flow initialized by the user."""
|
"""Handle a flow initialized by the user."""
|
||||||
if self._async_current_entries():
|
if self._async_current_entries():
|
||||||
return self.async_abort(reason="single_instance_allowed")
|
return self.async_abort(reason="single_instance_allowed")
|
||||||
|
|
||||||
return await self.async_step_broker()
|
return await self.async_step_broker()
|
||||||
|
|
||||||
async def async_step_broker(self, user_input=None):
|
async def async_step_broker(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Confirm the setup."""
|
"""Confirm the setup."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
|
||||||
@ -102,9 +107,12 @@ class FlowHandler(config_entries.ConfigFlow, domain=DOMAIN):
|
|||||||
|
|
||||||
return await self.async_step_hassio_confirm()
|
return await self.async_step_hassio_confirm()
|
||||||
|
|
||||||
async def async_step_hassio_confirm(self, user_input=None):
|
async def async_step_hassio_confirm(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Confirm a Hass.io discovery."""
|
"""Confirm a Hass.io discovery."""
|
||||||
errors = {}
|
errors = {}
|
||||||
|
assert self._hassio_discovery
|
||||||
|
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
data = self._hassio_discovery
|
data = self._hassio_discovery
|
||||||
@ -148,11 +156,13 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
self.broker_config: dict[str, str | int] = {}
|
self.broker_config: dict[str, str | int] = {}
|
||||||
self.options = dict(config_entry.options)
|
self.options = dict(config_entry.options)
|
||||||
|
|
||||||
async def async_step_init(self, user_input=None):
|
async def async_step_init(self, user_input: None = None) -> FlowResult:
|
||||||
"""Manage the MQTT options."""
|
"""Manage the MQTT options."""
|
||||||
return await self.async_step_broker()
|
return await self.async_step_broker()
|
||||||
|
|
||||||
async def async_step_broker(self, user_input=None):
|
async def async_step_broker(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage the MQTT broker configuration."""
|
"""Manage the MQTT broker configuration."""
|
||||||
errors = {}
|
errors = {}
|
||||||
current_config = self.config_entry.data
|
current_config = self.config_entry.data
|
||||||
@ -200,12 +210,14 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
last_step=False,
|
last_step=False,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def async_step_options(self, user_input=None):
|
async def async_step_options(
|
||||||
|
self, user_input: dict[str, Any] | None = None
|
||||||
|
) -> FlowResult:
|
||||||
"""Manage the MQTT options."""
|
"""Manage the MQTT options."""
|
||||||
errors = {}
|
errors = {}
|
||||||
current_config = self.config_entry.data
|
current_config = self.config_entry.data
|
||||||
yaml_config = self.hass.data.get(DATA_MQTT_CONFIG, {})
|
yaml_config = self.hass.data.get(DATA_MQTT_CONFIG, {})
|
||||||
options_config = {}
|
options_config: dict[str, Any] = {}
|
||||||
if user_input is not None:
|
if user_input is not None:
|
||||||
bad_birth = False
|
bad_birth = False
|
||||||
bad_will = False
|
bad_will = False
|
||||||
@ -251,7 +263,7 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
self.hass.config_entries.async_update_entry(
|
self.hass.config_entries.async_update_entry(
|
||||||
self.config_entry, data=updated_config
|
self.config_entry, data=updated_config
|
||||||
)
|
)
|
||||||
return self.async_create_entry(title="", data=None)
|
return self.async_create_entry(title="", data={})
|
||||||
|
|
||||||
birth = {
|
birth = {
|
||||||
**DEFAULT_BIRTH,
|
**DEFAULT_BIRTH,
|
||||||
@ -269,7 +281,7 @@ class MQTTOptionsFlowHandler(config_entries.OptionsFlow):
|
|||||||
CONF_DISCOVERY, yaml_config.get(CONF_DISCOVERY, DEFAULT_DISCOVERY)
|
CONF_DISCOVERY, yaml_config.get(CONF_DISCOVERY, DEFAULT_DISCOVERY)
|
||||||
)
|
)
|
||||||
|
|
||||||
fields = OrderedDict()
|
fields: OrderedDict[vol.Marker, Any] = OrderedDict()
|
||||||
fields[vol.Optional(CONF_DISCOVERY, default=discovery)] = bool
|
fields[vol.Optional(CONF_DISCOVERY, default=discovery)] = bool
|
||||||
|
|
||||||
# Birth message is disabled if CONF_BIRTH_MESSAGE = {}
|
# Birth message is disabled if CONF_BIRTH_MESSAGE = {}
|
||||||
|
@ -312,7 +312,7 @@ async def test_option_flow(hass, mqtt_mock_entry_no_yaml_config, mock_try_connec
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result["data"] is None
|
assert result["data"] == {}
|
||||||
assert config_entry.data == {
|
assert config_entry.data == {
|
||||||
mqtt.CONF_BROKER: "another-broker",
|
mqtt.CONF_BROKER: "another-broker",
|
||||||
mqtt.CONF_PORT: 2345,
|
mqtt.CONF_PORT: 2345,
|
||||||
@ -387,7 +387,7 @@ async def test_disable_birth_will(
|
|||||||
},
|
},
|
||||||
)
|
)
|
||||||
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
assert result["type"] == data_entry_flow.RESULT_TYPE_CREATE_ENTRY
|
||||||
assert result["data"] is None
|
assert result["data"] == {}
|
||||||
assert config_entry.data == {
|
assert config_entry.data == {
|
||||||
mqtt.CONF_BROKER: "another-broker",
|
mqtt.CONF_BROKER: "another-broker",
|
||||||
mqtt.CONF_PORT: 2345,
|
mqtt.CONF_PORT: 2345,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user