mirror of
https://github.com/home-assistant/core.git
synced 2025-07-16 09:47:13 +00:00
Added entity_id validation to the State class
This commit is contained in:
parent
f9fbb30fc0
commit
904bf4493e
@ -12,6 +12,7 @@ import time
|
|||||||
import logging
|
import logging
|
||||||
import threading
|
import threading
|
||||||
import enum
|
import enum
|
||||||
|
import re
|
||||||
import datetime as dt
|
import datetime as dt
|
||||||
import functools as ft
|
import functools as ft
|
||||||
|
|
||||||
@ -46,6 +47,9 @@ TIMER_INTERVAL = 10 # seconds
|
|||||||
# Number of worker threads
|
# Number of worker threads
|
||||||
POOL_NUM_THREAD = 4
|
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):
|
class HomeAssistant(object):
|
||||||
""" Core class to route all communication to right components. """
|
""" Core class to route all communication to right components. """
|
||||||
@ -399,6 +403,11 @@ class State(object):
|
|||||||
__slots__ = ['entity_id', 'state', 'attributes', 'last_changed']
|
__slots__ = ['entity_id', 'state', 'attributes', 'last_changed']
|
||||||
|
|
||||||
def __init__(self, entity_id, state, attributes=None, last_changed=None):
|
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.entity_id = entity_id
|
||||||
self.state = state
|
self.state = state
|
||||||
self.attributes = attributes or {}
|
self.attributes = attributes or {}
|
||||||
@ -641,3 +650,7 @@ class Timer(threading.Thread):
|
|||||||
|
|
||||||
class HomeAssistantError(Exception):
|
class HomeAssistantError(Exception):
|
||||||
""" General Home Assistant exception occured. """
|
""" General Home Assistant exception occured. """
|
||||||
|
|
||||||
|
|
||||||
|
class InvalidEntityFormatError(HomeAssistantError):
|
||||||
|
""" When an invalid formatted entity is encountered. """
|
||||||
|
Loading…
x
Reference in New Issue
Block a user