Skip to content

Commit

Permalink
Added entity_id validation to the State class
Browse files Browse the repository at this point in the history
  • Loading branch information
balloob committed Nov 5, 2014
1 parent f9fbb30 commit 904bf44
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions homeassistant/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import logging
import threading
import enum
import re
import datetime as dt
import functools as ft

Expand Down Expand Up @@ -46,6 +47,9 @@
# Number of worker threads
POOL_NUM_THREAD = 4

# Pattern for validating entity IDs (format: <domain>.<entity>)
ENTITY_ID_PATTERN = re.compile(r"^(?P<domain>\w+)\.(?P<entity>\w+)$")


class HomeAssistant(object):
""" Core class to route all communication to right components. """
Expand Down Expand Up @@ -399,6 +403,11 @@ class State(object):
__slots__ = ['entity_id', 'state', 'attributes', 'last_changed']

def __init__(self, entity_id, state, attributes=None, last_changed=None):
if not ENTITY_ID_PATTERN.match(entity_id):
raise InvalidEntityFormatError((
"Invalid entity id encountered: {}. "
"Format should be <domain>.<entity>").format(entity_id))

self.entity_id = entity_id
self.state = state
self.attributes = attributes or {}
Expand Down Expand Up @@ -641,3 +650,7 @@ def run(self):

class HomeAssistantError(Exception):
""" General Home Assistant exception occured. """


class InvalidEntityFormatError(HomeAssistantError):
""" When an invalid formatted entity is encountered. """

0 comments on commit 904bf44

Please sign in to comment.