mirror of
https://github.com/home-assistant/core.git
synced 2025-07-17 10:17:09 +00:00
Address review comments for D-Link config flow (#85712)
* Address review comments for D-Link config flow * uno mas * uno mas * uno mas
This commit is contained in:
parent
05590f63c9
commit
b14c141fe3
@ -19,7 +19,7 @@ class SmartPlugData:
|
|||||||
"""Initialize the data object."""
|
"""Initialize the data object."""
|
||||||
self.smartplug = smartplug
|
self.smartplug = smartplug
|
||||||
self.state: str | None = None
|
self.state: str | None = None
|
||||||
self.temperature: float | None = None
|
self.temperature: str | None = None
|
||||||
self.current_consumption = None
|
self.current_consumption = None
|
||||||
self.total_consumption: str | None = None
|
self.total_consumption: str | None = None
|
||||||
self.available = False
|
self.available = False
|
||||||
|
@ -17,8 +17,8 @@ class DLinkEntity(Entity):
|
|||||||
|
|
||||||
def __init__(
|
def __init__(
|
||||||
self,
|
self,
|
||||||
data: SmartPlugData,
|
|
||||||
config_entry: ConfigEntry,
|
config_entry: ConfigEntry,
|
||||||
|
data: SmartPlugData,
|
||||||
description: EntityDescription,
|
description: EntityDescription,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Initialize a D-Link Power Plug entity."""
|
"""Initialize a D-Link Power Plug entity."""
|
||||||
@ -27,7 +27,7 @@ class DLinkEntity(Entity):
|
|||||||
if config_entry.source == SOURCE_IMPORT:
|
if config_entry.source == SOURCE_IMPORT:
|
||||||
self._attr_name = config_entry.title
|
self._attr_name = config_entry.title
|
||||||
else:
|
else:
|
||||||
self._attr_name = f"{config_entry.title} {description.key}"
|
self._attr_has_entity_name = True
|
||||||
self._attr_unique_id = f"{config_entry.entry_id}_{description.key}"
|
self._attr_unique_id = f"{config_entry.entry_id}_{description.key}"
|
||||||
self._attr_device_info = DeviceInfo(
|
self._attr_device_info = DeviceInfo(
|
||||||
identifiers={(DOMAIN, config_entry.entry_id)},
|
identifiers={(DOMAIN, config_entry.entry_id)},
|
||||||
|
@ -33,7 +33,6 @@ from .const import (
|
|||||||
DEFAULT_USERNAME,
|
DEFAULT_USERNAME,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
)
|
)
|
||||||
from .data import SmartPlugData
|
|
||||||
from .entity import DLinkEntity
|
from .entity import DLinkEntity
|
||||||
|
|
||||||
SCAN_INTERVAL = timedelta(minutes=2)
|
SCAN_INTERVAL = timedelta(minutes=2)
|
||||||
@ -65,7 +64,7 @@ def setup_platform(
|
|||||||
hass,
|
hass,
|
||||||
DOMAIN,
|
DOMAIN,
|
||||||
"deprecated_yaml",
|
"deprecated_yaml",
|
||||||
breaks_in_ha_version="2023.3.0",
|
breaks_in_ha_version="2023.4.0",
|
||||||
is_fixable=False,
|
is_fixable=False,
|
||||||
severity=IssueSeverity.WARNING,
|
severity=IssueSeverity.WARNING,
|
||||||
translation_key="deprecated_yaml",
|
translation_key="deprecated_yaml",
|
||||||
@ -84,14 +83,7 @@ async def async_setup_entry(
|
|||||||
) -> None:
|
) -> None:
|
||||||
"""Set up the D-Link Power Plug switch."""
|
"""Set up the D-Link Power Plug switch."""
|
||||||
async_add_entities(
|
async_add_entities(
|
||||||
[
|
[SmartPlugSwitch(entry, hass.data[DOMAIN][entry.entry_id], SWITCH_TYPE)],
|
||||||
SmartPlugSwitch(
|
|
||||||
hass,
|
|
||||||
entry,
|
|
||||||
hass.data[DOMAIN][entry.entry_id],
|
|
||||||
SWITCH_TYPE,
|
|
||||||
),
|
|
||||||
],
|
|
||||||
True,
|
True,
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -99,37 +91,20 @@ async def async_setup_entry(
|
|||||||
class SmartPlugSwitch(DLinkEntity, SwitchEntity):
|
class SmartPlugSwitch(DLinkEntity, SwitchEntity):
|
||||||
"""Representation of a D-Link Smart Plug switch."""
|
"""Representation of a D-Link Smart Plug switch."""
|
||||||
|
|
||||||
def __init__(
|
|
||||||
self,
|
|
||||||
hass: HomeAssistant,
|
|
||||||
entry: ConfigEntry,
|
|
||||||
data: SmartPlugData,
|
|
||||||
description: SwitchEntityDescription,
|
|
||||||
) -> None:
|
|
||||||
"""Initialize the switch."""
|
|
||||||
super().__init__(data, entry, description)
|
|
||||||
self.units = hass.config.units
|
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def extra_state_attributes(self) -> dict[str, Any]:
|
def extra_state_attributes(self) -> dict[str, Any]:
|
||||||
"""Return the state attributes of the device."""
|
"""Return the state attributes of the device."""
|
||||||
try:
|
attrs: dict[str, Any] = {}
|
||||||
ui_temp = self.units.temperature(
|
if self.data.temperature and self.data.temperature.isnumeric():
|
||||||
int(self.data.temperature or 0), UnitOfTemperature.CELSIUS
|
attrs[ATTR_TEMPERATURE] = self.hass.config.units.temperature(
|
||||||
|
int(self.data.temperature), UnitOfTemperature.CELSIUS
|
||||||
)
|
)
|
||||||
temperature = ui_temp
|
else:
|
||||||
except (ValueError, TypeError):
|
attrs[ATTR_TEMPERATURE] = None
|
||||||
temperature = None
|
if self.data.total_consumption and self.data.total_consumption.isnumeric():
|
||||||
|
attrs[ATTR_TOTAL_CONSUMPTION] = float(self.data.total_consumption)
|
||||||
try:
|
else:
|
||||||
total_consumption = float(self.data.total_consumption or "0")
|
attrs[ATTR_TOTAL_CONSUMPTION] = None
|
||||||
except (ValueError, TypeError):
|
|
||||||
total_consumption = None
|
|
||||||
|
|
||||||
attrs = {
|
|
||||||
ATTR_TOTAL_CONSUMPTION: total_consumption,
|
|
||||||
ATTR_TEMPERATURE: temperature,
|
|
||||||
}
|
|
||||||
|
|
||||||
return attrs
|
return attrs
|
||||||
|
|
||||||
|
@ -12,7 +12,7 @@ from tests.common import MockConfigEntry
|
|||||||
|
|
||||||
|
|
||||||
def _patch_setup_entry():
|
def _patch_setup_entry():
|
||||||
return patch("homeassistant.components.dlink.async_setup_entry")
|
return patch("homeassistant.components.dlink.async_setup_entry", return_value=True)
|
||||||
|
|
||||||
|
|
||||||
async def test_flow_user(hass: HomeAssistant, mocked_plug: MagicMock) -> None:
|
async def test_flow_user(hass: HomeAssistant, mocked_plug: MagicMock) -> None:
|
||||||
@ -55,7 +55,7 @@ async def test_flow_user_cannot_connect(
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"]["base"] == "cannot_connect"
|
assert result["errors"]["base"] == "cannot_connect"
|
||||||
|
|
||||||
with patch_config_flow(mocked_plug):
|
with patch_config_flow(mocked_plug), _patch_setup_entry():
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=CONF_DATA,
|
user_input=CONF_DATA,
|
||||||
@ -78,7 +78,7 @@ async def test_flow_user_unknown_error(
|
|||||||
assert result["step_id"] == "user"
|
assert result["step_id"] == "user"
|
||||||
assert result["errors"]["base"] == "unknown"
|
assert result["errors"]["base"] == "unknown"
|
||||||
|
|
||||||
with patch_config_flow(mocked_plug):
|
with patch_config_flow(mocked_plug), _patch_setup_entry():
|
||||||
result = await hass.config_entries.flow.async_configure(
|
result = await hass.config_entries.flow.async_configure(
|
||||||
result["flow_id"],
|
result["flow_id"],
|
||||||
user_input=CONF_DATA,
|
user_input=CONF_DATA,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user