Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions homeassistant_api/rawapi.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
"""Module for parent RawWrapper class"""

import os
import re
from datetime import datetime
from typing import Dict, Optional, Tuple, Union

from .const import DATE_FMT
from .errors import MalformedInputError
from .models import Entity


Expand Down Expand Up @@ -72,16 +72,13 @@ def construct_params(params: Dict[str, Optional[str]]) -> str:
"""Custom method for constructing non-standard query strings"""
return "&".join([k if v is None else f"{k}={v}" for k, v in params.items()])

@classmethod
def malformed_id(cls, entity_id: str) -> bool:
"""Checks whether or not a given entity_id is formatted correctly"""
checks = [
" " in entity_id,
"." not in entity_id,
"-" in entity_id,
entity_id.lower() == entity_id,
]
return True in checks
@staticmethod
def format_entity_id(entity_id: str) -> str:
"""Takes in a string and formats it into valid snake_case."""
entity_id = re.sub(r"([A-Z]+)([A-Z][a-z])", r"\1_\2", entity_id)
entity_id = re.sub(r"([a-z\d])([A-Z])", r"\1_\2", entity_id)
entity_id = entity_id.replace("-", "_")
return entity_id.lower()

def prepare_entity_id(
self,
Expand All @@ -100,9 +97,7 @@ def prepare_entity_id(
entity_id = group + "." + slug
elif entity_id is None:
raise ValueError("Neither group and slug or entity_id provided.")
if self.malformed_id(entity_id):
raise MalformedInputError(f"The entity_id, {entity_id!r}, is malformed")
return entity_id
return self.format_entity_id(entity_id)

@staticmethod
def prepare_get_entity_histories_params(
Expand Down