Skip to content

Commit

Permalink
Add sensor name prefix. Fix #20. Add Toast command
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasloven committed Oct 3, 2019
1 parent 98f2797 commit eaed9cd
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ This binds the *aliases* `arrakis` to `99980b13-dabc9563` and `dashboard` to `d2

Note: Aliases must be unique.

### Prefix
You can add a custom prefix to all entity ids in `configuration.yaml`:

E.g. to give entities default names like `media_player.browser_99980b13_dabc9563` add:
```yaml
browser_mod:
prefix: "browser_"
```
This does not apply to devices with an alias.

## Entities
Once `browser_mod` is installed, loading up your Home Assistant frontend on a new *device* will create three to five new devices.

Expand Down Expand Up @@ -185,6 +195,16 @@ will show the more-info dialog of `camera.front_door` on the *devices* `ded3b4dc
The optional parameter `large: true` will make the popup wider.
### toast
```
service: browser_mod.toast
service_data:
message: Short message
```
Display a toast notification on all devices.
The optional parameter `duration:` determines the time (in ms) that the toast is shown. Set to 0 for persistent display. Default is 3000.
### popup
```
service: browser_mod.popup
Expand Down
2 changes: 1 addition & 1 deletion custom_components/browser_mod/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ async def async_setup(hass, config):
DATA_DEVICES: {},
DATA_ALIASES: aliases,
DATA_ADDERS: {},
DATA_CONFIG: config[DOMAIN].get(CONFIG_DEVICES, {}),
DATA_CONFIG: config[DOMAIN],
}

await hass.helpers.discovery.async_load_platform("media_player", DOMAIN, {}, config)
Expand Down
2 changes: 1 addition & 1 deletion custom_components/browser_mod/browser_mod.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions custom_components/browser_mod/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DATA_CONFIG = "config"

CONFIG_DEVICES = "devices"
CONFIG_PREFIX = "prefix"

WS_ROOT = DOMAIN
WS_CONNECT = "{}/connect".format(WS_ROOT)
Expand All @@ -26,4 +27,5 @@
"lovelace-reload",
"blackout",
"no-blackout",
"toast",
]
7 changes: 4 additions & 3 deletions custom_components/browser_mod/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from homeassistant.helpers.entity import Entity, async_generate_entity_id

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

_LOGGER = logging.getLogger(__name__)

Expand All @@ -16,7 +16,7 @@ def get_alias(hass, deviceID):
return None

def get_config(hass, deviceID):
config = hass.data[DOMAIN][DATA_CONFIG]
config = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_DEVICES, {})
return config.get(deviceID, config.get(deviceID.replace('-','_'), {}))

def create_entity(hass, platform, deviceID, connection):
Expand All @@ -39,7 +39,8 @@ def __init__(self, hass, connection, deviceID, alias=None):
self.connection = connection
self.deviceID = deviceID
self._data = {}
self.entity_id = async_generate_entity_id(self.domain+".{}", alias or deviceID, hass=hass)
prefix = hass.data[DOMAIN][DATA_CONFIG].get(CONFIG_PREFIX, '')
self.entity_id = async_generate_entity_id(self.domain+".{}", alias or f"{prefix}{deviceID}", hass=hass)

def updated(self):
pass
Expand Down
12 changes: 12 additions & 0 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ class BrowserMod {
this.mute(msg);
break;

case "toast":
this.toast(msg);
break;
case "popup":
this.popup(msg);
break;
Expand Down Expand Up @@ -208,6 +211,15 @@ class BrowserMod {
this.player.muted = Boolean(msg.mute)
}

toast(msg) {
if(!msg.message) return;

fireEvent("hass-notification", {
message: msg.message,
duration: msg.duration !== undefined ? parseInt(msg.duration) : undefined
}, document.querySelector("home-assistant"));
}

popup(msg){
if(!msg.title && !msg.auto_close) return;
if(!msg.card) return;
Expand Down

0 comments on commit eaed9cd

Please sign in to comment.