core/homeassistant/components/dlink/switch.py

137 lines
3.7 KiB
Python
Raw Normal View History

2023-01-11 00:10:56 +00:00
"""Support for D-Link Power Plug Switches."""
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-08-20 22:25:11 +00:00
import voluptuous as vol
2023-01-11 00:10:56 +00:00
from homeassistant.components.switch import (
PLATFORM_SCHEMA,
SwitchEntity,
SwitchEntityDescription,
)
from homeassistant.config_entries import SOURCE_IMPORT, ConfigEntry
2018-10-26 13:45:57 +00:00
from homeassistant.const import (
2019-07-31 19:25:30 +00:00
ATTR_TEMPERATURE,
CONF_HOST,
CONF_NAME,
CONF_PASSWORD,
CONF_USERNAME,
UnitOfTemperature,
2019-07-31 19:25:30 +00:00
)
from homeassistant.core import HomeAssistant
2016-08-20 22:25:11 +00:00
import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.entity_platform import AddEntitiesCallback
2023-01-11 00:10:56 +00:00
from homeassistant.helpers.issue_registry import IssueSeverity, async_create_issue
from homeassistant.helpers.typing import ConfigType, DiscoveryInfoType
2018-10-26 13:45:57 +00:00
2023-01-11 00:10:56 +00:00
from .const import (
ATTR_TOTAL_CONSUMPTION,
CONF_USE_LEGACY_PROTOCOL,
DEFAULT_NAME,
DEFAULT_USERNAME,
DOMAIN,
)
from .entity import DLinkEntity
2018-10-26 13:45:57 +00:00
SCAN_INTERVAL = timedelta(minutes=2)
2016-08-20 22:25:11 +00:00
2019-07-31 19:25:30 +00:00
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
{
vol.Required(CONF_HOST): cv.string,
2023-01-11 00:10:56 +00:00
vol.Required(CONF_PASSWORD, default=""): cv.string,
2019-07-31 19:25:30 +00:00
vol.Required(CONF_USERNAME, default=DEFAULT_USERNAME): cv.string,
vol.Optional(CONF_NAME, default=DEFAULT_NAME): cv.string,
vol.Optional(CONF_USE_LEGACY_PROTOCOL, default=False): cv.boolean,
}
)
2016-08-20 22:25:11 +00:00
SWITCH_TYPE = SwitchEntityDescription(
key="switch",
name="Switch",
)
2023-01-11 00:10:56 +00:00
def setup_platform(
hass: HomeAssistant,
config: ConfigType,
add_entities: AddEntitiesCallback,
discovery_info: DiscoveryInfoType | None = None,
) -> None:
"""Set up a D-Link Smart Plug."""
2023-01-11 00:10:56 +00:00
async_create_issue(
hass,
DOMAIN,
"deprecated_yaml",
breaks_in_ha_version="2023.4.0",
2023-01-11 00:10:56 +00:00
is_fixable=False,
severity=IssueSeverity.WARNING,
translation_key="deprecated_yaml",
)
hass.async_create_task(
hass.config_entries.flow.async_init(
DOMAIN,
context={"source": SOURCE_IMPORT},
data=config,
)
)
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(
[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
@property
2023-01-11 00:10:56 +00:00
def extra_state_attributes(self) -> dict[str, Any]:
"""Return the state attributes of the device."""
try:
temperature = self.hass.config.units.temperature(
int(self.data.temperature), UnitOfTemperature.CELSIUS
)
except ValueError:
temperature = None
try:
total_consumption = float(self.data.total_consumption)
except ValueError:
total_consumption = None
attrs = {
ATTR_TOTAL_CONSUMPTION: total_consumption,
ATTR_TEMPERATURE: temperature,
}
return attrs
@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"
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"
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()
@property
def available(self) -> bool:
"""Return True if entity is available."""
return self.data.available