1
mirror of https://github.com/home-assistant/core synced 2024-10-01 05:30:36 +02:00

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 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 @@ class StateMachine(object):
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 @@ class StateMachine(object):
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 @@ class StateMachine(object):
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 @@ class StateMachine(object):
""" 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 @@ class StateMachine(object):
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 @@ class StateMachine(object):
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 @@ class StateMachine(object):
@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):

View File

@ -6,6 +6,7 @@ Provides functionality to group devices that can be turned on or off.
"""
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 @@ class Group(object):
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

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()
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):

View File

@ -233,18 +233,18 @@ class TestStateMachine(unittest.TestCase):
""" 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'))