2023-01-11 00:10:56 +00:00
|
|
|
"""Support for D-Link Power Plug Switches."""
|
2024-03-08 13:15:26 +00:00
|
|
|
|
2022-01-03 15:31:24 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2018-10-26 13:45:57 +00:00
|
|
|
from datetime import timedelta
|
2022-08-19 14:10:45 +00:00
|
|
|
from typing import Any
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2023-05-06 22:01:58 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity, SwitchEntityDescription
|
|
|
|
from homeassistant.config_entries import ConfigEntry
|
|
|
|
from homeassistant.const import ATTR_TEMPERATURE, UnitOfTemperature
|
2022-01-03 15:31:24 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
2018-10-26 13:45:57 +00:00
|
|
|
|
2023-05-06 22:01:58 +00:00
|
|
|
from .const import ATTR_TOTAL_CONSUMPTION, DOMAIN
|
2023-01-11 00:10:56 +00:00
|
|
|
from .entity import DLinkEntity
|
2016-09-06 17:52:22 +00:00
|
|
|
|
2018-10-26 13:45:57 +00:00
|
|
|
SCAN_INTERVAL = timedelta(minutes=2)
|
2016-08-20 22:25:11 +00:00
|
|
|
|
2023-01-30 09:56:56 +00:00
|
|
|
SWITCH_TYPE = SwitchEntityDescription(
|
|
|
|
key="switch",
|
|
|
|
)
|
2023-01-11 00:10:56 +00:00
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2023-01-11 00:10:56 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant, entry: ConfigEntry, async_add_entities: AddEntitiesCallback
|
|
|
|
) -> None:
|
|
|
|
"""Set up the D-Link Power Plug switch."""
|
|
|
|
async_add_entities(
|
2023-01-12 02:46:51 +00:00
|
|
|
[SmartPlugSwitch(entry, hass.data[DOMAIN][entry.entry_id], SWITCH_TYPE)],
|
2023-01-11 00:10:56 +00:00
|
|
|
True,
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class SmartPlugSwitch(DLinkEntity, SwitchEntity):
|
2018-10-26 13:45:57 +00:00
|
|
|
"""Representation of a D-Link Smart Plug switch."""
|
2016-03-08 12:35:39 +00:00
|
|
|
|
2023-07-18 16:51:02 +00:00
|
|
|
_attr_name = None
|
|
|
|
|
2016-09-06 17:52:22 +00:00
|
|
|
@property
|
2023-01-11 00:10:56 +00:00
|
|
|
def extra_state_attributes(self) -> dict[str, Any]:
|
2016-09-06 17:52:22 +00:00
|
|
|
"""Return the state attributes of the device."""
|
2023-01-29 03:03:58 +00:00
|
|
|
try:
|
|
|
|
temperature = self.hass.config.units.temperature(
|
2023-01-12 02:46:51 +00:00
|
|
|
int(self.data.temperature), UnitOfTemperature.CELSIUS
|
2022-12-20 16:59:20 +00:00
|
|
|
)
|
2023-01-29 03:03:58 +00:00
|
|
|
except ValueError:
|
|
|
|
temperature = None
|
|
|
|
|
|
|
|
try:
|
|
|
|
total_consumption = float(self.data.total_consumption)
|
|
|
|
except ValueError:
|
|
|
|
total_consumption = None
|
|
|
|
|
2024-04-06 09:07:37 +00:00
|
|
|
return {
|
2023-01-29 03:03:58 +00:00
|
|
|
ATTR_TOTAL_CONSUMPTION: total_consumption,
|
|
|
|
ATTR_TEMPERATURE: temperature,
|
|
|
|
}
|
2016-09-06 17:52:22 +00:00
|
|
|
|
2016-02-20 20:17:48 +00:00
|
|
|
@property
|
2023-01-11 00:10:56 +00:00
|
|
|
def is_on(self) -> bool:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Return true if switch is on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
return self.data.state == "ON"
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
def turn_on(self, **kwargs: Any) -> None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch on."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data.smartplug.state = "ON"
|
2016-02-20 20:17:48 +00:00
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
def turn_off(self, **kwargs: Any) -> None:
|
2016-03-08 12:35:39 +00:00
|
|
|
"""Turn the switch off."""
|
2019-07-31 19:25:30 +00:00
|
|
|
self.data.smartplug.state = "OFF"
|
2016-11-17 02:55:58 +00:00
|
|
|
|
2022-08-19 14:10:45 +00:00
|
|
|
def update(self) -> None:
|
2016-11-17 02:55:58 +00:00
|
|
|
"""Get the latest data from the smart plug and updates the states."""
|
|
|
|
self.data.update()
|
|
|
|
|
2018-09-04 08:50:12 +00:00
|
|
|
@property
|
|
|
|
def available(self) -> bool:
|
|
|
|
"""Return True if entity is available."""
|
|
|
|
return self.data.available
|