Move unit system to util (#2763)

This commit is contained in:
Paulus Schoutsen 2016-08-08 20:42:25 -07:00 committed by GitHub
parent 640a8b5a7f
commit 0b7b0e54ba
18 changed files with 35 additions and 56 deletions

View File

@ -12,8 +12,6 @@ from typing import Any, Optional, Dict
import voluptuous as vol import voluptuous as vol
from homeassistant.helpers.typing import HomeAssistantType
import homeassistant.components as core_components import homeassistant.components as core_components
from homeassistant.components import group, persistent_notification from homeassistant.components import group, persistent_notification
import homeassistant.config as conf_util import homeassistant.config as conf_util
@ -218,7 +216,7 @@ def prepare_setup_platform(hass: core.HomeAssistant, config, domain: str,
# pylint: disable=too-many-branches, too-many-statements, too-many-arguments # pylint: disable=too-many-branches, too-many-statements, too-many-arguments
def from_config_dict(config: Dict[str, Any], def from_config_dict(config: Dict[str, Any],
hass: Optional[HomeAssistantType]=None, hass: Optional[core.HomeAssistant]=None,
config_dir: Optional[str]=None, config_dir: Optional[str]=None,
enable_log: bool=True, enable_log: bool=True,
verbose: bool=False, verbose: bool=False,

View File

@ -16,13 +16,13 @@ from typing import Any, Union, Optional, List
import voluptuous as vol import voluptuous as vol
from homeassistant.helpers.typing import (ConfigType, QueryType, from homeassistant.core import HomeAssistant
HomeAssistantType)
import homeassistant.util.dt as dt_util
from homeassistant.const import (EVENT_HOMEASSISTANT_START, from homeassistant.const import (EVENT_HOMEASSISTANT_START,
EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED, EVENT_HOMEASSISTANT_STOP, EVENT_STATE_CHANGED,
EVENT_TIME_CHANGED, MATCH_ALL) EVENT_TIME_CHANGED, MATCH_ALL)
from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.helpers.typing import ConfigType, QueryType
import homeassistant.util.dt as dt_util
DOMAIN = "recorder" DOMAIN = "recorder"
@ -95,7 +95,7 @@ def run_information(point_in_time: Optional[datetime]=None):
(recorder_runs.end > point_in_time)).first() (recorder_runs.end > point_in_time)).first()
def setup(hass: HomeAssistantType, config: ConfigType) -> bool: def setup(hass: HomeAssistant, config: ConfigType) -> bool:
"""Setup the recorder.""" """Setup the recorder."""
# pylint: disable=global-statement # pylint: disable=global-statement
global _INSTANCE global _INSTANCE
@ -155,7 +155,7 @@ class Recorder(threading.Thread):
"""A threaded recorder class.""" """A threaded recorder class."""
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
def __init__(self, hass: HomeAssistantType, purge_days: int, uri: str) \ def __init__(self, hass: HomeAssistant, purge_days: int, uri: str) \
-> None: -> None:
"""Initialize the recorder.""" """Initialize the recorder."""
threading.Thread.__init__(self) threading.Thread.__init__(self)

View File

@ -18,9 +18,9 @@ from homeassistant.core import valid_entity_id
from homeassistant.exceptions import HomeAssistantError from homeassistant.exceptions import HomeAssistantError
from homeassistant.util.yaml import load_yaml from homeassistant.util.yaml import load_yaml
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
from homeassistant.helpers.unit_system import (IMPERIAL_SYSTEM, METRIC_SYSTEM)
from homeassistant.helpers.entity import set_customize from homeassistant.helpers.entity import set_customize
from homeassistant.util import dt as date_util, location as loc_util from homeassistant.util import dt as date_util, location as loc_util
from homeassistant.util.unit_system import IMPERIAL_SYSTEM, METRIC_SYSTEM
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)

View File

@ -29,11 +29,10 @@ from homeassistant.const import (
SERVICE_HOMEASSISTANT_RESTART, SERVICE_HOMEASSISTANT_STOP, __version__) SERVICE_HOMEASSISTANT_RESTART, SERVICE_HOMEASSISTANT_STOP, __version__)
from homeassistant.exceptions import ( from homeassistant.exceptions import (
HomeAssistantError, InvalidEntityFormatError) HomeAssistantError, InvalidEntityFormatError)
from homeassistant.helpers.typing import UnitSystemType # NOQA
from homeassistant.helpers.unit_system import METRIC_SYSTEM
import homeassistant.util as util import homeassistant.util as util
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
import homeassistant.util.location as location import homeassistant.util.location as location
from homeassistant.util.unit_system import UnitSystem, METRIC_SYSTEM # NOQA
DOMAIN = "homeassistant" DOMAIN = "homeassistant"
@ -731,7 +730,7 @@ class Config(object):
self.elevation = None # type: Optional[int] self.elevation = None # type: Optional[int]
self.location_name = None # type: Optional[str] self.location_name = None # type: Optional[str]
self.time_zone = None # type: Optional[str] self.time_zone = None # type: Optional[str]
self.units = METRIC_SYSTEM # type: UnitSystemType self.units = METRIC_SYSTEM # type: UnitSystem
# If True, pip install is skipped for requirements on startup # If True, pip install is skipped for requirements on startup
self.skip_pip = False # type: bool self.skip_pip = False # type: bool

View File

@ -3,8 +3,9 @@ from datetime import timedelta
import logging import logging
import sys import sys
from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.helpers.typing import ConfigType
from homeassistant.core import HomeAssistant
from homeassistant.components import ( from homeassistant.components import (
zone as zone_cmp, sun as sun_cmp) zone as zone_cmp, sun as sun_cmp)
from homeassistant.const import ( from homeassistant.const import (
@ -42,7 +43,7 @@ def and_from_config(config: ConfigType, config_validation: bool=True):
config = cv.AND_CONDITION_SCHEMA(config) config = cv.AND_CONDITION_SCHEMA(config)
checks = [from_config(entry) for entry in config['conditions']] checks = [from_config(entry) for entry in config['conditions']]
def if_and_condition(hass: HomeAssistantType, def if_and_condition(hass: HomeAssistant,
variables=None) -> bool: variables=None) -> bool:
"""Test and condition.""" """Test and condition."""
for check in checks: for check in checks:
@ -64,7 +65,7 @@ def or_from_config(config: ConfigType, config_validation: bool=True):
config = cv.OR_CONDITION_SCHEMA(config) config = cv.OR_CONDITION_SCHEMA(config)
checks = [from_config(entry) for entry in config['conditions']] checks = [from_config(entry) for entry in config['conditions']]
def if_or_condition(hass: HomeAssistantType, def if_or_condition(hass: HomeAssistant,
variables=None) -> bool: variables=None) -> bool:
"""Test and condition.""" """Test and condition."""
for check in checks: for check in checks:
@ -80,7 +81,7 @@ def or_from_config(config: ConfigType, config_validation: bool=True):
# pylint: disable=too-many-arguments # pylint: disable=too-many-arguments
def numeric_state(hass: HomeAssistantType, entity, below=None, above=None, def numeric_state(hass: HomeAssistant, entity, below=None, above=None,
value_template=None, variables=None): value_template=None, variables=None):
"""Test a numeric state condition.""" """Test a numeric state condition."""
if isinstance(entity, str): if isinstance(entity, str):

View File

@ -8,11 +8,10 @@ from homeassistant.const import (
ATTR_UNIT_OF_MEASUREMENT, DEVICE_DEFAULT_NAME, STATE_OFF, STATE_ON, ATTR_UNIT_OF_MEASUREMENT, DEVICE_DEFAULT_NAME, STATE_OFF, STATE_ON,
STATE_UNAVAILABLE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT, STATE_UNAVAILABLE, STATE_UNKNOWN, TEMP_CELSIUS, TEMP_FAHRENHEIT,
ATTR_ENTITY_PICTURE) ATTR_ENTITY_PICTURE)
from homeassistant.core import HomeAssistant
from homeassistant.exceptions import NoEntitySpecifiedError from homeassistant.exceptions import NoEntitySpecifiedError
from homeassistant.util import ensure_unique_string, slugify from homeassistant.util import ensure_unique_string, slugify
from homeassistant.helpers.typing import HomeAssistantType
# Entity attributes that we will overwrite # Entity attributes that we will overwrite
_OVERWRITE = {} # type: Dict[str, Any] _OVERWRITE = {} # type: Dict[str, Any]
@ -21,7 +20,7 @@ _LOGGER = logging.getLogger(__name__)
def generate_entity_id(entity_id_format: str, name: Optional[str], def generate_entity_id(entity_id_format: str, name: Optional[str],
current_ids: Optional[List[str]]=None, current_ids: Optional[List[str]]=None,
hass: Optional[HomeAssistantType]=None) -> str: hass: Optional[HomeAssistant]=None) -> str:
"""Generate a unique entity ID based on given entity IDs or used IDs.""" """Generate a unique entity ID based on given entity IDs or used IDs."""
name = (name or DEVICE_DEFAULT_NAME).lower() name = (name or DEVICE_DEFAULT_NAME).lower()
if current_ids is None: if current_ids is None:
@ -137,7 +136,7 @@ class Entity(object):
# are used to perform a very specific function. Overwriting these may # are used to perform a very specific function. Overwriting these may
# produce undesirable effects in the entity's operation. # produce undesirable effects in the entity's operation.
hass = None # type: Optional[HomeAssistantType] hass = None # type: Optional[HomeAssistant]
def update_ha_state(self, force_refresh=False): def update_ha_state(self, force_refresh=False):
"""Update Home Assistant with current state of entity. """Update Home Assistant with current state of entity.

View File

@ -3,11 +3,11 @@ import functools
# pylint: disable=unused-import # pylint: disable=unused-import
from typing import Optional # NOQA from typing import Optional # NOQA
from homeassistant.helpers.typing import HomeAssistantType # NOQA
from homeassistant.core import HomeAssistant # NOQA
from homeassistant.helpers import event from homeassistant.helpers import event
HASS = None # type: Optional[HomeAssistantType] HASS = None # type: Optional[HomeAssistant]
def track_state_change(entity_ids, from_state=None, to_state=None): def track_state_change(entity_ids, from_state=None, to_state=None):

View File

@ -2,18 +2,17 @@
import logging import logging
import threading import threading
from itertools import islice from itertools import islice
from typing import Optional, Sequence from typing import Optional, Sequence
import voluptuous as vol import voluptuous as vol
from homeassistant.helpers.typing import ConfigType, HomeAssistantType from homeassistant.core import HomeAssistant
import homeassistant.util.dt as date_util
from homeassistant.const import EVENT_TIME_CHANGED, CONF_CONDITION from homeassistant.const import EVENT_TIME_CHANGED, CONF_CONDITION
from homeassistant.helpers import (
service, condition, template, config_validation as cv)
from homeassistant.helpers.event import track_point_in_utc_time from homeassistant.helpers.event import track_point_in_utc_time
from homeassistant.helpers import service, condition, template from homeassistant.helpers.typing import ConfigType
import homeassistant.helpers.config_validation as cv import homeassistant.util.dt as date_util
_LOGGER = logging.getLogger(__name__) _LOGGER = logging.getLogger(__name__)
@ -26,7 +25,7 @@ CONF_EVENT_DATA = "event_data"
CONF_DELAY = "delay" CONF_DELAY = "delay"
def call_from_config(hass: HomeAssistantType, config: ConfigType, def call_from_config(hass: HomeAssistant, config: ConfigType,
variables: Optional[Sequence]=None) -> None: variables: Optional[Sequence]=None) -> None:
"""Call a script based on a config entry.""" """Call a script based on a config entry."""
Script(hass, config).run(variables) Script(hass, config).run(variables)
@ -36,7 +35,7 @@ class Script():
"""Representation of a script.""" """Representation of a script."""
# pylint: disable=too-many-instance-attributes # pylint: disable=too-many-instance-attributes
def __init__(self, hass: HomeAssistantType, sequence, name: str=None, def __init__(self, hass: HomeAssistant, sequence, name: str=None,
change_listener=None) -> None: change_listener=None) -> None:
"""Initialize the script.""" """Initialize the script."""
self.hass = hass self.hass = hass

View File

@ -1,21 +1,19 @@
"""Service calling related helpers.""" """Service calling related helpers."""
import functools import functools
import logging import logging
# pylint: disable=unused-import # pylint: disable=unused-import
from typing import Optional # NOQA from typing import Optional # NOQA
import voluptuous as vol import voluptuous as vol
from homeassistant.helpers.typing import HomeAssistantType # NOQA
from homeassistant.const import ATTR_ENTITY_ID from homeassistant.const import ATTR_ENTITY_ID
from homeassistant.core import HomeAssistant # NOQA
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template from homeassistant.helpers import template
from homeassistant.loader import get_component from homeassistant.loader import get_component
import homeassistant.helpers.config_validation as cv import homeassistant.helpers.config_validation as cv
HASS = None # type: Optional[HomeAssistantType] HASS = None # type: Optional[HomeAssistant]
CONF_SERVICE = 'service' CONF_SERVICE = 'service'
CONF_SERVICE_TEMPLATE = 'service_template' CONF_SERVICE_TEMPLATE = 'service_template'

View File

@ -8,22 +8,9 @@ try:
except ImportError: except ImportError:
NewType = None NewType = None
# HACK: mypy/pytype will import, other interpreters will not; this is to avoid
# circular dependencies where the type is needed.
# All homeassistant types should be imported this way.
# Documentation
# http://mypy.readthedocs.io/en/latest/common_issues.html#import-cycles
# pylint: disable=using-constant-test,unused-import
if False:
from homeassistant.core import HomeAssistant # NOQA
from homeassistant.helpers.unit_system import UnitSystem # NOQA
# ENDHACK
# pylint: disable=invalid-name # pylint: disable=invalid-name
if NewType: if NewType:
ConfigType = NewType('ConfigType', Dict[str, Any]) ConfigType = NewType('ConfigType', Dict[str, Any])
HomeAssistantType = NewType('HomeAssistantType', 'HomeAssistant')
UnitSystemType = NewType('UnitSystemType', 'UnitSystem')
# Custom type for recorder Queries # Custom type for recorder Queries
QueryType = NewType('QueryType', Any) QueryType = NewType('QueryType', Any)
@ -32,8 +19,6 @@ if NewType:
# pylint: disable=invalid-name # pylint: disable=invalid-name
else: else:
ConfigType = Dict[str, Any] # type: ignore ConfigType = Dict[str, Any] # type: ignore
HomeAssistantType = 'HomeAssistant' # type: ignore
UnitSystemType = 'UnitSystemType' # type: ignore
# Custom type for recorder Queries # Custom type for recorder Queries
QueryType = Any # type: ignore QueryType = Any # type: ignore

View File

@ -6,7 +6,7 @@ from unittest import mock
from homeassistant import core as ha, loader from homeassistant import core as ha, loader
from homeassistant.bootstrap import _setup_component from homeassistant.bootstrap import _setup_component
from homeassistant.helpers.entity import ToggleEntity from homeassistant.helpers.entity import ToggleEntity
from homeassistant.helpers.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM
import homeassistant.util.dt as date_util import homeassistant.util.dt as date_util
from homeassistant.const import ( from homeassistant.const import (
STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED, STATE_ON, STATE_OFF, DEVICE_DEFAULT_NAME, EVENT_TIME_CHANGED,

View File

@ -1,7 +1,7 @@
"""The tests for the demo hvac.""" """The tests for the demo hvac."""
import unittest import unittest
from homeassistant.helpers.unit_system import ( from homeassistant.util.unit_system import (
METRIC_SYSTEM, METRIC_SYSTEM,
) )
from homeassistant.components import hvac from homeassistant.components import hvac

View File

@ -1,7 +1,7 @@
"""The tests for the demo thermostat.""" """The tests for the demo thermostat."""
import unittest import unittest
from homeassistant.helpers.unit_system import ( from homeassistant.util.unit_system import (
METRIC_SYSTEM, METRIC_SYSTEM,
) )
from homeassistant.components import thermostat from homeassistant.components import thermostat

View File

@ -13,7 +13,7 @@ from homeassistant.const import (
STATE_OFF, STATE_OFF,
TEMP_CELSIUS, TEMP_CELSIUS,
) )
from homeassistant.helpers.unit_system import METRIC_SYSTEM from homeassistant.util.unit_system import METRIC_SYSTEM
from homeassistant.components import thermostat from homeassistant.components import thermostat
from tests.common import get_test_home_assistant from tests.common import get_test_home_assistant

View File

@ -6,7 +6,7 @@ from unittest.mock import patch
from homeassistant.components import group from homeassistant.components import group
from homeassistant.exceptions import TemplateError from homeassistant.exceptions import TemplateError
from homeassistant.helpers import template from homeassistant.helpers import template
from homeassistant.helpers.unit_system import UnitSystem from homeassistant.util.unit_system import UnitSystem
from homeassistant.const import ( from homeassistant.const import (
LENGTH_METERS, LENGTH_METERS,
TEMP_CELSIUS, TEMP_CELSIUS,

View File

@ -15,7 +15,7 @@ import homeassistant.core as ha
from homeassistant.exceptions import ( from homeassistant.exceptions import (
HomeAssistantError, InvalidEntityFormatError) HomeAssistantError, InvalidEntityFormatError)
import homeassistant.util.dt as dt_util import homeassistant.util.dt as dt_util
from homeassistant.helpers.unit_system import (METRIC_SYSTEM) from homeassistant.util.unit_system import (METRIC_SYSTEM)
from homeassistant.const import ( from homeassistant.const import (
__version__, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP, __version__, EVENT_HOMEASSISTANT_START, EVENT_HOMEASSISTANT_STOP,
EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM) EVENT_STATE_CHANGED, ATTR_FRIENDLY_NAME, CONF_UNIT_SYSTEM)

View File

@ -1,7 +1,7 @@
"""Test the unit system helper.""" """Test the unit system helper."""
import unittest import unittest
from homeassistant.helpers.unit_system import ( from homeassistant.util.unit_system import (
UnitSystem, UnitSystem,
METRIC_SYSTEM, METRIC_SYSTEM,
IMPERIAL_SYSTEM, IMPERIAL_SYSTEM,