Entity IDs are now always lowercase

This commit is contained in:
Paulus Schoutsen 2015-02-06 00:17:30 -08:00
parent e3643b1faf
commit d053f93419
4 changed files with 20 additions and 19 deletions

View File

@ -15,8 +15,6 @@ import re
import datetime as dt import datetime as dt
import functools as ft import functools as ft
from requests.structures import CaseInsensitiveDict
from homeassistant.const import ( from homeassistant.const import (
EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
SERVICE_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED, SERVICE_HOMEASSISTANT_STOP, EVENT_TIME_CHANGED, EVENT_STATE_CHANGED,
@ -510,7 +508,7 @@ class StateMachine(object):
""" Helper class that tracks the state of different entities. """ """ Helper class that tracks the state of different entities. """
def __init__(self, bus): def __init__(self, bus):
self._states = CaseInsensitiveDict() self._states = {}
self._bus = bus self._bus = bus
self._lock = threading.Lock() self._lock = threading.Lock()
@ -520,7 +518,7 @@ class StateMachine(object):
domain_filter = domain_filter.lower() domain_filter = domain_filter.lower()
return [state.entity_id for key, state return [state.entity_id for key, state
in self._states.lower_items() in self._states.items()
if util.split_entity_id(key)[0] == domain_filter] if util.split_entity_id(key)[0] == domain_filter]
else: else:
return list(self._states.keys()) return list(self._states.keys())
@ -531,7 +529,7 @@ class StateMachine(object):
def get(self, entity_id): def get(self, entity_id):
""" Returns the state of the specified entity. """ """ Returns the state of the specified entity. """
state = self._states.get(entity_id) state = self._states.get(entity_id.lower())
# Make a copy so people won't mutate the state # Make a copy so people won't mutate the state
return state.copy() if state else None return state.copy() if state else None
@ -548,6 +546,8 @@ class StateMachine(object):
def is_state(self, entity_id, state): def is_state(self, entity_id, state):
""" Returns True if entity exists and is specified state. """ """ Returns True if entity exists and is specified state. """
entity_id = entity_id.lower()
return (entity_id in self._states and return (entity_id in self._states and
self._states[entity_id].state == state) self._states[entity_id].state == state)
@ -555,6 +555,8 @@ class StateMachine(object):
""" Removes an entity from the state machine. """ Removes an entity from the state machine.
Returns boolean to indicate if an entity was removed. """ Returns boolean to indicate if an entity was removed. """
entity_id = entity_id.lower()
with self._lock: with self._lock:
return self._states.pop(entity_id, None) is not None return self._states.pop(entity_id, None) is not None
@ -566,7 +568,7 @@ class StateMachine(object):
If you just update the attributes and not the state, last changed will If you just update the attributes and not the state, last changed will
not be affected. not be affected.
""" """
entity_id = entity_id.lower()
new_state = str(new_state) new_state = str(new_state)
attributes = attributes or {} attributes = attributes or {}
@ -581,8 +583,8 @@ class StateMachine(object):
if not (same_state and same_attr): if not (same_state and same_attr):
last_changed = old_state.last_changed if same_state else None last_changed = old_state.last_changed if same_state else None
state = self._states[entity_id] = \ state = State(entity_id, new_state, attributes, last_changed)
State(entity_id, new_state, attributes, last_changed) self._states[entity_id] = state
event_data = {'entity_id': entity_id, 'new_state': state} event_data = {'entity_id': entity_id, 'new_state': state}
@ -612,7 +614,7 @@ class StateMachine(object):
@ft.wraps(action) @ft.wraps(action)
def state_listener(event): def state_listener(event):
""" The listener that listens for specific state changes. """ """ The listener that listens for specific state changes. """
if event.data['entity_id'].lower() in entity_ids and \ if event.data['entity_id'] in entity_ids and \
'old_state' in event.data and \ 'old_state' in event.data and \
_matcher(event.data['old_state'].state, from_state) and \ _matcher(event.data['old_state'].state, from_state) and \
_matcher(event.data['new_state'].state, to_state): _matcher(event.data['new_state'].state, to_state):

View File

@ -6,6 +6,7 @@ Provides functionality to group devices that can be turned on or off.
""" """
import homeassistant as ha import homeassistant as ha
from homeassistant.helpers import generate_entity_id
import homeassistant.util as util import homeassistant.util as util
from homeassistant.const import ( from homeassistant.const import (
ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF, ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF,
@ -103,9 +104,7 @@ class Group(object):
self.name = name self.name = name
self.user_defined = user_defined self.user_defined = user_defined
self.entity_id = util.ensure_unique_string( self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass)
ENTITY_ID_FORMAT.format(util.slugify(name)),
hass.states.entity_ids(DOMAIN))
self.tracking = [] self.tracking = []
self.group_on, self.group_off = None, None self.group_on, self.group_off = None, None

View File

@ -21,7 +21,7 @@ def generate_entity_id(entity_id_format, name, current_ids=None, hass=None):
current_ids = hass.states.entity_ids() current_ids = hass.states.entity_ids()
return ensure_unique_string( return ensure_unique_string(
entity_id_format.format(slugify(name)), current_ids) entity_id_format.format(slugify(name.lower())), current_ids)
def extract_entity_ids(hass, service): def extract_entity_ids(hass, service):

View File

@ -233,18 +233,18 @@ class TestStateMachine(unittest.TestCase):
""" Test get_entity_ids method. """ """ Test get_entity_ids method. """
ent_ids = self.states.entity_ids() ent_ids = self.states.entity_ids()
self.assertEqual(2, len(ent_ids)) self.assertEqual(2, len(ent_ids))
self.assertTrue('light.Bowl' in ent_ids) self.assertTrue('light.bowl' in ent_ids)
self.assertTrue('switch.AC' in ent_ids) self.assertTrue('switch.ac' in ent_ids)
ent_ids = self.states.entity_ids('light') ent_ids = self.states.entity_ids('light')
self.assertEqual(1, len(ent_ids)) self.assertEqual(1, len(ent_ids))
self.assertTrue('light.Bowl' in ent_ids) self.assertTrue('light.bowl' in ent_ids)
def test_remove(self): def test_remove(self):
""" Test remove method. """ """ Test remove method. """
self.assertTrue('light.Bowl' in self.states.entity_ids()) self.assertTrue('light.bowl' in self.states.entity_ids())
self.assertTrue(self.states.remove('light.Bowl')) self.assertTrue(self.states.remove('light.bowl'))
self.assertFalse('light.Bowl' in self.states.entity_ids()) self.assertFalse('light.bowl' in self.states.entity_ids())
# If it does not exist, we should get False # If it does not exist, we should get False
self.assertFalse(self.states.remove('light.Bowl')) self.assertFalse(self.states.remove('light.Bowl'))