diff --git a/homeassistant/components/dlink/data.py b/homeassistant/components/dlink/data.py index 08a5946d9ec..c9e3b20a0bf 100644 --- a/homeassistant/components/dlink/data.py +++ b/homeassistant/components/dlink/data.py @@ -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 diff --git a/homeassistant/components/dlink/entity.py b/homeassistant/components/dlink/entity.py index 327bbabd90b..33302f7fffa 100644 --- a/homeassistant/components/dlink/entity.py +++ b/homeassistant/components/dlink/entity.py @@ -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)}, diff --git a/homeassistant/components/dlink/switch.py b/homeassistant/components/dlink/switch.py index 4eac8862976..9827c1c13a1 100644 --- a/homeassistant/components/dlink/switch.py +++ b/homeassistant/components/dlink/switch.py @@ -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 diff --git a/tests/components/dlink/test_config_flow.py b/tests/components/dlink/test_config_flow.py index fde57d336f1..dc4064211ca 100644 --- a/tests/components/dlink/test_config_flow.py +++ b/tests/components/dlink/test_config_flow.py @@ -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,