mirror of
https://github.com/home-assistant/core.git
synced 2025-07-19 11:17:21 +00:00
Refactored zone creation based on code review feedback, enhanced configuration
This commit is contained in:
parent
ed872f6054
commit
5a25c74276
@ -23,23 +23,28 @@ BEACON_DEV_ID = 'beacon'
|
|||||||
|
|
||||||
LOCATION_TOPIC = 'owntracks/+/+'
|
LOCATION_TOPIC = 'owntracks/+/+'
|
||||||
EVENT_TOPIC = 'owntracks/+/+/event'
|
EVENT_TOPIC = 'owntracks/+/+/event'
|
||||||
WAYPOINT_TOPIC = 'owntracks/{}/+/waypoint'
|
WAYPOINT_TOPIC = 'owntracks/{}/{}/waypoint'
|
||||||
|
|
||||||
_LOGGER = logging.getLogger(__name__)
|
_LOGGER = logging.getLogger(__name__)
|
||||||
|
|
||||||
LOCK = threading.Lock()
|
LOCK = threading.Lock()
|
||||||
|
|
||||||
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
||||||
CONF_WAYPOINT_IMPORT_USER = 'waypoint_import_user'
|
CONF_WAYPOINT_IMPORT = 'waypoints'
|
||||||
|
CONF_WAYPOINT_WHITELIST = 'waypoint_whitelist'
|
||||||
|
|
||||||
VALIDATE_LOCATION = 'location'
|
VALIDATE_LOCATION = 'location'
|
||||||
VALIDATE_TRANSITION = 'transition'
|
VALIDATE_TRANSITION = 'transition'
|
||||||
|
|
||||||
|
WAYPOINT_LAT_KEY = 'lat'
|
||||||
|
WAYPOINT_LON_KEY = 'lon'
|
||||||
|
|
||||||
|
|
||||||
def setup_scanner(hass, config, see):
|
def setup_scanner(hass, config, see):
|
||||||
"""Setup an OwnTracks tracker."""
|
"""Setup an OwnTracks tracker."""
|
||||||
max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
|
max_gps_accuracy = config.get(CONF_MAX_GPS_ACCURACY)
|
||||||
waypoint_import_user = config.get(CONF_WAYPOINT_IMPORT_USER)
|
waypoint_import = config.get(CONF_WAYPOINT_IMPORT, True)
|
||||||
|
waypoint_whitelist = config.get(CONF_WAYPOINT_WHITELIST)
|
||||||
|
|
||||||
def validate_payload(payload, data_type):
|
def validate_payload(payload, data_type):
|
||||||
"""Validate OwnTracks payload."""
|
"""Validate OwnTracks payload."""
|
||||||
@ -198,10 +203,12 @@ def setup_scanner(hass, config, see):
|
|||||||
_LOGGER.info("Got %d waypoints from %s", len(wayps), topic)
|
_LOGGER.info("Got %d waypoints from %s", len(wayps), topic)
|
||||||
for wayp in wayps:
|
for wayp in wayps:
|
||||||
name = wayp['desc']
|
name = wayp['desc']
|
||||||
lat = wayp['lat']
|
lat = wayp[WAYPOINT_LAT_KEY]
|
||||||
lon = wayp['lon']
|
lon = wayp[WAYPOINT_LON_KEY]
|
||||||
rad = wayp['rad']
|
rad = wayp['rad']
|
||||||
zone_comp.add_zone(hass, name, lat, lon, rad)
|
zone = zone_comp.Zone(hass, name, lat, lon, rad,
|
||||||
|
zone_comp.ICON_IMPORT, False, True)
|
||||||
|
zone_comp.add_zone(hass, name, zone)
|
||||||
|
|
||||||
def see_beacons(dev_id, kwargs_param):
|
def see_beacons(dev_id, kwargs_param):
|
||||||
"""Set active beacons to the current location."""
|
"""Set active beacons to the current location."""
|
||||||
@ -216,8 +223,14 @@ def setup_scanner(hass, config, see):
|
|||||||
mqtt.subscribe(hass, LOCATION_TOPIC, owntracks_location_update, 1)
|
mqtt.subscribe(hass, LOCATION_TOPIC, owntracks_location_update, 1)
|
||||||
mqtt.subscribe(hass, EVENT_TOPIC, owntracks_event_update, 1)
|
mqtt.subscribe(hass, EVENT_TOPIC, owntracks_event_update, 1)
|
||||||
|
|
||||||
if waypoint_import_user is not None:
|
if waypoint_import:
|
||||||
mqtt.subscribe(hass, WAYPOINT_TOPIC.format(waypoint_import_user),
|
if waypoint_whitelist is None:
|
||||||
|
mqtt.subscribe(hass, WAYPOINT_TOPIC.format('+', '+'),
|
||||||
|
owntracks_waypoint_update, 1)
|
||||||
|
else:
|
||||||
|
for whitelist_user in waypoint_whitelist:
|
||||||
|
mqtt.subscribe(hass, WAYPOINT_TOPIC.format(whitelist_user,
|
||||||
|
'+'),
|
||||||
owntracks_waypoint_update, 1)
|
owntracks_waypoint_update, 1)
|
||||||
|
|
||||||
return True
|
return True
|
||||||
@ -231,7 +244,7 @@ def _parse_see_args(topic, data):
|
|||||||
kwargs = {
|
kwargs = {
|
||||||
'dev_id': dev_id,
|
'dev_id': dev_id,
|
||||||
'host_name': host_name,
|
'host_name': host_name,
|
||||||
'gps': (data['lat'], data['lon'])
|
'gps': (data[WAYPOINT_LAT_KEY], data[WAYPOINT_LON_KEY])
|
||||||
}
|
}
|
||||||
if 'acc' in data:
|
if 'acc' in data:
|
||||||
kwargs['gps_accuracy'] = data['acc']
|
kwargs['gps_accuracy'] = data['acc']
|
||||||
|
@ -92,17 +92,16 @@ def setup(hass, config):
|
|||||||
'Each zone needs a latitude and longitude.')
|
'Each zone needs a latitude and longitude.')
|
||||||
continue
|
continue
|
||||||
|
|
||||||
zone = Zone(hass, name, latitude, longitude, radius, icon,
|
zone = Zone(hass, name, latitude, longitude, radius,
|
||||||
passive, False)
|
icon, passive, False)
|
||||||
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
|
add_zone(hass, name, zone, entities)
|
||||||
entities)
|
|
||||||
zone.update_ha_state()
|
|
||||||
entities.add(zone.entity_id)
|
entities.add(zone.entity_id)
|
||||||
|
|
||||||
if ENTITY_ID_HOME not in entities:
|
if ENTITY_ID_HOME not in entities:
|
||||||
zone = Zone(hass, hass.config.location_name, hass.config.latitude,
|
zone = Zone(hass, hass.config.location_name,
|
||||||
hass.config.longitude, DEFAULT_RADIUS, ICON_HOME,
|
hass.config.latitude, hass.config.longitude,
|
||||||
False, False)
|
DEFAULT_RADIUS, ICON_HOME, False, False)
|
||||||
|
add_zone(hass, hass.config.location_name, zone, entities)
|
||||||
zone.entity_id = ENTITY_ID_HOME
|
zone.entity_id = ENTITY_ID_HOME
|
||||||
zone.update_ha_state()
|
zone.update_ha_state()
|
||||||
|
|
||||||
@ -110,20 +109,24 @@ def setup(hass, config):
|
|||||||
|
|
||||||
|
|
||||||
# Add a zone to the existing set
|
# Add a zone to the existing set
|
||||||
def add_zone(hass, name, latitude, longitude, radius):
|
def add_zone(hass, name, zone, entities=None):
|
||||||
"""Add a zone from other components."""
|
"""Add a zone from other components."""
|
||||||
_LOGGER.info("Adding new zone %s", name)
|
_LOGGER.info("Adding new zone %s", name)
|
||||||
entities = set()
|
if entities is None:
|
||||||
|
_entities = set()
|
||||||
|
else:
|
||||||
|
_entities = entities
|
||||||
|
|
||||||
if hass.states.get('zone.' + name) is None:
|
zone_exists = hass.states.get('zone.' + str(name))
|
||||||
zone = Zone(hass, name, latitude, longitude, radius, ICON_IMPORT,
|
if zone_exists is None:
|
||||||
False, True)
|
|
||||||
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
|
zone.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name,
|
||||||
entities)
|
_entities)
|
||||||
zone.update_ha_state()
|
zone.update_ha_state()
|
||||||
entities.add(zone.entity_id)
|
_entities.add(zone.entity_id)
|
||||||
|
return zone
|
||||||
else:
|
else:
|
||||||
_LOGGER.info("Zone already exists")
|
_LOGGER.info("Zone already exists")
|
||||||
|
return zone_exists
|
||||||
|
|
||||||
|
|
||||||
class Zone(Entity):
|
class Zone(Entity):
|
||||||
|
@ -17,7 +17,10 @@ DEVICE = 'phone'
|
|||||||
|
|
||||||
LOCATION_TOPIC = "owntracks/{}/{}".format(USER, DEVICE)
|
LOCATION_TOPIC = "owntracks/{}/{}".format(USER, DEVICE)
|
||||||
EVENT_TOPIC = "owntracks/{}/{}/event".format(USER, DEVICE)
|
EVENT_TOPIC = "owntracks/{}/{}/event".format(USER, DEVICE)
|
||||||
WAYPOINT_TOPIC = 'owntracks/{}/{}/waypoint'.format(USER, DEVICE)
|
WAYPOINT_TOPIC = owntracks.WAYPOINT_TOPIC.format(USER, DEVICE)
|
||||||
|
USER_BLACKLIST = 'ram'
|
||||||
|
WAYPOINT_TOPIC_BLOCKED = owntracks.WAYPOINT_TOPIC.format(USER_BLACKLIST,
|
||||||
|
DEVICE)
|
||||||
|
|
||||||
DEVICE_TRACKER_STATE = "device_tracker.{}_{}".format(USER, DEVICE)
|
DEVICE_TRACKER_STATE = "device_tracker.{}_{}".format(USER, DEVICE)
|
||||||
|
|
||||||
@ -25,7 +28,8 @@ IBEACON_DEVICE = 'keys'
|
|||||||
REGION_TRACKER_STATE = "device_tracker.beacon_{}".format(IBEACON_DEVICE)
|
REGION_TRACKER_STATE = "device_tracker.beacon_{}".format(IBEACON_DEVICE)
|
||||||
|
|
||||||
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
CONF_MAX_GPS_ACCURACY = 'max_gps_accuracy'
|
||||||
CONF_WAYPOINT_IMPORT_USER = 'waypoint_import_user'
|
CONF_WAYPOINT_IMPORT = owntracks.CONF_WAYPOINT_IMPORT
|
||||||
|
CONF_WAYPOINT_WHITELIST = owntracks.CONF_WAYPOINT_WHITELIST
|
||||||
|
|
||||||
LOCATION_MESSAGE = {
|
LOCATION_MESSAGE = {
|
||||||
'batt': 92,
|
'batt': 92,
|
||||||
@ -168,7 +172,8 @@ class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
|||||||
device_tracker.DOMAIN: {
|
device_tracker.DOMAIN: {
|
||||||
CONF_PLATFORM: 'owntracks',
|
CONF_PLATFORM: 'owntracks',
|
||||||
CONF_MAX_GPS_ACCURACY: 200,
|
CONF_MAX_GPS_ACCURACY: 200,
|
||||||
CONF_WAYPOINT_IMPORT_USER: USER
|
CONF_WAYPOINT_IMPORT: True,
|
||||||
|
CONF_WAYPOINT_WHITELIST: ['jon', 'greg']
|
||||||
}}))
|
}}))
|
||||||
|
|
||||||
self.hass.states.set(
|
self.hass.states.set(
|
||||||
@ -565,3 +570,13 @@ class TestDeviceTrackerOwnTracks(unittest.TestCase):
|
|||||||
self.assertTrue(wayp is not None)
|
self.assertTrue(wayp is not None)
|
||||||
wayp = self.hass.states.get('zone.exp_wayp2')
|
wayp = self.hass.states.get('zone.exp_wayp2')
|
||||||
self.assertTrue(wayp is not None)
|
self.assertTrue(wayp is not None)
|
||||||
|
|
||||||
|
def test_waypoint_import_blacklist(self):
|
||||||
|
"""Test import of list of waypoints for blacklisted user."""
|
||||||
|
waypoints_message = WAYPOINTS_EXPORTED_MESSAGE.copy()
|
||||||
|
self.send_message(WAYPOINT_TOPIC_BLOCKED, waypoints_message)
|
||||||
|
# Check if it made it into states
|
||||||
|
wayp = self.hass.states.get('zone.exp_wayp1')
|
||||||
|
self.assertTrue(wayp is None)
|
||||||
|
wayp = self.hass.states.get('zone.exp_wayp2')
|
||||||
|
self.assertTrue(wayp is None)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user