Skip to content

Commit

Permalink
Merge pull request #18 from albrandwood/HS220
Browse files Browse the repository at this point in the history
Added HS220 support, with limited success. Turn on and off works, set…
  • Loading branch information
jimboca committed Apr 29, 2021
2 parents 2fa8757 + ae330e2 commit fa52263
Show file tree
Hide file tree
Showing 7 changed files with 610 additions and 384 deletions.
7 changes: 7 additions & 0 deletions nodes/Controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from kasa import Discover
from nodes import SmartStripNode
from nodes import SmartPlugNode
from nodes import SmartDimmerNode
from nodes import SmartBulbNode
from nodes import SmartLightStripNode
LOGGER = polyinterface.LOGGER
Expand Down Expand Up @@ -194,6 +195,9 @@ def add_node(self, dev=None, cfg=None):
elif dev.is_light_strip:
type = 'SmartLightStrip'
name = dev.alias
elif dev.is_dimmable:
type = 'SmartDimmer'
name = dev.alias
else:
LOGGER.error(f"What is this? {dev}")
return False
Expand All @@ -207,10 +211,13 @@ def add_node(self, dev=None, cfg=None):
# Add Based on device type. SmartStrip is a unique type, all others
# are handled by SmartDevice
#
# LOGGER.error(f"alb:controller.py:{cfg['type']}")
if cfg['type'] == 'SmartStrip':
node = self.addNode(SmartStripNode(self, cfg['address'], cfg['name'], dev=dev, cfg=cfg))
elif cfg['type'] == 'SmartPlug':
node = self.addNode(SmartPlugNode(self, cfg['address'], cfg['name'], dev=dev, cfg=cfg))
elif cfg['type'] == 'SmartDimmer':
node = self.addNode(SmartDimmerNode(self, cfg['address'], cfg['name'], dev=dev, cfg=cfg))
elif cfg['type'] == 'SmartBulb':
node = self.addNode(SmartBulbNode(self, cfg['address'], cfg['name'], dev=dev, cfg=cfg))
elif cfg['type'] == 'SmartLightStrip':
Expand Down
111 changes: 111 additions & 0 deletions nodes/SmartDimmerNode.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
#
# TP Link Kasa Smart Dimmer Node
#
# This code is used for dimmers (HS220)
#
import polyinterface,asyncio
from kasa import SmartPlug,SmartDimmer,SmartDeviceException
from converters import bri2st,st2bri

from nodes import SmartDeviceNode

LOGGER = polyinterface.LOGGER

class SmartDimmerNode(SmartDeviceNode):

def __init__(self, controller, address, name, cfg=None, dev=None):
# All plugs have these.
self.debug_level = 0
self.name = name
# All devices have these.
self.drivers = [
{'driver': 'ST', 'value': 0, 'uom': 51},
{'driver': 'GV0', 'value': 0, 'uom': 2}, #connection state
]
if dev is not None:
# Figure out the id based in the device info
self.id = 'SmartDimmer_'
if dev.is_dimmable:
self.id += 'D'
self.drivers.append({'driver': 'GV5', 'value': 0, 'uom': 100})
else:
self.id += 'N'
if dev.has_emeter:
self.id += 'E'
else:
self.id += 'N'
cfg['emeter'] = dev.has_emeter
if cfg['emeter']:
self.drivers.append({'driver': 'CC', 'value': 0, 'uom': 56})
self.drivers.append({'driver': 'CV', 'value': 0, 'uom': 56})
self.drivers.append({'driver': 'CPW', 'value': 0, 'uom': 73})
self.drivers.append({'driver': 'TPW', 'value': 0, 'uom': 73})
super().__init__(controller, controller.address, address, name, dev, cfg)

def start(self):
super().start()
self.set_energy()

def longPoll(self):
super().longPoll()
self.set_energy()

def set_bri(self,val):
LOGGER.debug(f'{self.pfx} connected={self.connected} val={val}')
if self.is_connected():
self.brightness = int(val)
LOGGER.debug(f'{val}')
self.setDriver('GV5',self.brightness)
# This won't actually change unless the device is on
asyncio.run(self.dev.set_brightness(int(bri2st(self.brightness))))
asyncio.run(self.dev.update())
self.setDriver('ST',self.dev.brightness)

def brt(self):
LOGGER.debug(f'{self.pfx} connected={self.connected}')
asyncio.run(self.dev.update())
self.brightness = st2bri(self.dev.brightness)
if self.is_connected() and self.brightness <= 100:
self.set_bri(self.brightness + 7)

def dim(self):
LOGGER.debug('{self.pfx} connected={self.connected}')
asyncio.run(self.dev.update())
self.brightness = st2bri(self.dev.brightness)
if self.is_connected() and self.brightness > 0:
self.set_bri(self.brightness - 7)

def newdev(self):
return SmartDimmer(self.host)

def cmd_set_on(self,command):
super().cmd_set_on(command)

def cmd_set_off(self,command):
super().cmd_set_off(command)

def cmd_set_bri(self,command):
val = int(command.get('value'))
LOGGER.info(f'{self.pfx} val={val}')
asyncio.run(self.dev.turn_on())
self.set_bri(val)

def cmd_brt(self,command):
if not self.dev.is_dimmable:
LOGGER.error('{self.pfx} Not supported on this device')
asyncio.run(self.dev.turn_on())
self.brt()

def cmd_dim(self,command):
if not self.dev.is_dimmable:
LOGGER.error('{self.pfx} Not supported on this device')
self.dim()

commands = {
'DON': cmd_set_on,
'DOF': cmd_set_off,
'SET_BRI': cmd_set_bri,
'BRT': cmd_brt,
'DIM': cmd_dim,
}

7 changes: 4 additions & 3 deletions nodes/SmartPlugNode.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#
# TP Link Kasa Smart Bulb Node
# TP Link Kasa Smart Plug Node
#
# This code is used for bulbs
# This code is used for plugs
#
import polyinterface
from kasa import SmartPlug,SmartDeviceException
Expand All @@ -19,7 +19,7 @@ def __init__(self, controller, address, name, cfg=None, dev=None):
# All devices have these.
self.drivers = [
{'driver': 'ST', 'value': 0, 'uom': 78},
{'driver': 'GV0', 'value': 0, 'uom': 2} #connection state
{'driver': 'GV0', 'value': 0, 'uom': 2}, #connection state
]
if dev is not None:
# Figure out the id based in the device info
Expand Down Expand Up @@ -61,3 +61,4 @@ def cmd_set_off(self,command):
'DON': cmd_set_on,
'DOF': cmd_set_off,
}

1 change: 1 addition & 0 deletions nodes/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from .SmartStripPlugNode import SmartStripPlugNode
from .SmartStripNode import SmartStripNode
from .SmartPlugNode import SmartPlugNode
from .SmartDimmerNode import SmartDimmerNode
from .SmartBulbNode import SmartBulbNode
from .SmartLightStripNode import SmartLightStripNode
from .Controller import Controller
33 changes: 24 additions & 9 deletions profile/nls/en_us.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,36 +36,52 @@ ND-SmartStripPlug-ICON = GenericCtl
# SmartPlugs
ND-SmartPlug_NN-NAME = SmartPlug
ND-SmartPlug_NE-NAME = SmartPlug Energy
ND-SmartPlug_DN-NAME = SmartPlug Dmmable
ND-SmartPlug_DN-NAME = SmartPlug Dimmable
ND-SmartPlug_DE-NAME = SmartPlug Dimmable Energy
ND-SmartPlug_NN-ICON = GenericCtl
ND-SmartPlug_NE-ICON = GenericCtl
ND-SmartPlug_DN-ICON = GenericCtl
ND-SmartPlug_DE-ICON = GenericCtl

ST-spd-GV5-NAME = Brightness
ST-spde-GV5-NAME = Brightness
ST-spde-CC-NAME = Current
ST-spde-CV-NAME = Voltage
ST-spde-CPW-NAME = Current Power Used
ST-spde-TPW-NAME = Total Power Used
ST-spe-CC-NAME = Current
ST-spe-CV-NAME = Voltage
ST-spe-CPW-NAME = Current Power Used
ST-spe-TPW-NAME = Total Power Used

# SmartDimmer's
ND-SmartDimmer_DN-NAME = SmartDimmer
ND-SmartDimmer_DE-NAME = SmartDimmer Energy
ND-SmartDimmer_DN-ICON = GenericCtl
ND-SmartDimmer_DE-ICON = GenericCtl
ST-sd-GV5-NAME = Brightness
ST-sde-GV5-NAME = Brightness
ST-sde-CC-NAME = Current
ST-sde-CV-NAME = Voltage
ST-sde-CPW-NAME = Current Power Used
ST-sde-TPW-NAME = Total Power Used

# SmartBulb's
ND-SmartBulb_NNNN-NAME = SmartBulb
ND-SmartBulb_DNNN-NAME = SmartBulb Dimmable
ND-SmartBulb_DTNN-NAME = SmartBulb Dimmable Color Temperature
ND-SmartBulb_DTNE-NAME = SmartBulb Dimmable Color Temperature Energy
ND-SmartBulb_DNNE-NAME = SmartBulb Dimmable Energy
ND-SmartBulb_DTCE-NAME = SmartBulb Color Energy
ND-SmartBulb_DTCN-NAME = SmartBulb Color

ND-SmartBulb_NNNN-ICON = Lamp
ND-SmartBulb_DNNN-ICON = Lamp
ND-SmartBulb_DTNN-ICON = Lamp
ND-SmartBulb_DTNE-ICON = Lamp
ND-SmartBulb_DNNE-ICON = Lamp
ND-SmartBulb_DTCE-ICON = Lamp

# SmartLightStrip
ND-SmartLightStrip_DTCE-NAME = SmartBulb Color Energy
ND-SmartLightStrip_DTCE-ICON = Lamp
ND-SmartBulb_DTCN-ICON = Lamp

ST-sb-GV2-NAME = Color Temperature
ST-sb-GV3-NAME = Hue
Expand All @@ -74,10 +90,9 @@ ST-sb-GV5-NAME = Brightness
ST-sb-RR-NAME = Transition time
ST-sb-CLITEMP-NAME = Color Temperature

ST-spe-CC-NAME = Current
ST-spe-CV-NAME = Voltage
ST-spe-CPW-NAME = Current Power Used
ST-spe-TPW-NAME = Total Power Used
# SmartLightStrip
ND-SmartLightStrip_DTCE-NAME = SmartBulb Color Energy
ND-SmartLightStrip_DTCE-ICON = Lamp

CMD-BRI-NAME = Brightness

Expand Down
Loading

0 comments on commit fa52263

Please sign in to comment.