Skip to content

Commit

Permalink
Merge pull request #42 from StevusPrimus/H-J-Models-integration
Browse files Browse the repository at this point in the history
H j models integration
  • Loading branch information
roberodin authored Jun 26, 2020
2 parents f9427d3 + a762fde commit d96ce14
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 8 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ Edit it by adding the following lines:
sourcelist: '{"RaspberryPi": "KEY_HDMI2", "Chromecast": "KEY_HDMI3"}'
protocol: ctl_beta
```
### Example configuration.yaml for ctl_beta protocol for H/J Models
```
media_player:
- platform: samsungtv_custom
host: IP_ADDRESS
port: (8001 or 8002)
mac: MAC_ADDRESS
sourcelist: '{"RaspberryPi": "KEY_HDMI2", "Chromecast": "KEY_HDMI3"}'
protocol: ctl_beta
token: kdsjfbsdkjbsdkjfskdjf:2
id: 07270e01-0078-1000-8cd0-c4576e9dxxxxx
```
The token get be fetched via https://github.com/eclair4151/SmartCrypto
The id can be fetched vie http://IP_ADDRESS:8001/api/v2/
### Example configuration.yaml for ctl_qled protocol
```
media_player:
Expand Down
38 changes: 32 additions & 6 deletions custom_components/samsungtv_custom/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
vol.Optional(CONF_SOURCELIST): cv.string,
vol.Optional(CONF_APPLIST): cv.string,
vol.Optional(CONF_PROTOCOL): cv.string,
vol.Optional(CONF_ID): cv.string,
vol.Optional(CONF_TOKEN): cv.string,
}
)

Expand Down Expand Up @@ -120,6 +122,8 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
name = config.get(CONF_NAME)
mac = config.get(CONF_MAC)
timeout = config.get(CONF_TIMEOUT)
uuid = config.get(CONF_ID)
token = config.get(CONF_TOKEN)
elif discovery_info is not None:
tv_name = discovery_info.get("name")
model = discovery_info.get("model_name")
Expand All @@ -146,7 +150,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
elif protocol == "ws":
add_entities([SamsungTVDeviceWS(host, port, name, timeout, mac, uuid, sourcelist)])
else:
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol)])
add_entities([SamsungTVDevice(host, port, name, timeout, mac, uuid, sourcelist, protocol, token)])

_LOGGER.info("Samsung TV %s:%d added as '%s'", host, port, name)
else:
Expand All @@ -156,7 +160,7 @@ def setup_platform(hass, config, add_entities, discovery_info=None):
class SamsungTVDevice(MediaPlayerDevice):
"""Representation of a Samsung TV."""

def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, protocol):
def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, protocol, token):
"""Initialize the Samsung device."""
if protocol == "ctl_beta":
from .samsungctl_080b import exceptions
Expand All @@ -171,6 +175,7 @@ def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, protocol):
self._name = name
self._mac = mac
self._uuid = uuid
self._token = token
self._wol = wakeonlan
# Assume that the TV is not muted
self._muted = False
Expand All @@ -189,17 +194,33 @@ def __init__(self, host, port, name, timeout, mac, uuid, sourcelist, protocol):
"port": port,
"host": host,
"timeout": timeout,
"token": token,
}
self._sourcelist = sourcelist

if self._config["port"] in (8001, 8002):
if self._token != "":
self._config["method"] = "encrypted"
elif self._config["port"] in (8001, 8002):
self._config["method"] = "websocket"
else:
self._config["method"] = "legacy"

def update(self):
"""Update state of device."""
self.send_key("KEY")
if self._config["method"] == "encrypted" and self._config["port"] == 8001:
try:
r = requests.get(
"http://{}:8001/api/v2/".format(self._config["host"]), timeout=2
)
self._state = STATE_ON
_LOGGER.debug("State is on %s", repr(r))
# break
except:
_LOGGER.debug("State is off")
self._state = STATE_OFF

else:
self.send_key("KEY")

def get_remote(self):
"""Create or return a remote control instance."""
Expand All @@ -211,6 +232,7 @@ def get_remote(self):

def send_key(self, key):
"""Send a key to the tv and handles exceptions."""
_LOGGER.debug("Send key %s", key)
if self._power_off_in_progress() and key not in ("KEY_POWER", "KEY_POWEROFF"):
_LOGGER.info("TV is powering off, not sending command: %s", key)
return
Expand All @@ -219,7 +241,12 @@ def send_key(self, key):
retry_count = 1
for _ in range(retry_count + 1):
try:
self.get_remote().control(key)
control_result = self.get_remote().control(key)
if not control_result:
_LOGGER.debug("Failed sending command %s. Retry!", key)
self._remote.close()
self._remote = None
self.get_remote().control(key)
break
except (self._exceptions_class.ConnectionClosed, BrokenPipeError):
# BrokenPipe can occur when the commands is sent to fast
Expand Down Expand Up @@ -293,7 +320,6 @@ def turn_off(self):
self.send_key("KEY_POWEROFF")
# Force closing of remote session to provide instant UI feedback
try:
self.get_remote().close()
self._remote = None
except OSError:
_LOGGER.debug("Could not establish connection.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from ..upnp.UPNP_Device.xmlns import strip_xmlns # NOQA
from ..utils import LogIt, LogItWithReturn # NOQA

logger = logging.getLogger('samsungctl')
logger = logging.getLogger(__name__)


class URL(object):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ def control(self, key, *args, **kwargs):

elif self.sock is None:
logger.info(
self.config.model +
self.config.host +
' -- is the TV on?!?'
)
return False
Expand Down
14 changes: 14 additions & 0 deletions info.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,20 @@ Edit it by adding the following lines:
sourcelist: '{"RaspberryPi": "KEY_HDMI2", "Chromecast": "KEY_HDMI3"}'
protocol: ctl_beta
```
### Example configuration.yaml for ctl_beta protocol for H/J Models
```
media_player:
- platform: samsungtv_custom
host: IP_ADDRESS
port: (8001 or 8002)
mac: MAC_ADDRESS
sourcelist: '{"RaspberryPi": "KEY_HDMI2", "Chromecast": "KEY_HDMI3"}'
protocol: ctl_beta
token: kdsjfbsdkjbsdkjfskdjf:2
id: 07270e01-0078-1000-8cd0-c4576e9dxxxxx
```
The token get be fetched via https://github.com/eclair4151/SmartCrypto
The id can be fetched vie http://IP_ADDRESS:8001/api/v2/
### Example configuration.yaml for ctl_qled protocol
```
media_player:
Expand Down

0 comments on commit d96ce14

Please sign in to comment.