Skip to content

Commit

Permalink
Huge restructure in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Sep 17, 2019
1 parent 13f17af commit fdc509f
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 104 deletions.
9 changes: 7 additions & 2 deletions custom_components/browser_mod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@ async def async_setup(hass, config):
hass.data[DOMAIN] = {
DATA_DEVICES: {},
DATA_ALIASES: aliases,
DATA_ADDERS: [],
DATA_ADDERS: {},
}

await hass.helpers.discovery.async_load_platform("media_player", DOMAIN, {}, config)
await hass.helpers.discovery.async_load_platform("sensor", DOMAIN, {}, config)
await hass.helpers.discovery.async_load_platform("light", DOMAIN, {}, config)

setup_connection(hass)
await setup_connection(hass, config)

setup_service(hass)

return True



2 changes: 1 addition & 1 deletion custom_components/browser_mod/browser_mod.js

Large diffs are not rendered by default.

148 changes: 85 additions & 63 deletions custom_components/browser_mod/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,78 +4,100 @@
from homeassistant.components.websocket_api import websocket_command, result_message, event_message, async_register_command
from homeassistant.helpers.entity import Entity, async_generate_entity_id

from .const import DOMAIN, DATA_DEVICES, DATA_ADDERS, WS_CONNECT, WS_UPDATE
from .const import DOMAIN, WS_CONNECT, WS_UPDATE
from .helpers import get_devices, create_entity

_LOGGER = logging.getLogger(__name__)

async def setup_connection(hass, config):
_LOGGER.error("--------------------")
_LOGGER.error("Setting up BM connection")

@websocket_command({
vol.Required("type"): WS_CONNECT,
vol.Required("deviceID"): str,
})
def handle_connect(hass, connection, msg):
_LOGGER.error("--------------------")
_LOGGER.error("CONNECTING BM")
deviceID = msg["deviceID"]

device = get_devices(hass).get(deviceID, BrowserModConnection(hass, deviceID))
device.connect(connection, msg["id"])
get_devices(hass)[deviceID] = device

_LOGGER.error("DONE")
connection.send_message(result_message(msg["id"]))

@websocket_command({
vol.Required("type"): WS_UPDATE,
vol.Required("deviceID"): str,
vol.Optional("data"): dict,
})
def handle_update( hass, connection, msg):
_LOGGER.error("--------------------")
_LOGGER.error("UPDATING BM")
_LOGGER.error(msg)
devices = get_devices(hass)
deviceID = msg["deviceID"]
if deviceID in devices:
devices[deviceID].update(msg.get("data", None))

def setup_connection(hass):
async_register_command(hass, handle_connect)
async_register_command(hass, handle_update)


@websocket_command({
vol.Required("type"): WS_CONNECT,
vol.Required("deviceID"): str,
})
def handle_connect(hass, connection, msg):

devices = hass.data[DOMAIN][DATA_DEVICES]
deviceID = msg["deviceID"]
if deviceID in devices:
devices[deviceID].ws_connect(connection, msg["id"])
else:
adder = hass.data[DOMAIN][DATA_ADDERS][0]
devices[deviceID] = adder(hass, deviceID, connection, msg["id"])
connection.send_message(result_message(msg["id"]))


@websocket_command({
vol.Required("type"): WS_UPDATE,
vol.Required("deviceID"): str,
vol.Optional("data"): dict,
})
def handle_update(hass, connection, msg):
devices = hass.data[DOMAIN][DATA_DEVICES]
deviceID = msg["deviceID"]
if deviceID in devices:
devices[deviceID].ws_update(msg.get("data", None))


class BrowserModEntity(Entity):
def __init__(self, hass, deviceID, alias=None):
self._deviceID = deviceID
self._alias = alias
self._ws_data = {}
self._ws_connection = None
self.entity_id = async_generate_entity_id("media_player.{}", alias or deviceID, hass=hass)

def ws_send(self, command, **kwargs):
if self._ws_connection:
self._ws_connection.send_message(event_message(self._ws_cid, {
class BrowserModConnection:
def __init__(self, hass, deviceID):
self.hass = hass
self.deviceID = deviceID
self.connection = []

self.media_player = None
self.screen = None
self.sensor = None

def connect(self, connection, cid):
self.connection.append((connection, cid))
_LOGGER.error("********************")
_LOGGER.error("Connected %s", self.deviceID)
self.send("update")

def disconnect():
self.connection.remove((connection, cid))

connection.subscriptions[cid] = disconnect

def send(self, command, **kwargs):
if self.connection:
connection, cid = self.connection[-1]
connection.send_message(event_message(cid, {
"command": command,
**kwargs,
}))

def ws_connect(self, connection, cid):
self._ws_cid = cid
self._ws_connection = connection
self.ws_send("update", entity_id=self.entity_id)
connection.subscriptions[cid] = self.ws_disconnect
if self.hass:
self.schedule_update_ha_state()

def ws_disconnect(self):
self._ws_cid = None
self._ws_connection = None
if self.hass:
self.schedule_update_ha_state()

def ws_update(self, data):
self._ws_data = data
if self.hass:
self.schedule_update_ha_state()

@property
def device_id(self):
return self._deviceID
def update(self, data):
_LOGGER.error("********************")
_LOGGER.error("Got update %s for %s", data, self.deviceID)
if data.get('player'):
self.media_player = self.media_player or create_entity(
self.hass,
'media_player',
self.deviceID,
self)
self.media_player.data = data.get('player')
if data.get('browser'):
self.sensor = self.sensor or create_entity(
self.hass,
'sensor',
self.deviceID,
self)
self.sensor.data = data.get('browser')
if data.get('screen'):
self.screen = self.screen or create_entity(
self.hass,
'light',
self.deviceID,
self)
self.screen.data = data.get('screen')

58 changes: 58 additions & 0 deletions custom_components/browser_mod/helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import logging

from homeassistant.helpers.entity import Entity, async_generate_entity_id

from .const import DOMAIN, DATA_DEVICES, DATA_ALIASES, DATA_ADDERS, CONFIG_DEVICES

_LOGGER = logging.getLogger(__name__)

def get_devices(hass):
return hass.data[DOMAIN][DATA_DEVICES]

def get_alias(hass, deviceID):
for k,v in hass.data[DOMAIN][DATA_ALIASES].items():
if v == deviceID:
return k
return None

def create_entity(hass, platform, deviceID, connection):
_LOGGER.error("********************")
_LOGGER.error("Creating %s for %s", platform, deviceID)
adder = hass.data[DOMAIN][DATA_ADDERS][platform]
entity = adder(hass, deviceID, connection, get_alias(hass, deviceID))
return entity

def setup_platform(hass, config, async_add_devices, platform, cls):
def adder(hass, deviceID, connection, alias=None):
entity = cls(hass, connection, deviceID, alias)
async_add_devices([entity])
return entity
hass.data[DOMAIN][DATA_ADDERS][platform] = adder
return True

class BrowserModEntity(Entity):

def __init__(self, hass, connection, deviceID, alias=None):
self.hass = hass
self.connection = connection
self.deviceID = deviceID
self._data = {}
self.entity_id = async_generate_entity_id(self.domain+".{}", alias or deviceID, hass=hass)

def updated(self):
pass

@property
def data(self):
return self._data
@data.setter
def data(self, data):
self._data = data
self.updated()

@property
def device_id(self):
return self.deviceID

def send(self, command, **kwargs):
self.connection.send(command, **kwargs)
52 changes: 52 additions & 0 deletions custom_components/browser_mod/light.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
import logging
from datetime import datetime

from homeassistant.const import STATE_UNAVAILABLE, STATE_ON, STATE_OFF
from homeassistant.components.light import Light, SUPPORT_BRIGHTNESS

from .helpers import setup_platform, BrowserModEntity

PLATFORM = 'light'

async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
return setup_platform(hass, config, async_add_devices, PLATFORM, BrowserModLight)

class BrowserModLight(Light, BrowserModEntity):
domain = PLATFORM

def __init__(self, hass, connection, deviceID, alias=None):
super().__init__(hass, connection, deviceID, alias)

def updated(self):
self.last_seen = datetime.now()
self.schedule_update_ha_state()

@property
def state(self):
if not self.connection.connection:
return STATE_UNAVAILABLE
if self.data.get('blackout', False):
return STATE_OFF
return STATE_ON

@property
def is_on(self):
return not self.data.get('blackout', False)

@property
def device_state_attributes(self):
return {
"type": "browser_mod",
}

@property
def supported_features(self):
if self.data.get('brightness', False):
return SUPPORT_BRIGHTNESS
return 0

def turn_on(self, **kwargs):
self.connection.send("no-blackout", **kwargs)

def turn_off(self, **kwargs):
self.connection.send("blackout")
54 changes: 21 additions & 33 deletions custom_components/browser_mod/media_player.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,36 @@
STATE_UNKNOWN,
)

from .const import DOMAIN, DATA_DEVICES, DATA_ADDERS, DATA_ALIASES
from .connection import BrowserModEntity
from .helpers import setup_platform, BrowserModEntity

_LOGGER = logging.getLogger(__name__)

PLATFORM = 'media_player'

async def async_setup_platform(hass, config, async_add_devices, discovery_info=None):
def adder(hass, deviceID, connection, cid):
player = BrowserModPlayer(hass, deviceID)
if connection:
player.ws_connect(connection, cid)
async_add_devices([player])
return player
hass.data[DOMAIN][DATA_ADDERS].append(adder)

for k,v in hass.data[DOMAIN][DATA_ALIASES].items():
devices = hass.data[DOMAIN][DATA_DEVICES]
devices[v] = BrowserModPlayer(hass, v, k)
async_add_devices([devices[v]])
return setup_platform(hass, config, async_add_devices, PLATFORM, BrowserModPlayer)


class BrowserModPlayer(MediaPlayerDevice, BrowserModEntity):
domain = PLATFORM

def __init__(self, hass, connection, deviceID, alias=None):
super().__init__(hass, connection, deviceID, alias)

def __init__(self, hass, deviceID, alias=None):
super().__init__(hass, deviceID, alias)
def updated(self):
self.schedule_update_ha_state()

@property
def device_state_attributes(self):
return {
"type": "browser",
**self._ws_data.get("browser", {}),
"type": "browser_mod",
}

@property
def _player_data(self):
return self._ws_data.get("player", {})

@property
def state(self):
if not self._ws_connection:
if not self.connection.connection:
return STATE_UNAVAILABLE
state = self._player_data.get("state", "unknown")
state = self.data.get("state", "unknown")
return {
"playing": STATE_PLAYING,
"paused": STATE_PAUSED,
Expand All @@ -69,24 +57,24 @@ def supported_features(self):
)
@property
def volume_level(self):
return self._player_data.get("volume", 0)
return self.data.get("volume", 0)
@property
def is_volume_muted(self):
return self._player_data.get("muted", False)
return self.data.get("muted", False)
@property
def media_content_id(self):
return self._player_data.get("src", "")
return self.data.get("src", "")

def set_volume_level(self, volume):
self.ws_send("set_volume", volume_level=volume)
self.connection.send("set_volume", volume_level=volume)
def mute_volume(self, mute):
self.ws_send("mute", mute=mute)
self.connection.send("mute", mute=mute)

def play_media(self, media_type, media_id, **kwargs):
self.ws_send("play", media_content_id=media_id)
self.connection.send("play", media_content_id=media_id)
def media_play(self):
self.ws_send("play")
self.connection.send("play")
def media_pause(self):
self.ws_send("pause")
self.connection.send("pause")
def media_stop(self):
self.ws_send("stop")
self.connection.send("stop")
Loading

0 comments on commit fdc509f

Please sign in to comment.