1 """Support for customised Kankun SP3 Wifi switch."""
3 from __future__
import annotations
9 import voluptuous
as vol
12 PLATFORM_SCHEMA
as SWITCH_PLATFORM_SCHEMA,
29 _LOGGER = logging.getLogger(__name__)
32 DEFAULT_PATH =
"/cgi-bin/json.cgi"
34 SWITCH_SCHEMA = vol.Schema(
36 vol.Required(CONF_HOST): cv.string,
37 vol.Optional(CONF_NAME): cv.string,
38 vol.Optional(CONF_PORT, default=DEFAULT_PORT): cv.port,
39 vol.Optional(CONF_PATH, default=DEFAULT_PATH): cv.string,
40 vol.Optional(CONF_USERNAME): cv.string,
41 vol.Optional(CONF_PASSWORD): cv.string,
45 PLATFORM_SCHEMA = SWITCH_PLATFORM_SCHEMA.extend(
46 {vol.Required(CONF_SWITCHES): cv.schema_with_slug_keys(SWITCH_SCHEMA)}
53 add_entities_callback: AddEntitiesCallback,
54 discovery_info: DiscoveryInfoType |
None =
None,
56 """Set up Kankun Wifi switches."""
57 switches = config.get(
"switches", {})
60 for dev_name, properties
in switches.items():
64 properties.get(CONF_NAME, dev_name),
65 properties.get(CONF_HOST),
66 properties.get(CONF_PORT, DEFAULT_PORT),
67 properties.get(CONF_PATH, DEFAULT_PATH),
68 properties.get(CONF_USERNAME),
69 properties.get(CONF_PASSWORD),
73 add_entities_callback(devices)
77 """Representation of a Kankun Wifi switch."""
79 def __init__(self, hass, name, host, port, path, user, passwd):
80 """Initialize the device."""
84 self.
_url_url = f
"http://{host}:{port}{path}"
86 self.
_auth_auth = (user, passwd)
88 self.
_auth_auth =
None
91 """Switch on or off."""
92 _LOGGER.debug(
"Switching to state: %s", newstate)
96 f
"{self._url}?set={newstate}", auth=self.
_auth_auth, timeout=5
98 return req.json()[
"ok"]
99 except requests.RequestException:
100 _LOGGER.error(
"Switching failed")
103 """Query switch state."""
104 _LOGGER.debug(
"Querying state from: %s", self.
_url_url)
107 req = requests.get(f
"{self._url}?get=state", auth=self.
_auth_auth, timeout=5)
108 return req.json()[
"state"] ==
"on"
109 except requests.RequestException:
110 _LOGGER.error(
"State query failed")
114 """Return the name of the switch."""
115 return self.
_name_name
119 """Return true if device is on."""
123 """Update device state."""
127 """Turn the device on."""
132 """Turn the device off."""
def _switch(self, newstate)
def __init__(self, hass, name, host, port, path, user, passwd)
None turn_on(self, **Any kwargs)
None turn_off(self, **Any kwargs)
None setup_platform(HomeAssistant hass, ConfigType config, AddEntitiesCallback add_entities_callback, DiscoveryInfoType|None discovery_info=None)