mirror of
https://github.com/home-assistant/core.git
synced 2025-07-10 14:57:09 +00:00
Allow device_tracker platforms to specify picture and icon upon discovery (#8018)
* Allow device tracker platforms to specify picture * Allow device tracker to specify icon during discovery * Clean up and add tests * Fix lint * Fix test
This commit is contained in:
parent
8c0967a190
commit
02f7eb9675
@ -122,12 +122,7 @@ def async_setup(hass: HomeAssistantType, config: ConfigType):
|
|||||||
"""Set up the device tracker."""
|
"""Set up the device tracker."""
|
||||||
yaml_path = hass.config.path(YAML_DEVICES)
|
yaml_path = hass.config.path(YAML_DEVICES)
|
||||||
|
|
||||||
try:
|
|
||||||
conf = config.get(DOMAIN, [])
|
conf = config.get(DOMAIN, [])
|
||||||
except vol.Invalid as ex:
|
|
||||||
async_log_exception(ex, DOMAIN, config, hass)
|
|
||||||
return False
|
|
||||||
else:
|
|
||||||
conf = conf[0] if conf else {}
|
conf = conf[0] if conf else {}
|
||||||
consider_home = conf.get(CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME)
|
consider_home = conf.get(CONF_CONSIDER_HOME, DEFAULT_CONSIDER_HOME)
|
||||||
track_new = conf.get(CONF_TRACK_NEW, DEFAULT_TRACK_NEW)
|
track_new = conf.get(CONF_TRACK_NEW, DEFAULT_TRACK_NEW)
|
||||||
@ -246,18 +241,21 @@ class DeviceTracker(object):
|
|||||||
def see(self, mac: str=None, dev_id: str=None, host_name: str=None,
|
def see(self, mac: str=None, dev_id: str=None, host_name: str=None,
|
||||||
location_name: str=None, gps: GPSType=None, gps_accuracy=None,
|
location_name: str=None, gps: GPSType=None, gps_accuracy=None,
|
||||||
battery: str=None, attributes: dict=None,
|
battery: str=None, attributes: dict=None,
|
||||||
source_type: str=SOURCE_TYPE_GPS):
|
source_type: str=SOURCE_TYPE_GPS, picture: str=None,
|
||||||
|
icon: str=None):
|
||||||
"""Notify the device tracker that you see a device."""
|
"""Notify the device tracker that you see a device."""
|
||||||
self.hass.add_job(
|
self.hass.add_job(
|
||||||
self.async_see(mac, dev_id, host_name, location_name, gps,
|
self.async_see(mac, dev_id, host_name, location_name, gps,
|
||||||
gps_accuracy, battery, attributes, source_type)
|
gps_accuracy, battery, attributes, source_type,
|
||||||
|
picture, icon)
|
||||||
)
|
)
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def async_see(self, mac: str=None, dev_id: str=None, host_name: str=None,
|
def async_see(self, mac: str=None, dev_id: str=None, host_name: str=None,
|
||||||
location_name: str=None, gps: GPSType=None,
|
location_name: str=None, gps: GPSType=None,
|
||||||
gps_accuracy=None, battery: str=None, attributes: dict=None,
|
gps_accuracy=None, battery: str=None, attributes: dict=None,
|
||||||
source_type: str=SOURCE_TYPE_GPS):
|
source_type: str=SOURCE_TYPE_GPS, picture: str=None,
|
||||||
|
icon: str=None):
|
||||||
"""Notify the device tracker that you see a device.
|
"""Notify the device tracker that you see a device.
|
||||||
|
|
||||||
This method is a coroutine.
|
This method is a coroutine.
|
||||||
@ -285,7 +283,8 @@ class DeviceTracker(object):
|
|||||||
dev_id = util.ensure_unique_string(dev_id, self.devices.keys())
|
dev_id = util.ensure_unique_string(dev_id, self.devices.keys())
|
||||||
device = Device(
|
device = Device(
|
||||||
self.hass, self.consider_home, self.track_new,
|
self.hass, self.consider_home, self.track_new,
|
||||||
dev_id, mac, (host_name or dev_id).replace('_', ' '))
|
dev_id, mac, (host_name or dev_id).replace('_', ' '),
|
||||||
|
picture=picture, icon=icon)
|
||||||
self.devices[dev_id] = device
|
self.devices[dev_id] = device
|
||||||
if mac is not None:
|
if mac is not None:
|
||||||
self.mac_to_dev[mac] = device
|
self.mac_to_dev[mac] = device
|
||||||
|
@ -654,14 +654,25 @@ class TestComponentsDeviceTracker(unittest.TestCase):
|
|||||||
|
|
||||||
assert len(config) == 4
|
assert len(config) == 4
|
||||||
|
|
||||||
@patch('homeassistant.components.device_tracker.async_log_exception')
|
def test_config_failure(self):
|
||||||
def test_config_failure(self, mock_ex):
|
|
||||||
"""Test that the device tracker see failures."""
|
"""Test that the device tracker see failures."""
|
||||||
with assert_setup_component(0, device_tracker.DOMAIN):
|
with assert_setup_component(0, device_tracker.DOMAIN):
|
||||||
setup_component(self.hass, device_tracker.DOMAIN,
|
setup_component(self.hass, device_tracker.DOMAIN,
|
||||||
{device_tracker.DOMAIN: {
|
{device_tracker.DOMAIN: {
|
||||||
device_tracker.CONF_CONSIDER_HOME: -1}})
|
device_tracker.CONF_CONSIDER_HOME: -1}})
|
||||||
|
|
||||||
|
def test_picture_and_icon_on_see_discovery(self):
|
||||||
|
"""Test that picture and icon are set in initial see."""
|
||||||
|
tracker = device_tracker.DeviceTracker(
|
||||||
|
self.hass, timedelta(seconds=60), False, [])
|
||||||
|
tracker.see(dev_id=11, picture='pic_url', icon='mdi:icon')
|
||||||
|
self.hass.block_till_done()
|
||||||
|
config = device_tracker.load_config(self.yaml_devices, self.hass,
|
||||||
|
timedelta(seconds=0))
|
||||||
|
assert len(config) == 1
|
||||||
|
assert config[0].icon == 'mdi:icon'
|
||||||
|
assert config[0].entity_picture == 'pic_url'
|
||||||
|
|
||||||
|
|
||||||
@asyncio.coroutine
|
@asyncio.coroutine
|
||||||
def test_async_added_to_hass(hass):
|
def test_async_added_to_hass(hass):
|
||||||
@ -691,3 +702,15 @@ def test_async_added_to_hass(hass):
|
|||||||
for key, val in attr.items():
|
for key, val in attr.items():
|
||||||
atr = state.attributes.get(key)
|
atr = state.attributes.get(key)
|
||||||
assert atr == val, "{}={} expected: {}".format(key, atr, val)
|
assert atr == val, "{}={} expected: {}".format(key, atr, val)
|
||||||
|
|
||||||
|
|
||||||
|
@asyncio.coroutine
|
||||||
|
def test_bad_platform(hass):
|
||||||
|
"""Test bad platform."""
|
||||||
|
config = {
|
||||||
|
'device_tracker': [{
|
||||||
|
'platform': 'bad_platform'
|
||||||
|
}]
|
||||||
|
}
|
||||||
|
with assert_setup_component(0, device_tracker.DOMAIN):
|
||||||
|
assert (yield from device_tracker.async_setup(hass, config))
|
||||||
|
Loading…
x
Reference in New Issue
Block a user