-
-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
160 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,48 @@ | ||
# | ||
# Copyright (c) 2019, Andrey "Limych" Khrolenok <[email protected]> | ||
# Copyright (c) 2019-2022, Andrey "Limych" Khrolenok <[email protected]> | ||
# Creative Commons BY-NC-SA 4.0 International Public License | ||
# (see LICENSE.md or https://creativecommons.org/licenses/by-nc-sa/4.0/) | ||
# | ||
|
||
""" | ||
The Average Temperature Sensor. | ||
The Average Sensor. | ||
For more details about this sensor, please refer to the documentation at | ||
https://github.com/Limych/ha-average/ | ||
""" | ||
from __future__ import annotations | ||
|
||
import logging | ||
|
||
import voluptuous as vol | ||
from homeassistant.const import SERVICE_RELOAD | ||
from homeassistant.core import HomeAssistant, ServiceCall | ||
from homeassistant.helpers.entity_component import EntityComponent | ||
from homeassistant.helpers.reload import async_reload_integration_platforms | ||
from homeassistant.helpers.typing import ConfigType | ||
|
||
from .const import DOMAIN, PLATFORMS, STARTUP_MESSAGE | ||
|
||
_LOGGER = logging.getLogger(__name__) | ||
|
||
|
||
async def async_setup(hass: HomeAssistant, config: ConfigType) -> bool: | ||
"""Set up the platforms.""" | ||
# Print startup message | ||
_LOGGER.info(STARTUP_MESSAGE) | ||
|
||
# await async_setup_reload_service(hass, DOMAIN, PLATFORMS) | ||
|
||
component = EntityComponent(_LOGGER, DOMAIN, hass) | ||
|
||
async def reload_service_handler(service: ServiceCall) -> None: | ||
"""Reload all average sensors from config.""" | ||
print("+++++++++++++++++++++++++") | ||
print(component) | ||
# print(hass.data[DATA_INSTANCES]["sensor"].entities[0]) | ||
|
||
await async_reload_integration_platforms(hass, DOMAIN, PLATFORMS) | ||
|
||
hass.services.async_register( | ||
DOMAIN, SERVICE_RELOAD, reload_service_handler, schema=vol.Schema({}) | ||
) | ||
|
||
return True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,19 @@ | ||
{ | ||
"domain": "average", | ||
"name": "Average Sensor", | ||
"version": "2.2.4-alpha", | ||
"documentation": "https://github.com/Limych/ha-average", | ||
"issue_tracker": "https://github.com/Limych/ha-average/issues", | ||
"dependencies": [], | ||
"after_dependencies": [ | ||
"history", | ||
"recorder", | ||
"weather" | ||
], | ||
"config_flow": false, | ||
"codeowners": [ | ||
"@Limych" | ||
], | ||
"requirements": [], | ||
"iot_class": "calculated" | ||
} | ||
"domain": "average", | ||
"name": "Average Sensor", | ||
"version": "2.2.4-alpha", | ||
"documentation": "https://github.com/Limych/ha-average", | ||
"issue_tracker": "https://github.com/Limych/ha-average/issues", | ||
"dependencies": [], | ||
"after_dependencies": [ | ||
"history", | ||
"recorder", | ||
"weather" | ||
], | ||
"config_flow": false, | ||
"codeowners": [ | ||
"@Limych" | ||
], | ||
"requirements": [], | ||
"iot_class": "calculated" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
reload: | ||
name: Reload | ||
description: Reload all average sensor entities |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"name": "Average Sensor", | ||
"hacs": "1.6.0", | ||
"homeassistant": "2022.6.0" | ||
"homeassistant": "2022.7.0" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
homeassistant>=2022.6.0 | ||
homeassistant>=2022.7.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,9 @@ | ||
"""Tests for integration.""" | ||
import pathlib | ||
import traceback | ||
|
||
|
||
def get_fixture_path(filename: str) -> pathlib.Path: | ||
"""Get path of fixture.""" | ||
start_path = traceback.extract_stack()[-1].filename | ||
return pathlib.Path(start_path).parent.joinpath("fixtures", filename) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,17 @@ | ||
"""Constants for tests.""" | ||
from __future__ import annotations | ||
|
||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN | ||
from homeassistant.const import CONF_ENTITIES, CONF_NAME, CONF_UNIQUE_ID | ||
from homeassistant.util import yaml | ||
|
||
from tests import get_fixture_path | ||
|
||
MOCK_CONFIG = yaml.load_yaml(str(get_fixture_path("configuration.yaml"))) | ||
|
||
_cfg = MOCK_CONFIG.get(SENSOR_DOMAIN)[0] # type: dict | ||
|
||
TEST_UNIQUE_ID = _cfg.get(CONF_UNIQUE_ID) | ||
TEST_NAME = _cfg.get(CONF_NAME) | ||
TEST_ENTITY_IDS = _cfg.get(CONF_ENTITIES) | ||
TEST_VALUES = [3, 11.16, -17, 4.29, -29, -16.8, 8, 5, -4.7, 5, -15] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
sensor: | ||
- platform: average | ||
entities: | ||
- sensor.test_monitored | ||
# unique_id: test_id | ||
name: test_name |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
"""The test for the average integration.""" | ||
# pylint: disable=redefined-outer-name | ||
from __future__ import annotations | ||
|
||
from unittest.mock import patch | ||
|
||
from homeassistant import config as hass_config | ||
from homeassistant.components.sensor import DOMAIN as SENSOR_DOMAIN | ||
from homeassistant.const import SERVICE_RELOAD | ||
from homeassistant.setup import async_setup_component | ||
|
||
from custom_components.average.const import DOMAIN | ||
|
||
from . import get_fixture_path | ||
from .const import MOCK_CONFIG, TEST_NAME | ||
|
||
|
||
async def test_reload(hass): | ||
"""Verify we can reload.""" | ||
assert await async_setup_component(hass, SENSOR_DOMAIN, MOCK_CONFIG) | ||
await hass.async_block_till_done() | ||
await hass.async_start() | ||
await hass.async_block_till_done() | ||
|
||
assert len(hass.states.async_all()) == 1 | ||
|
||
assert hass.states.get(f"{SENSOR_DOMAIN}.{TEST_NAME}") | ||
|
||
yaml_path = get_fixture_path("configuration.yaml") | ||
|
||
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): | ||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, {}, blocking=True) | ||
await hass.async_block_till_done() | ||
|
||
assert hass.states.get(f"{SENSOR_DOMAIN}.{TEST_NAME}") | ||
|
||
|
||
async def test_reload_and_remove_all(hass): | ||
"""Verify we can reload and remove all.""" | ||
assert await async_setup_component(hass, SENSOR_DOMAIN, MOCK_CONFIG) | ||
await hass.async_block_till_done() | ||
await hass.async_start() | ||
await hass.async_block_till_done() | ||
|
||
assert len(hass.states.async_all()) == 1 | ||
|
||
assert hass.states.get(f"{SENSOR_DOMAIN}.{TEST_NAME}") | ||
|
||
yaml_path = get_fixture_path("configuration_empty.yaml") | ||
|
||
with patch.object(hass_config, "YAML_CONFIG_FILE", yaml_path): | ||
await hass.services.async_call(DOMAIN, SERVICE_RELOAD, {}, blocking=True) | ||
await hass.async_block_till_done() | ||
|
||
print(hass.states.get(f"{SENSOR_DOMAIN}.{TEST_NAME}")) | ||
assert hass.states.get(f"{SENSOR_DOMAIN}.{TEST_NAME}") is None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters