Address review comments for D-Link config flow (#85712)

* Address review comments for D-Link config flow

* uno mas

* uno mas

* uno mas
pull/85661/head
Robert Hillis 2023-01-11 21:46:51 -05:00 committed by GitHub
parent 05590f63c9
commit b14c141fe3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 18 additions and 43 deletions

View File

@ -19,7 +19,7 @@ class SmartPlugData:
"""Initialize the data object."""
self.smartplug = smartplug
self.state: str | None = None
self.temperature: float | None = None
self.temperature: str | None = None
self.current_consumption = None
self.total_consumption: str | None = None
self.available = False

View File

@ -17,8 +17,8 @@ class DLinkEntity(Entity):
def __init__(
self,
data: SmartPlugData,
config_entry: ConfigEntry,
data: SmartPlugData,
description: EntityDescription,
) -> None:
"""Initialize a D-Link Power Plug entity."""
@ -27,7 +27,7 @@ class DLinkEntity(Entity):
if config_entry.source == SOURCE_IMPORT:
self._attr_name = config_entry.title
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_device_info = DeviceInfo(
identifiers={(DOMAIN, config_entry.entry_id)},

View File

@ -33,7 +33,6 @@ from .const import (
DEFAULT_USERNAME,
DOMAIN,
)
from .data import SmartPlugData
from .entity import DLinkEntity
SCAN_INTERVAL = timedelta(minutes=2)
@ -65,7 +64,7 @@ def setup_platform(
hass,
DOMAIN,
"deprecated_yaml",
breaks_in_ha_version="2023.3.0",
breaks_in_ha_version="2023.4.0",
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
@ -84,14 +83,7 @@ async def async_setup_entry(
) -> None:
"""Set up the D-Link Power Plug switch."""
async_add_entities(
[
SmartPlugSwitch(
hass,
entry,
hass.data[DOMAIN][entry.entry_id],
SWITCH_TYPE,
),
],
[SmartPlugSwitch(entry, hass.data[DOMAIN][entry.entry_id], SWITCH_TYPE)],
True,
)
@ -99,37 +91,20 @@ async def async_setup_entry(
class SmartPlugSwitch(DLinkEntity, SwitchEntity):
"""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
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the device."""
try:
ui_temp = self.units.temperature(
int(self.data.temperature or 0), UnitOfTemperature.CELSIUS
attrs: dict[str, Any] = {}
if self.data.temperature and self.data.temperature.isnumeric():
attrs[ATTR_TEMPERATURE] = self.hass.config.units.temperature(
int(self.data.temperature), UnitOfTemperature.CELSIUS
)
temperature = ui_temp
except (ValueError, TypeError):
temperature = None
try:
total_consumption = float(self.data.total_consumption or "0")
except (ValueError, TypeError):
total_consumption = None
attrs = {
ATTR_TOTAL_CONSUMPTION: total_consumption,
ATTR_TEMPERATURE: temperature,
}
else:
attrs[ATTR_TEMPERATURE] = None
if self.data.total_consumption and self.data.total_consumption.isnumeric():
attrs[ATTR_TOTAL_CONSUMPTION] = float(self.data.total_consumption)
else:
attrs[ATTR_TOTAL_CONSUMPTION] = None
return attrs

View File

@ -12,7 +12,7 @@ from tests.common import MockConfigEntry
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:
@ -55,7 +55,7 @@ async def test_flow_user_cannot_connect(
assert result["step_id"] == "user"
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["flow_id"],
user_input=CONF_DATA,
@ -78,7 +78,7 @@ async def test_flow_user_unknown_error(
assert result["step_id"] == "user"
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["flow_id"],
user_input=CONF_DATA,