def run(self): while True: self.get_yamaha_running_tv() self.get_tv_running() RxvLogger.debug("Status:" + str(self.running) + " Amplifier: " + str(self.yh_running) + " Amplifier TV input: " + str(self.yh_tv_running) + " TV: " + str(self.tv_running)) if not self.running: if self.yh_tv_running and self.tv_running: self.running = True elif self.tv_running and not self.yh_running and not self.yh_tv_running: self.connect_all() elif not self.tv_running and self.yh_tv_running: self.connect_all() else: if self.tv_running and self.yh_tv_running: None elif self.tv_running and not self.yh_running and not self.yh_tv_running: self.power_off_all() elif not self.tv_running and self.yh_tv_running: self.power_off_all() else: self.running = False time.sleep(5)
def setup(self): self.check_and_create_store() if not self.key_correct(): RxvLogger.log("LG key required please accept connection on tv:") if not self.is_running(): RxvLogger.log("Turning on TV and wait for boot 10s") self.start() time.sleep(10) self.connect()
def power_off(self): self.check_and_create_store() lg = self.connect() lg_sys = SystemControl(lg) RxvLogger.log("TV power off") lg_sys.notify("RxvLG: TV will be power off!") time.sleep(2) lg_sys.power_off() lg.close() time.sleep(2)
def __init__(self): self.running = False self.amplifier = RxvLGYamaha( "http://192.168.0.4:80/YamahaRemoteControl/ctrl", "RX-V477") self.tv = RxvLGTv("192.168.0.6") self.tv_running = False self.yh_tv_running = False self.yh_running = False RxvLogger.log("Starting") self.tv.setup() if self.get_tv_running(): self.tv.register_volume_control(self.volume_control_callback)
def run_os_command(self, args): try: # stdout = subprocess.PIPE lets you redirect the output RxvLogger.debug(' '.join(args)) res = subprocess.Popen(args, stdout=subprocess.PIPE) except OSError: RxvLogger.error("error: popen") exit( -1 ) # if the subprocess call failed, there's not much point in continuing res.wait( ) # wait for process to finish; this also sets the returncode variable inside 'res' if res.returncode != 0: RxvLogger.debug(" os.wait:exit status != 0\n") else: RxvLogger.debug("os.wait:({},{})".format(res.pid, res.returncode)) # access the output from stdout result = res.stdout.read() RxvLogger.debug("after read: {}".format(result)) return res.returncode
def power_off_all(self): RxvLogger.log("Disconnecting...") if self.amplifier.is_running(): RxvLogger.log("Power off amplifier") self.amplifier.power_off() self.running = False elif self.tv_running: RxvLogger.log("Power off TV") self.tv.power_off() self.running = False
def connect_all(self): RxvLogger.log("Connecting...") if not self.yh_tv_running: RxvLogger.log("Starting amplifier") self.amplifier.start_and_connect() self.running = True elif not self.tv_running: RxvLogger.log("Starting TV") self.tv.start() self.running = True self.tv.register_volume_control(self.volume_control_callback)
def check_and_create_store(self): RxvLogger.debug("TV connect read key") try: with open(RxvLGTv.KEY_FILE, 'r+') as file: self.store = yaml.full_load(file) except IOError: self.store = {} RxvLogger.debug("TV connect key:" + str(self.store)) if not self.key_correct(): RxvLogger.log("TV connect key empty will be created") self.store = {}
def volume_control_callback(self, status, payload): RxvLogger.debug("Volume action") if status: if u'changed' in payload: if u'cause' in payload and payload[u'changed'] == [u'volume']: if payload[u'cause'] == u'volumeDown': self.amplifier.change_volume(-0.5) elif payload[u'cause'] == u'volumeUp': self.amplifier.change_volume(0.5) elif u'muted' in payload and payload[u'changed'] == [u'muted']: self.amplifier.mute(payload[u'muted']) RxvLogger.debug(str(payload)) else: RxvLogger.debug("Something went wrong.")
def start(self): RxvLogger.log("TV power on") trying = 0 while trying < 10: self.run_os_command( ['/usr/bin/kodi-send', '-a', 'CECActivateSource']) time.sleep(2) if self.is_running(): trying = 11 elif trying > 10: RxvLogger.error("Could not turn on TV") return else: RxvLogger.debug("Retrying connect TV") time.sleep(1) time.sleep(30)
def mute(self, state): RxvLogger.debug("Mute change change: " + str(state)) self.rv.mute = state
def __init__(self, url, name): RxvLogger.log("Yamaha connect ip: " + url) self.rv = rxv.RXV(url, name)
def power_off(self): RxvLogger.log("Yamaha power off") self.rv.on = False
def change_volume(self, value): vol = self.rv.volume RxvLogger.debug("Volume change: " + str(vol + value)) self.rv.volume = vol + value
def is_tv_enabled(self): RxvLogger.debug("Yamaha input?: " + self.rv.input) return re.search(r"^((AV)|(HDMI))\d+$", self.rv.input) is not None
def start_and_connect(self): RxvLogger.log("Yamaha power on") self.rv.on = True self.rv.input = 'AV1'
def is_running(self): RxvLogger.debug("Yamaha running?: " + str(self.rv.on)) return self.rv.on
def connect(self): if not self.is_running(): self.start() RxvLogger.debug("TV connect ip:" + self.ip) for i in range(0, 10): try: lg = WebOSClient(self.ip) lg.connect() except Exception as e: RxvLogger.error("Unable connect TV, retry e: " + str(e)) time.sleep(3) else: break try: for status in lg.register(self.store): if status == WebOSClient.PROMPTED: RxvLogger.log("Please accept the connect on the TV!") elif status == WebOSClient.REGISTERED: RxvLogger.log("Registration successful!") lg_sys = SystemControl(lg) lg_sys.notify("RxvLG: Registration successful!") self.save_store() return lg except Exception as e: RxvLogger.error("Registration failed exiting!") RxvLogger.debug("Exception" + str(e))
def save_store(self): RxvLogger.debug("TV connect key stored: " + str(self.store)) with open(RxvLGTv.KEY_FILE, 'w') as file: yaml.dump(self.store, file)
def register_volume_control(self, callback): lg = self.connect() media = MediaControl(lg) RxvLogger.debug("callback registered") media.subscribe_get_volume(callback)
def __init__(self, ip): self.ip = ip RxvLogger.log("TV connect ip: " + self.ip) self.store = {}