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
|
|
|
"""Component to embed TP-Link smart home devices."""
|
2021-07-29 18:02:47 +00:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2021-09-28 14:58:25 +00:00
|
|
|
import asyncio
|
2021-10-07 15:52:24 +00:00
|
|
|
from datetime import timedelta
|
2021-08-01 21:58:55 +00:00
|
|
|
from typing import Any
|
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 kasa import SmartDevice, SmartDeviceException
|
|
|
|
from kasa.discover import Discover
|
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
|
|
|
|
|
|
|
from homeassistant import config_entries
|
2021-09-28 14:58:25 +00:00
|
|
|
from homeassistant.components import network
|
2022-01-05 14:00:49 +00:00
|
|
|
from homeassistant.config_entries import ConfigEntry
|
2021-10-07 15:52:24 +00:00
|
|
|
from homeassistant.const import (
|
|
|
|
CONF_HOST,
|
|
|
|
CONF_MAC,
|
|
|
|
CONF_NAME,
|
|
|
|
EVENT_HOMEASSISTANT_STARTED,
|
|
|
|
)
|
2021-09-27 19:11:55 +00:00
|
|
|
from homeassistant.core import HomeAssistant, callback
|
2022-01-05 14:00:49 +00:00
|
|
|
from homeassistant.exceptions import ConfigEntryNotReady
|
2021-12-21 10:24:32 +00:00
|
|
|
from homeassistant.helpers import device_registry as dr
|
2021-10-07 15:52:24 +00:00
|
|
|
from homeassistant.helpers.event import async_track_time_interval
|
2021-04-22 14:53:57 +00:00
|
|
|
from homeassistant.helpers.typing import ConfigType
|
2019-05-31 05:51:04 +00:00
|
|
|
|
2021-12-21 10:24:32 +00:00
|
|
|
from .const import DOMAIN, PLATFORMS
|
2021-09-27 19:11:55 +00:00
|
|
|
from .coordinator import TPLinkDataUpdateCoordinator
|
2019-07-31 19:25:30 +00:00
|
|
|
|
2021-10-07 15:52:24 +00:00
|
|
|
DISCOVERY_INTERVAL = timedelta(minutes=15)
|
|
|
|
|
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
|
|
|
@callback
|
|
|
|
def async_trigger_discovery(
|
|
|
|
hass: HomeAssistant,
|
|
|
|
discovered_devices: dict[str, SmartDevice],
|
|
|
|
) -> None:
|
|
|
|
"""Trigger config flows for discovered devices."""
|
|
|
|
for formatted_mac, device in discovered_devices.items():
|
|
|
|
hass.async_create_task(
|
|
|
|
hass.config_entries.flow.async_init(
|
|
|
|
DOMAIN,
|
2022-02-16 11:06:58 +00:00
|
|
|
context={"source": config_entries.SOURCE_INTEGRATION_DISCOVERY},
|
2021-09-27 19:11:55 +00:00
|
|
|
data={
|
|
|
|
CONF_NAME: device.alias,
|
|
|
|
CONF_HOST: device.host,
|
|
|
|
CONF_MAC: formatted_mac,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2021-09-28 14:58:25 +00:00
|
|
|
async def async_discover_devices(hass: HomeAssistant) -> dict[str, SmartDevice]:
|
|
|
|
"""Discover TPLink devices on configured network interfaces."""
|
|
|
|
broadcast_addresses = await network.async_get_ipv4_broadcast_addresses(hass)
|
|
|
|
tasks = [Discover.discover(target=str(address)) for address in broadcast_addresses]
|
|
|
|
discovered_devices: dict[str, SmartDevice] = {}
|
|
|
|
for device_list in await asyncio.gather(*tasks):
|
|
|
|
for device in device_list.values():
|
|
|
|
discovered_devices[dr.format_mac(device.mac)] = device
|
|
|
|
return discovered_devices
|
|
|
|
|
|
|
|
|
2021-05-22 12:47:26 +00:00
|
|
|
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool:
|
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
|
|
|
"""Set up the TP-Link component."""
|
|
|
|
hass.data[DOMAIN] = {}
|
|
|
|
|
2021-12-21 10:24:32 +00:00
|
|
|
if discovered_devices := await async_discover_devices(hass):
|
2021-09-27 19:11:55 +00:00
|
|
|
async_trigger_discovery(hass, discovered_devices)
|
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-10-07 15:52:24 +00:00
|
|
|
async def _async_discovery(*_: Any) -> None:
|
|
|
|
if discovered := await async_discover_devices(hass):
|
|
|
|
async_trigger_discovery(hass, discovered)
|
|
|
|
|
|
|
|
hass.bus.async_listen_once(EVENT_HOMEASSISTANT_STARTED, _async_discovery)
|
|
|
|
async_track_time_interval(hass, _async_discovery, DISCOVERY_INTERVAL)
|
|
|
|
|
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
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-05-27 13:56:20 +00:00
|
|
|
async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
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
|
|
|
"""Set up TPLink from a config entry."""
|
2021-09-27 19:11:55 +00:00
|
|
|
try:
|
|
|
|
device: SmartDevice = await Discover.discover_single(entry.data[CONF_HOST])
|
|
|
|
except SmartDeviceException as ex:
|
|
|
|
raise ConfigEntryNotReady from ex
|
2021-08-01 21:58:55 +00:00
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
hass.data[DOMAIN][entry.entry_id] = TPLinkDataUpdateCoordinator(hass, device)
|
2021-07-29 18:02:47 +00:00
|
|
|
hass.config_entries.async_setup_platforms(entry, PLATFORMS)
|
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
|
|
|
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
2021-05-22 12:47:26 +00:00
|
|
|
async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
|
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
|
|
|
"""Unload a config entry."""
|
2021-08-01 21:58:55 +00:00
|
|
|
hass_data: dict[str, Any] = hass.data[DOMAIN]
|
2021-12-21 10:24:32 +00:00
|
|
|
device: SmartDevice = hass_data[entry.entry_id].device
|
2021-09-27 19:11:55 +00:00
|
|
|
if unload_ok := await hass.config_entries.async_unload_platforms(entry, PLATFORMS):
|
|
|
|
hass_data.pop(entry.entry_id)
|
2022-03-22 06:20:40 +00:00
|
|
|
await device.protocol.close()
|
2021-04-27 20:19:57 +00:00
|
|
|
return unload_ok
|
2021-07-29 18:02:47 +00:00
|
|
|
|
|
|
|
|
2021-09-27 19:11:55 +00:00
|
|
|
def legacy_device_id(device: SmartDevice) -> str:
|
|
|
|
"""Convert the device id so it matches what was used in the original version."""
|
|
|
|
device_id: str = device.device_id
|
|
|
|
# Plugs are prefixed with the mac in python-kasa but not
|
|
|
|
# in pyHS100 so we need to strip off the mac
|
|
|
|
if "_" not in device_id:
|
|
|
|
return device_id
|
|
|
|
return device_id.split("_")[1]
|