Add sensor name prefix. Fix #20. Add Toast command

This commit is contained in:
Thomas Lovén 2019-10-03 15:26:54 +02:00
parent 98f2797aa1
commit eaed9cd8d9
6 changed files with 40 additions and 5 deletions

View File

@ -60,6 +60,16 @@ This binds the *aliases* `arrakis` to `99980b13-dabc9563` and `dashboard` to `d2
Note: Aliases must be unique. 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 ## Entities
Once `browser_mod` is installed, loading up your Home Assistant frontend on a new *device* will create three to five new devices. Once `browser_mod` is installed, loading up your Home Assistant frontend on a new *device* will create three to five new devices.
@ -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. 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 ### popup
``` ```
service: browser_mod.popup service: browser_mod.popup

View File

@ -22,7 +22,7 @@ async def async_setup(hass, config):
DATA_DEVICES: {}, DATA_DEVICES: {},
DATA_ALIASES: aliases, DATA_ALIASES: aliases,
DATA_ADDERS: {}, DATA_ADDERS: {},
DATA_CONFIG: config[DOMAIN].get(CONFIG_DEVICES, {}), DATA_CONFIG: config[DOMAIN],
} }
await hass.helpers.discovery.async_load_platform("media_player", DOMAIN, {}, config) await hass.helpers.discovery.async_load_platform("media_player", DOMAIN, {}, config)

File diff suppressed because one or more lines are too long

View File

@ -10,6 +10,7 @@ DATA_ADDERS = "adders"
DATA_CONFIG = "config" DATA_CONFIG = "config"
CONFIG_DEVICES = "devices" CONFIG_DEVICES = "devices"
CONFIG_PREFIX = "prefix"
WS_ROOT = DOMAIN WS_ROOT = DOMAIN
WS_CONNECT = "{}/connect".format(WS_ROOT) WS_CONNECT = "{}/connect".format(WS_ROOT)
@ -26,4 +27,5 @@ USER_COMMANDS = [
"lovelace-reload", "lovelace-reload",
"blackout", "blackout",
"no-blackout", "no-blackout",
"toast",
] ]

View File

@ -2,7 +2,7 @@ import logging
from homeassistant.helpers.entity import Entity, async_generate_entity_id 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__) _LOGGER = logging.getLogger(__name__)
@ -16,7 +16,7 @@ def get_alias(hass, deviceID):
return None return None
def get_config(hass, deviceID): 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('-','_'), {})) return config.get(deviceID, config.get(deviceID.replace('-','_'), {}))
def create_entity(hass, platform, deviceID, connection): def create_entity(hass, platform, deviceID, connection):
@ -39,7 +39,8 @@ class BrowserModEntity(Entity):
self.connection = connection self.connection = connection
self.deviceID = deviceID self.deviceID = deviceID
self._data = {} 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): def updated(self):
pass pass

View File

@ -126,6 +126,9 @@ class BrowserMod {
this.mute(msg); this.mute(msg);
break; break;
case "toast":
this.toast(msg);
break;
case "popup": case "popup":
this.popup(msg); this.popup(msg);
break; break;
@ -208,6 +211,15 @@ class BrowserMod {
this.player.muted = Boolean(msg.mute) 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){ popup(msg){
if(!msg.title && !msg.auto_close) return; if(!msg.title && !msg.auto_close) return;
if(!msg.card) return; if(!msg.card) return;