Skip to content

Commit

Permalink
Entity IDs are now always lowercase
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Feb 6, 2015
1 parent e3643b1 commit d053f93
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 19 deletions.
20 changes: 11 additions & 9 deletions homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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()

Expand All @@ -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())
Expand All @@ -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
Expand All @@ -548,13 +546,17 @@ 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)

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

Expand All @@ -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 {}

Expand All @@ -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}

Expand Down Expand Up @@ -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):
Expand Down
5 changes: 2 additions & 3 deletions homeassistant/components/group.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion homeassistant/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
12 changes: 6 additions & 6 deletions tests/test_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit d053f93

Please sign in to comment.