2019-04-03 15:40:03 +00:00
|
|
|
"""Support for TPLink HS100/HS110/HS200 smart switch."""
|
2021-05-22 12:47:26 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
import logging
|
2021-05-22 12:47:26 +00:00
|
|
|
from typing import Any
|
2016-11-11 06:43:16 +00:00
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
from kasa import SmartDevice
|
2019-05-31 05:51:04 +00:00
|
|
|
|
2021-07-29 18:02:47 +00:00
|
|
|
from homeassistant.components.switch import SwitchEntity
|
2021-05-22 12:47:26 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-11-13 23:50:37 +00:00
|
|
|
from homeassistant.const import ENTITY_CATEGORY_CONFIG
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.core import HomeAssistant
|
2021-05-22 12:47:26 +00:00
|
|
|
from homeassistant.helpers.entity_platform import AddEntitiesCallback
|
Add support for automatic discovery of TP-Link switches, bulbs and dimmers (#18091)
* {switch,light}.tplink: use deviceid as unique id, fetch name from the device during initialization
* raise PlatformNotReady when no device is available
* Use mac instead of deviceid
* remove name option as obsolete
* Add support for configuration flow / integration
Allows activating automatic discovery of supported devices from the configuration
* Fix linting, update requirements_all.txt
* start cleaning up tplink component based on feedback
* add device info, improve config handling
* Allow overriding detected devices via configuration file
* Update requirements.txt
* Remove debug logging
* make hound happy
* Avoid I/O during init and simplify the code, remove remains of leds_on
* Fix issues based on feedback, use consistent quotation marks for device info
* add async_setup_platform emiting a deprecation warning
* Avoid blocking the I/O, check for None on features
* handle some Martin's comments, schema-validation is still missing
* use async_create_task instead of async_add_job, let core validate the schema
* simplify configuration handling by storing the configuration data separately from initialized instances
* add default values to schema, make hound happy
* with defaults set by schema, simplify the checks. add async_unload_entry
* Use constant for data structure access
* REWORD add a short note about async_unload_entry
* handle feedback from Martin, config_data is checked against Noneness
* use pop to remove the domain on unload
* First steps to add tests for the new tplink component
* embed platforms under the component directory
* Fix tests by mocking the pyhs100 internals
* Fix linting
* Test against multiple instances of devices, tidy up
* (hopefully) final linting round
* Add pyHS100 to test requirements
* log always the warnings occured during an update to make them easy to see
* revert back the warning behavior (requirement for silver level in IQS)
* Unload only when an entry is being loaded and add tests for that
Thanks @MartinHjelmare for pointing this out!
* Fix linting
* Bump the upstream lib, fixes most prominently the HSV setting on bulbs
* Test unloading for all platforms, clear the data storage instead of popping it out, making it possible to reconfigure after removal without restarting hass first
* Use class variables instead of instance variables for bulb states, required for HS220
* Use new-style format string
* Fix indenting, uppercase the mock constant
* Run black on test_init, hopefully that will finally fix the weird formatting (pycharm, pylint and hound seems to have different opinions...)
2019-02-21 19:29:07 +00:00
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
from . import legacy_device_id
|
|
|
|
from .const import DOMAIN
|
|
|
|
from .coordinator import TPLinkDataUpdateCoordinator
|
|
|
|
from .entity import CoordinatedTPLinkEntity, async_refresh_after
|
|
|
|
|
|
|
|
_LOGGER = logging.getLogger(__name__)
|
2019-05-31 05:51:04 +00:00
|
|
|
|
|
|
|
|
2021-05-22 12:47:26 +00:00
|
|
|
async def async_setup_entry(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
config_entry: ConfigEntry,
|
|
|
|
async_add_entities: AddEntitiesCallback,
|
|
|
|
) -> None:
|
2019-05-31 05:51:04 +00:00
|
|
|
"""Set up switches."""
|
2021-09-27 19:11:55 +00:00
|
|
|
coordinator: TPLinkDataUpdateCoordinator = hass.data[DOMAIN][config_entry.entry_id]
|
|
|
|
device = coordinator.device
|
|
|
|
if not device.is_plug and not device.is_strip:
|
|
|
|
return
|
2021-11-13 23:50:37 +00:00
|
|
|
entities: list = []
|
2021-09-27 19:11:55 +00:00
|
|
|
if device.is_strip:
|
|
|
|
# Historically we only add the children if the device is a strip
|
|
|
|
_LOGGER.debug("Initializing strip with %s sockets", len(device.children))
|
|
|
|
for child in device.children:
|
|
|
|
entities.append(SmartPlugSwitch(child, coordinator))
|
|
|
|
else:
|
|
|
|
entities.append(SmartPlugSwitch(device, coordinator))
|
2020-10-28 08:51:53 +00:00
|
|
|
|
2021-11-13 23:50:37 +00:00
|
|
|
entities.append(SmartPlugLedSwitch(device, coordinator))
|
|
|
|
|
2021-07-29 18:02:47 +00:00
|
|
|
async_add_entities(entities)
|
2016-07-10 16:48:02 +00:00
|
|
|
|
|
|
|
|
2021-11-13 23:50:37 +00:00
|
|
|
class SmartPlugLedSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
|
|
|
"""Representation of switch for the LED of a TPLink Smart Plug."""
|
|
|
|
|
|
|
|
coordinator: TPLinkDataUpdateCoordinator
|
|
|
|
|
|
|
|
_attr_entity_category = ENTITY_CATEGORY_CONFIG
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self, device: SmartDevice, coordinator: TPLinkDataUpdateCoordinator
|
|
|
|
) -> None:
|
|
|
|
"""Initialize the LED switch."""
|
|
|
|
super().__init__(device, coordinator)
|
|
|
|
|
|
|
|
self._attr_name = f"{device.alias} LED"
|
|
|
|
self._attr_unique_id = f"{self.device.mac}_led"
|
|
|
|
|
|
|
|
@property
|
|
|
|
def icon(self) -> str:
|
|
|
|
"""Return the icon for the LED."""
|
|
|
|
return "mdi:led-on" if self.is_on else "mdi:led-off"
|
|
|
|
|
|
|
|
@async_refresh_after
|
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the LED switch on."""
|
|
|
|
await self.device.set_led(True)
|
|
|
|
|
|
|
|
@async_refresh_after
|
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
|
|
|
"""Turn the LED switch off."""
|
|
|
|
await self.device.set_led(False)
|
|
|
|
|
|
|
|
@property
|
|
|
|
def is_on(self) -> bool:
|
|
|
|
"""Return true if LED switch is on."""
|
|
|
|
return bool(self.device.led)
|
|
|
|
|
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
class SmartPlugSwitch(CoordinatedTPLinkEntity, SwitchEntity):
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Representation of a TPLink Smart Plug switch."""
|
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
coordinator: TPLinkDataUpdateCoordinator
|
|
|
|
|
2021-07-29 18:02:47 +00:00
|
|
|
def __init__(
|
2021-09-27 19:11:55 +00:00
|
|
|
self,
|
|
|
|
device: SmartDevice,
|
|
|
|
coordinator: TPLinkDataUpdateCoordinator,
|
2021-07-29 18:02:47 +00:00
|
|
|
) -> None:
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Initialize the switch."""
|
2021-09-27 19:11:55 +00:00
|
|
|
super().__init__(device, coordinator)
|
|
|
|
# For backwards compat with pyHS100
|
|
|
|
self._attr_unique_id = legacy_device_id(device)
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
@async_refresh_after
|
2021-07-29 18:02:47 +00:00
|
|
|
async def async_turn_on(self, **kwargs: Any) -> None:
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Turn the switch on."""
|
2021-09-27 19:11:55 +00:00
|
|
|
await self.device.turn_on()
|
2016-07-10 16:48:02 +00:00
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
@async_refresh_after
|
2021-07-29 18:02:47 +00:00
|
|
|
async def async_turn_off(self, **kwargs: Any) -> None:
|
2016-07-10 16:48:02 +00:00
|
|
|
"""Turn the switch off."""
|
2021-09-27 19:11:55 +00:00
|
|
|
await self.device.turn_off()
|