diff --git a/homeassistant/__init__.py b/homeassistant/__init__.py index 2cd4c1f228f9b..2a72a8ce797d4 100644 --- a/homeassistant/__init__.py +++ b/homeassistant/__init__.py @@ -15,8 +15,6 @@ import datetime as dt import functools as ft -from requests.structures import CaseInsensitiveDict - from homeassistant.const import ( EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, 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. """ def __init__(self, bus): - self._states = CaseInsensitiveDict() + self._states = {} self._bus = bus self._lock = threading.Lock() @@ -520,7 +518,7 @@ def entity_ids(self, domain_filter=None): domain_filter = domain_filter.lower() 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] else: return list(self._states.keys()) @@ -531,7 +529,7 @@ def all(self): def get(self, entity_id): """ 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 return state.copy() if state else None @@ -548,6 +546,8 @@ def get_since(self, point_in_time): def is_state(self, entity_id, state): """ Returns True if entity exists and is specified state. """ + entity_id = entity_id.lower() + return (entity_id in self._states and self._states[entity_id].state == state) @@ -555,6 +555,8 @@ def remove(self, entity_id): """ Removes an entity from the state machine. Returns boolean to indicate if an entity was removed. """ + entity_id = entity_id.lower() + with self._lock: return self._states.pop(entity_id, None) is not None @@ -566,7 +568,7 @@ def set(self, entity_id, new_state, attributes=None): If you just update the attributes and not the state, last changed will not be affected. """ - + entity_id = entity_id.lower() new_state = str(new_state) attributes = attributes or {} @@ -581,8 +583,8 @@ def set(self, entity_id, new_state, attributes=None): if not (same_state and same_attr): last_changed = old_state.last_changed if same_state else None - state = self._states[entity_id] = \ - State(entity_id, new_state, attributes, last_changed) + state = State(entity_id, new_state, attributes, last_changed) + self._states[entity_id] = state event_data = {'entity_id': entity_id, 'new_state': state} @@ -612,7 +614,7 @@ def track_change(self, entity_ids, action, from_state=None, to_state=None): @ft.wraps(action) def state_listener(event): """ 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 \ _matcher(event.data['old_state'].state, from_state) and \ _matcher(event.data['new_state'].state, to_state): diff --git a/homeassistant/components/group.py b/homeassistant/components/group.py index bcf7f3fe8b4a7..be98b76f9bb1b 100644 --- a/homeassistant/components/group.py +++ b/homeassistant/components/group.py @@ -6,6 +6,7 @@ """ import homeassistant as ha +from homeassistant.helpers import generate_entity_id import homeassistant.util as util from homeassistant.const import ( ATTR_ENTITY_ID, ATTR_FRIENDLY_NAME, STATE_ON, STATE_OFF, @@ -103,9 +104,7 @@ def __init__(self, hass, name, entity_ids=None, user_defined=True): self.name = name self.user_defined = user_defined - self.entity_id = util.ensure_unique_string( - ENTITY_ID_FORMAT.format(util.slugify(name)), - hass.states.entity_ids(DOMAIN)) + self.entity_id = generate_entity_id(ENTITY_ID_FORMAT, name, hass=hass) self.tracking = [] self.group_on, self.group_off = None, None diff --git a/homeassistant/helpers.py b/homeassistant/helpers.py index 4fbffaf1df7b5..412ed3696e9f6 100644 --- a/homeassistant/helpers.py +++ b/homeassistant/helpers.py @@ -21,7 +21,7 @@ def generate_entity_id(entity_id_format, name, current_ids=None, hass=None): current_ids = hass.states.entity_ids() 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): diff --git a/tests/test_core.py b/tests/test_core.py index d3d97e0b8d4cd..61ac776fba853 100644 --- a/tests/test_core.py +++ b/tests/test_core.py @@ -233,18 +233,18 @@ def test_entity_ids(self): """ Test get_entity_ids method. """ ent_ids = self.states.entity_ids() self.assertEqual(2, len(ent_ids)) - self.assertTrue('light.Bowl' in ent_ids) - self.assertTrue('switch.AC' in ent_ids) + self.assertTrue('light.bowl' in ent_ids) + self.assertTrue('switch.ac' in ent_ids) ent_ids = self.states.entity_ids('light') self.assertEqual(1, len(ent_ids)) - self.assertTrue('light.Bowl' in ent_ids) + self.assertTrue('light.bowl' in ent_ids) def test_remove(self): """ Test remove method. """ - self.assertTrue('light.Bowl' in self.states.entity_ids()) - self.assertTrue(self.states.remove('light.Bowl')) - self.assertFalse('light.Bowl' in self.states.entity_ids()) + self.assertTrue('light.bowl' in self.states.entity_ids()) + self.assertTrue(self.states.remove('light.bowl')) + self.assertFalse('light.bowl' in self.states.entity_ids()) # If it does not exist, we should get False self.assertFalse(self.states.remove('light.Bowl'))