python-kasa icon indicating copy to clipboard operation
python-kasa copied to clipboard

KS220M(US) support

Open mrbetta opened this issue 4 years ago • 10 comments

It'd be great if Home Assistant could be used to manage the new KS220M Motion Detection Dimmer. I set up a man in the middle proxy and captured the following JSON commands being sent from the KASA app to the switch and changed various motion sensor settings. If someone better with Python wanted to integrate these, that'd be great.

Get the current PIR config: Sent: {"smartlife.iot.PIR":{"get_config":{}}} Received: {"smartlife.iot.PIR":{"get_config":{"enable":0,"version":"1.0","trigger_index":2,"cold_time":60000,"min_adc":0,"max_adc":4095,"array":[80,50,20,0],"err_code":0}}}

Disable the PIR: {"smartlife.iot.PIR":{"set_enable":{"enable":0}}} Enable the PIR: {"smartlife.iot.PIR":{"set_enable":{"enable":1}}} Set the PIR to far range detection: {"smartlife.iot.PIR":{"set_trigger_sens":{"index":0}}} Mid Range: {"smartlife.iot.PIR":{"set_trigger_sens":{"index":1}}} Near Range: {"smartlife.iot.PIR":{"set_trigger_sens":{"index":2}}} "Custom" range of 20 ft: {"smartlife.iot.PIR":{"set_trigger_sens":{"index":3,"value":61}}} (Uses decimeters?)

I'll add more objects as I find them.

mrbetta avatar Dec 08 '21 06:12 mrbetta

An integrated light sensor is used to disable the motion sensor if it's not needed: Disable sensor: {"smartlife.iot.LAS":{"set_enable":{"enable":0}}} Enable sensor: {"smartlife.iot.LAS":{"set_enable":{"enable":1}}} Get current config: Sent: {"smartlife.iot.LAS":{"get_config":{}}} Response: {"smartlife.iot.LAS":{"get_config":{"devs":[{"hw_id":0,"enable":0,"dark_index":1,"min_adc":0,"max_adc":2450,"level_array":[{"name":"cloudy","adc":490,"value":20},{"name":"overcast","adc":294,"value":12},{"name":"dawn","adc":222,"value":9},{"name":"twilight","adc":222,"value":9},{"name":"total darkness","adc":111,"value":4},{"name":"custom","adc":2400,"value":97}]}],"ver":"1.0","err_code":0}}} Get current light level in darkness: Sent: {"smartlife.iot.LAS":{"get_current_brt":{}}} Response: {"smartlife.iot.LAS":{"get_current_brt":{"value":0,"err_code":0}}} Current light level with surrounding lights on: Response: {"smartlife.iot.LAS":{"get_current_brt":{"value":26,"err_code":0}}} Set trigger light level to "Cloudy" preset: {"smartlife.iot.LAS":{"set_brt_level":{"index":0,"value":20}}}

mrbetta avatar Dec 08 '21 06:12 mrbetta

It also has a feature to fade the lights on or off over time. Here's what that looks like:

Get current settings with it set to "Instant On" and "Instant Off": Sent: {"smartlife.iot.dimmer":{"get_dimmer_parameters":{}}} Received: {"smartlife.iot.dimmer":{"get_dimmer_parameters":{"fadeOnTime":0,"fadeOffTime":0,"gentleOnTime":3000,"gentleOffTime":10000,"rampRate":30,"minThreshold":1,"bulb_type":1,"err_code":0}}} Fade on over 5 seconds: {"smartlife.iot.dimmer":{"set_fade_on_time":{"fadeTime":5000}}} Fade off over 5 seconds: {""smartlife.iot.dimmer":{"set_fade_off_time":{"fadeTime":5000}}} Response of the 'get' after setting fade on time: {"smartlife.iot.dimmer":{"get_dimmer_parameters":{"fadeOnTime":5000,"fadeOffTime":0,"gentleOnTime":3000,"gentleOffTime":10000,"rampRate":30,"minThreshold":1,"bulb_type":1,"err_code":0}}}

mrbetta avatar Dec 08 '21 07:12 mrbetta

I'll make an attempt to add these to the code, but if someone wants to beat me to it I won't mind.

mrbetta avatar Dec 08 '21 07:12 mrbetta

I added the LAS and PIR sensors to the devtools script and generated the following dump: KS220M(US)_1.0_1.0.4.txt

mrbetta avatar Dec 08 '21 07:12 mrbetta

Based on the sysinfo, it should be detected as a SmartDimmer already, so I suppose this would be an extension to that class which implements the LAS & PIR module handling?

Could you try and see if the controls are similar to the existing dimmer (using the PR that I just linked, kasa --type dimmer will force the tool to use the dimmer implementation)?

Also, feel free to create a PR to add the modified devtools script as well as the fixture file to git. That can be done separately to any implementation PRs.

rytilahti avatar Dec 08 '21 14:12 rytilahti

You're right. It does turn on and off and the dimmer works. This is for the motion and light sensors. Is there any interest in figuring out the scheduler or 'away mode'?

mrbetta avatar Dec 09 '21 02:12 mrbetta

Okay, great! So the support for these new controls should be added to SmartDimmer class.

For scheduler, see #265. There is another development branch where some work has been done on that, but it's far from complete. The away mode scheduler is called anti_theft in contrast to regular scheduling.

rytilahti avatar Dec 09 '21 16:12 rytilahti

I had some downtime and created the base structure for those two new modules in the linked PR. They are not yet exposed via SmartDimmer anyhow, but I just wanted to add the queries already in place to see if the queries keep working on HS220 in case someone is running the modularize branch.

@mrbetta if there is something missing from the smartlife.iot.dimmer, feel free to create a PR to add them to the current master branch. For adding the PIR & LAS, I think it's better to go with the modularize branch though.

rytilahti avatar Dec 13 '21 19:12 rytilahti

So from what I understand, this library will support the ks220m now, correct? And if so would it be okay for me to update the readme to reflect that?

Chrisrdouglas avatar Apr 17 '22 18:04 Chrisrdouglas

@Chrisrdouglas there is currently no nice way to interact with the new features either using the CLI nor the API, so that's why I didn't close the issue yet. The device itself is supported afaik, so feel free to update the readme!

rytilahti avatar Apr 24 '22 00:04 rytilahti

@rytilahti I'm interested in using the motion sensor data for home automation. I planned to implement some quick hack to publish this info in HA via MQTT until the proper integration with HA is available. Can you share any example of how this API can be used? Thanks!

CrazyCoder avatar Nov 30 '22 20:11 CrazyCoder

@CrazyCoder is the sensor data available in the mobile app? If yes, you could do a traffic capture and use the script in devtools to find out how it can be read from the device. The comments above I used to create the implementation provide only information about how to change the settings as far as I know.

rytilahti avatar Nov 30 '22 21:11 rytilahti

@rytilahti Oh, I didn't realize that it may not expose the PIR status at all. I don't see anything in the app UI that would indicate that motion was detected or not detected. If it still exposes something, what would be the best way to find it?

CrazyCoder avatar Nov 30 '22 22:11 CrazyCoder

@CrazyCoder considering the comprehensive input from @mrbetta, I doubt the device is reporting the status. But you could record the communications (either by using a simulator, or tcpdumping from your router) and use the script located in the devtools to inspect the trace for anything interesting.

rytilahti avatar Nov 30 '22 22:11 rytilahti

I didn't see the PIR event in the captures I did, unfortunately. I had hoped to use the PIRs as a type of burglar alarm 'Away Mode' in HA, but they don't register directly as an event. Only thing I could think of was to monitor if the light turns on itself and register that as an alarm.

mrbetta avatar Nov 30 '22 23:11 mrbetta

@rytilahti and @mrbetta Appreciate your quick responses. I'll give up on further tinkering as it appears to be unsupported by the device firmware.

I checked built-in Kasa app Smart Actions, and there is no way to set up any action based on this switch motion sensing. So it's not supported even via a cloud. There is a feature request from another user.

Will use external motion sensors for automation and the built-in sensor as the last backup option with some long interval for turning off. Could have bought switches without PIR to save some money, but at least the other controls work.

Thanks again guys for your great support and for maintaining this library.

CrazyCoder avatar Nov 30 '22 23:11 CrazyCoder

Sorry to hear that. Did you see anything besides the functionality that is already supported? I'm wondering if any of those values reported by get_config could be of any use.

edit: I just saw your most recent update, looks like there is nothing much that can be done. I suppose this issue could be closed considering the implementation is already in place?

rytilahti avatar Nov 30 '22 23:11 rytilahti

Yes, it looks like everything that is supported by the device is already implemented in this library. I tested with 1.0 HW switch, also have 1.8 HW version which I didn't try yet. I will open another issue if I find anything new.

CrazyCoder avatar Nov 30 '22 23:11 CrazyCoder

Should the mentioned PIR JSON commands be expected to work with an ES20M? When smartlife.iot.PIR enable is either 1 or 0, the switch does not react to movement.

Firmware: 1.0.8 Build 211201 Rel.123822
Hardware: 1.0

Sorry if this post is not appropriate to this thread.

caretakerscurse avatar Dec 05 '22 13:12 caretakerscurse

Please create a separate issue or discussion for that, and describe what exactly is not working.

rytilahti avatar Dec 05 '22 14:12 rytilahti