mirror of
https://github.com/home-assistant/core
synced 2024-08-02 23:40:32 +02:00
Remove deprecated yaml config from co2signal (#62343)
This commit is contained in:
parent
abc7dcf6bf
commit
cabcb52fb3
@ -6,11 +6,11 @@ from typing import Any
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE, CONF_TOKEN
|
||||
from homeassistant.const import CONF_API_KEY, CONF_LATITUDE, CONF_LONGITUDE
|
||||
from homeassistant.data_entry_flow import FlowResult
|
||||
import homeassistant.helpers.config_validation as cv
|
||||
|
||||
from . import APIRatelimitExceeded, CO2Error, InvalidAuth, get_data
|
||||
from . import APIRatelimitExceeded, InvalidAuth, get_data
|
||||
from .const import CONF_COUNTRY_CODE, DOMAIN
|
||||
from .util import get_extra_name
|
||||
|
||||
@ -19,71 +19,12 @@ TYPE_SPECIFY_COORDINATES = "Specify coordinates"
|
||||
TYPE_SPECIFY_COUNTRY = "Specify country code"
|
||||
|
||||
|
||||
def _get_entry_type(config: dict) -> str:
|
||||
"""Get entry type from the configuration."""
|
||||
if CONF_LATITUDE in config:
|
||||
return TYPE_SPECIFY_COORDINATES
|
||||
|
||||
if CONF_COUNTRY_CODE in config:
|
||||
return TYPE_SPECIFY_COUNTRY
|
||||
|
||||
return TYPE_USE_HOME
|
||||
|
||||
|
||||
class ConfigFlow(config_entries.ConfigFlow, domain=DOMAIN):
|
||||
"""Handle a config flow for Co2signal."""
|
||||
|
||||
VERSION = 1
|
||||
_data: dict | None
|
||||
|
||||
async def async_step_import(self, import_info):
|
||||
"""Set the config entry up from yaml."""
|
||||
data = {CONF_API_KEY: import_info[CONF_TOKEN]}
|
||||
|
||||
if CONF_COUNTRY_CODE in import_info:
|
||||
data[CONF_COUNTRY_CODE] = import_info[CONF_COUNTRY_CODE]
|
||||
new_entry_type = TYPE_SPECIFY_COUNTRY
|
||||
elif (
|
||||
CONF_LATITUDE in import_info
|
||||
and import_info[CONF_LATITUDE] != self.hass.config.latitude
|
||||
and import_info[CONF_LONGITUDE] != self.hass.config.longitude
|
||||
):
|
||||
data[CONF_LATITUDE] = import_info[CONF_LATITUDE]
|
||||
data[CONF_LONGITUDE] = import_info[CONF_LONGITUDE]
|
||||
new_entry_type = TYPE_SPECIFY_COORDINATES
|
||||
else:
|
||||
new_entry_type = TYPE_USE_HOME
|
||||
|
||||
for entry in self._async_current_entries(include_ignore=False):
|
||||
|
||||
if (cur_entry_type := _get_entry_type(entry.data)) != new_entry_type:
|
||||
continue
|
||||
|
||||
if cur_entry_type == TYPE_USE_HOME and new_entry_type == TYPE_USE_HOME:
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
if (
|
||||
cur_entry_type == TYPE_SPECIFY_COUNTRY
|
||||
and data[CONF_COUNTRY_CODE] == entry.data[CONF_COUNTRY_CODE]
|
||||
):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
if (
|
||||
cur_entry_type == TYPE_SPECIFY_COORDINATES
|
||||
and data[CONF_LATITUDE] == entry.data[CONF_LATITUDE]
|
||||
and data[CONF_LONGITUDE] == entry.data[CONF_LONGITUDE]
|
||||
):
|
||||
return self.async_abort(reason="already_configured")
|
||||
|
||||
try:
|
||||
await self.hass.async_add_executor_job(get_data, self.hass, data)
|
||||
except CO2Error:
|
||||
return self.async_abort(reason="unknown")
|
||||
|
||||
return self.async_create_entry(
|
||||
title=get_extra_name(data) or "CO2 Signal", data=data
|
||||
)
|
||||
|
||||
async def async_step_user(
|
||||
self, user_input: dict[str, Any] | None = None
|
||||
) -> FlowResult:
|
||||
|
@ -4,8 +4,3 @@
|
||||
DOMAIN = "co2signal"
|
||||
CONF_COUNTRY_CODE = "country_code"
|
||||
ATTRIBUTION = "Data provided by CO2signal"
|
||||
MSG_LOCATION = (
|
||||
"Please use either coordinates or the country code. "
|
||||
"For the coordinates, "
|
||||
"you need to use both latitude and longitude."
|
||||
)
|
||||
|
@ -5,40 +5,19 @@ from dataclasses import dataclass
|
||||
from datetime import timedelta
|
||||
from typing import cast
|
||||
|
||||
import voluptuous as vol
|
||||
|
||||
from homeassistant import config_entries
|
||||
from homeassistant.components.sensor import (
|
||||
PLATFORM_SCHEMA,
|
||||
SensorEntity,
|
||||
SensorStateClass,
|
||||
)
|
||||
from homeassistant.const import (
|
||||
ATTR_ATTRIBUTION,
|
||||
CONF_LATITUDE,
|
||||
CONF_LONGITUDE,
|
||||
CONF_TOKEN,
|
||||
PERCENTAGE,
|
||||
)
|
||||
from homeassistant.helpers import config_validation as cv, update_coordinator
|
||||
from homeassistant.components.sensor import SensorEntity, SensorStateClass
|
||||
from homeassistant.const import ATTR_ATTRIBUTION, PERCENTAGE
|
||||
from homeassistant.helpers import update_coordinator
|
||||
from homeassistant.helpers.device_registry import DeviceEntryType
|
||||
from homeassistant.helpers.entity import DeviceInfo
|
||||
from homeassistant.helpers.typing import StateType
|
||||
|
||||
from . import CO2SignalCoordinator, CO2SignalResponse
|
||||
from .const import ATTRIBUTION, CONF_COUNTRY_CODE, DOMAIN, MSG_LOCATION
|
||||
from .const import ATTRIBUTION, DOMAIN
|
||||
|
||||
SCAN_INTERVAL = timedelta(minutes=3)
|
||||
|
||||
PLATFORM_SCHEMA = PLATFORM_SCHEMA.extend(
|
||||
{
|
||||
vol.Required(CONF_TOKEN): cv.string,
|
||||
vol.Inclusive(CONF_LATITUDE, "coords", msg=MSG_LOCATION): cv.latitude,
|
||||
vol.Inclusive(CONF_LONGITUDE, "coords", msg=MSG_LOCATION): cv.longitude,
|
||||
vol.Optional(CONF_COUNTRY_CODE): cv.string,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@dataclass
|
||||
class CO2SensorEntityDescription:
|
||||
|
@ -7,12 +7,9 @@ from homeassistant import config_entries
|
||||
from homeassistant.components.co2signal import DOMAIN, config_flow
|
||||
from homeassistant.core import HomeAssistant
|
||||
from homeassistant.data_entry_flow import RESULT_TYPE_CREATE_ENTRY, RESULT_TYPE_FORM
|
||||
from homeassistant.setup import async_setup_component
|
||||
|
||||
from . import VALID_PAYLOAD
|
||||
|
||||
from tests.common import MockConfigEntry
|
||||
|
||||
|
||||
async def test_form_home(hass: HomeAssistant) -> None:
|
||||
"""Test we get the form."""
|
||||
@ -196,104 +193,3 @@ async def test_form_error_unexpected_data(hass: HomeAssistant) -> None:
|
||||
|
||||
assert result2["type"] == RESULT_TYPE_FORM
|
||||
assert result2["errors"] == {"base": "unknown"}
|
||||
|
||||
|
||||
async def test_import(hass: HomeAssistant) -> None:
|
||||
"""Test we import correctly."""
|
||||
|
||||
with patch(
|
||||
"CO2Signal.get_latest",
|
||||
return_value=VALID_PAYLOAD,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass, "sensor", {"sensor": {"platform": "co2signal", "token": "1234"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries("co2signal")) == 1
|
||||
|
||||
state = hass.states.get("sensor.co2_intensity")
|
||||
assert state is not None
|
||||
assert state.state == "45.99"
|
||||
assert state.name == "CO2 intensity"
|
||||
assert state.attributes["unit_of_measurement"] == "gCO2eq/kWh"
|
||||
assert state.attributes["country_code"] == "FR"
|
||||
|
||||
state = hass.states.get("sensor.grid_fossil_fuel_percentage")
|
||||
assert state is not None
|
||||
assert state.state == "5.46"
|
||||
assert state.name == "Grid fossil fuel percentage"
|
||||
assert state.attributes["unit_of_measurement"] == "%"
|
||||
assert state.attributes["country_code"] == "FR"
|
||||
|
||||
|
||||
async def test_import_abort_existing_home(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if home entry found."""
|
||||
|
||||
MockConfigEntry(domain="co2signal", data={"api_key": "abcd"}).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"CO2Signal.get_latest",
|
||||
return_value=VALID_PAYLOAD,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass, "sensor", {"sensor": {"platform": "co2signal", "token": "1234"}}
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries("co2signal")) == 1
|
||||
|
||||
|
||||
async def test_import_abort_existing_country(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if existing country found."""
|
||||
|
||||
MockConfigEntry(
|
||||
domain="co2signal", data={"api_key": "abcd", "country_code": "nl"}
|
||||
).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"CO2Signal.get_latest",
|
||||
return_value=VALID_PAYLOAD,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "co2signal",
|
||||
"token": "1234",
|
||||
"country_code": "nl",
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries("co2signal")) == 1
|
||||
|
||||
|
||||
async def test_import_abort_existing_coordinates(hass: HomeAssistant) -> None:
|
||||
"""Test we abort if existing coordinates found."""
|
||||
|
||||
MockConfigEntry(
|
||||
domain="co2signal", data={"api_key": "abcd", "latitude": 1, "longitude": 2}
|
||||
).add_to_hass(hass)
|
||||
|
||||
with patch(
|
||||
"CO2Signal.get_latest",
|
||||
return_value=VALID_PAYLOAD,
|
||||
):
|
||||
assert await async_setup_component(
|
||||
hass,
|
||||
"sensor",
|
||||
{
|
||||
"sensor": {
|
||||
"platform": "co2signal",
|
||||
"token": "1234",
|
||||
"latitude": 1,
|
||||
"longitude": 2,
|
||||
}
|
||||
},
|
||||
)
|
||||
await hass.async_block_till_done()
|
||||
|
||||
assert len(hass.config_entries.async_entries("co2signal")) == 1
|
||||
|
Loading…
Reference in New Issue
Block a user