Skip to content

Commit

Permalink
Merge pull request #29 from itchannel/1.17
Browse files Browse the repository at this point in the history
added new switch
  • Loading branch information
itchannel authored May 27, 2024
2 parents e0919e4 + 8fa9a29 commit fd0691b
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 32 deletions.
5 changes: 5 additions & 0 deletions custom_components/tdarr/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,9 @@
"nodefps": {"icon": "mdi:video", "unit_of_measurement": "FPS"},
"stats_totalfps": {"icon": "mdi:video", "unit_of_measurement": "FPS", "type": "single", "entry": "nodes"},
"library": {"icon": "mdi:folder-multiple", "unit_of_measurement": "Files"},
}

SWITCHES = {
"pauseAll": {"icon": "mdi:pause-circle", "name": "pauseAll", "data": "globalsettings"},
"ignoreSchedules": {"icon": "mdi:calendar-remove", "name": "ignoreSchedules", "data": "globalsettings"},
}
2 changes: 1 addition & 1 deletion custom_components/tdarr/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@
"issue_tracker": "https://github.com/itchannel/tdarr_ha/issues",
"requirements": [],
"ssdp": [],
"version": "1.16",
"version": "1.17",
"zeroconf": []
}
39 changes: 25 additions & 14 deletions custom_components/tdarr/switch.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,41 +4,48 @@
from homeassistant.components.switch import SwitchEntity

from . import TdarrEntity
from .const import DOMAIN, COORDINATOR
from .const import DOMAIN, COORDINATOR, SWITCHES

_LOGGER = logging.getLogger(__name__)

async def async_setup_entry(hass, config_entry, async_add_entities):
"""Add the Switch from the config."""
entry = hass.data[DOMAIN][config_entry.entry_id][COORDINATOR]

switches = []
for key, value in entry.data["nodes"].items():
sw = Switch(entry, value, config_entry.options)
async_add_entities([sw], False)
sw = Switch(entry, value, value["_id"], config_entry.options)
switches.append(sw)

sw = Switch(entry, entry.data["globalsettings"], config_entry.options)
async_add_entities([sw], False)
for key, value in SWITCHES.items():
_LOGGER.debug(value)
switches.append(Switch(entry, entry.data["globalsettings"], value["name"], config_entry.options))

async_add_entities(switches, False)

class Switch(TdarrEntity, SwitchEntity):
"""Define the Switch for turning ignition off/on"""

def __init__(self, coordinator, switch, options):
def __init__(self, coordinator, switch, name, options):
_LOGGER.debug(name)
if "nodeName" in switch:
self._device_id = "tdarr_node_" + switch["nodeName"] + "_paused"
elif "_id" in switch and switch["_id"] == "globalsettings":
elif name == "pauseAll":
self._device_id = "tdarr_pause_all"
elif name == "ignoreSchedules":
self._device_id = "tdarr_ignore_schedules"
else:
self._device_id = "tdarr_node_" + switch["_id"] + "_paused"
self.switch = switch
self.coordinator = coordinator
self._state = None
self.object_name = name
# Required for HA 2022.7
self.coordinator_context = object()

async def async_turn_on(self, **kwargs):
update = await self.coordinator.hass.async_add_executor_job(
self.coordinator.tdarr.pauseNode,
self.switch["_id"],
self.object_name,
True
)

Expand All @@ -53,7 +60,7 @@ async def async_turn_on(self, **kwargs):
async def async_turn_off(self, **kwargs):
update = await self.coordinator.hass.async_add_executor_job(
self.coordinator.tdarr.pauseNode,
self.switch["_id"],
self.object_name,
False
)

Expand All @@ -65,11 +72,13 @@ async def async_turn_off(self, **kwargs):

@property
def name(self):
_LOGGER.debug(self.switch)
#_LOGGER.debug(self.switch)
if "nodeName" in self.switch:
return "tdarr_node_" + self.switch["nodeName"] + "_paused"
elif "_id" in self.switch and self.switch["_id"] == "globalsettings":
elif self.object_name == "pauseAll":
return "tdarr_pause_all"
elif self.object_name == "ignoreSchedules":
return "tdarr_ignore_schedules"
else:
return "tdarr_node_" + self.switch["_id"] + "_paused"

Expand All @@ -86,8 +95,10 @@ def is_on(self):
elif self._state == False:
self._state = None
return False
if self.switch["_id"] == "globalsettings":
if self.object_name == "pauseAll":
return self.coordinator.data["globalsettings"]["pauseAllNodes"]
elif self.object_name == "ignoreSchedules":
return self.coordinator.data["globalsettings"]["ignoreSchedules"]
for key, value in self.coordinator.data["nodes"].items():
if value["_id"] == self.switch["_id"]:
return value["nodePaused"]
Expand All @@ -96,7 +107,7 @@ def is_on(self):

@property
def icon(self):
return None
return SWITCHES.get(self.object_name, {}).get("icon", None)

@property
def extra_state_attributes(self):
Expand Down
46 changes: 29 additions & 17 deletions custom_components/tdarr/tdarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,42 +41,42 @@ def getStats(self):
else:
return "ERROR"

def getSettings(self):
def getStaged(self):
post = {
"data": {
"collection":"SettingsGlobalJSONDB",
"mode":"getById",
"docID":"globalsettings",
"obj":{}
"filters":[],
"start":0,
"pageSize":10,
"sorts":[],
"opts":{}
},
"timeout":1000
}
r = requests.post(self.baseurl + 'cruddb', json = post)
r = requests.post(self.baseurl + 'client/staged', json = post)
if r.status_code == 200:
return r.json()
else:
return "ERROR"

def getStaged(self):
def getSettings(self):
post = {
"data": {
"filters":[],
"start":0,
"pageSize":10,
"sorts":[],
"opts":{}
"collection":"SettingsGlobalJSONDB",
"mode":"getById",
"docID":"globalsettings",
"obj":{}
},
"timeout":1000
}
r = requests.post(self.baseurl + 'client/staged', json = post)
r = requests.post(self.baseurl + 'cruddb', json = post)
if r.status_code == 200:
return r.json()
else:
return "ERROR"

def pauseNode(self, nodeID, status):

if nodeID == "globalsettings":
if nodeID == "pauseAll":
data = {
"data":{
"collection":"SettingsGlobalJSONDB",
Expand All @@ -89,6 +89,19 @@ def pauseNode(self, nodeID, status):
"timeout":20000
}
r = requests.post(self.baseurl + 'cruddb', json=data)
elif nodeID == "ignoreSchedules":
data = {
"data":{
"collection":"SettingsGlobalJSONDB",
"mode":"update",
"docID":"globalsettings",
"obj":{
"ignoreSchedules": status
}
},
"timeout":20000
}
r = requests.post(self.baseurl + 'cruddb', json=data)
else:
data = {
"data": {
Expand All @@ -99,7 +112,6 @@ def pauseNode(self, nodeID, status):
}
}
r = requests.post(self.baseurl + 'update-node', json=data)

if r.status_code == 200:
return "OK"
else:
Expand Down
3 changes: 3 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
## **Changelog**
### Version 1.17
- Added disable schedules toggle
- Add icons for certain switches
### Version 1.16
- Added Health and Transcode failed sensors
- Added icons
Expand Down

0 comments on commit fd0691b

Please sign in to comment.