-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from infeeeee/monitor-switch
Monitor as switch, linked command with sensor, battery fix
- Loading branch information
Showing
7 changed files
with
236 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
from pathlib import Path | ||
|
||
from IoTuring.Entity.Entity import Entity | ||
from IoTuring.Entity.EntityData import EntityCommand, EntitySensor | ||
from IoTuring.Configurator.MenuPreset import MenuPreset | ||
|
||
KEY_STATE = 'fileswitch_state' | ||
KEY_CMD = 'fileswitch' | ||
|
||
CONFIG_KEY_PATH = 'path' | ||
|
||
|
||
class FileSwitch(Entity): | ||
NAME = "FileSwitch" | ||
ALLOW_MULTI_INSTANCE = True | ||
|
||
def Initialize(self): | ||
|
||
try: | ||
self.config_path = self.GetConfigurations()[CONFIG_KEY_PATH] | ||
except Exception as e: | ||
raise Exception("Configuration error: " + str(e)) | ||
|
||
self.RegisterEntitySensor(EntitySensor(self, KEY_STATE, True)) | ||
self.RegisterEntityCommand(EntityCommand( | ||
self, KEY_CMD, self.Callback, KEY_STATE)) | ||
|
||
def PostInitialize(self): | ||
pass | ||
|
||
def Callback(self, message): | ||
payloadString = message.payload.decode('utf-8') | ||
|
||
if payloadString == "True": | ||
Path(self.config_path).touch() | ||
|
||
elif payloadString == "False": | ||
Path(self.config_path).unlink(missing_ok=True) | ||
|
||
else: | ||
raise Exception('Incorrect payload!') | ||
|
||
def Update(self): | ||
self.SetEntitySensorValue(KEY_STATE, | ||
str(Path(self.config_path).exists())) | ||
|
||
extra = {} | ||
extra["Path"] = str(self.config_path) | ||
self.SetEntitySensorExtraAttributes(KEY_STATE, extra) | ||
|
||
@classmethod | ||
def ConfigurationPreset(self): | ||
preset = MenuPreset() | ||
preset.AddEntry("Path to file?", CONFIG_KEY_PATH, mandatory=True) | ||
return preset |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.