Skip to content
This repository has been archived by the owner on Aug 15, 2024. It is now read-only.

Commit

Permalink
Merge pull request #120 from oischinger/multidevice
Browse files Browse the repository at this point in the history
MultiDevice support
  • Loading branch information
oischinger authored Mar 31, 2023
2 parents ca89c6e + 135ebc9 commit b37912c
Show file tree
Hide file tree
Showing 21 changed files with 902 additions and 470 deletions.
74 changes: 53 additions & 21 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,58 +1,90 @@
repos:
- repo: https://github.com/asottile/pyupgrade
rev: v2.3.0
rev: v3.3.1
hooks:
- id: pyupgrade
args: [--py37-plus]
args: [--py39-plus]
- repo: https://github.com/PyCQA/autoflake
rev: v2.0.0
hooks:
- id: autoflake
args:
- --in-place
- --remove-all-unused-imports
- repo: https://github.com/psf/black
rev: 22.3.0
rev: 22.12.0
hooks:
- id: black
args:
- --safe
- --quiet
files: ^((homeassistant|script|tests)/.+)?[^/]+\.py$
files: ^((homeassistant|pylint|script|tests)/.+)?[^/]+\.py$
- repo: https://github.com/codespell-project/codespell
rev: v1.16.0
rev: v2.2.2
hooks:
- id: codespell
args:
- --ignore-words-list=hass,alot,datas,dof,dur,farenheit,hist,iff,ines,ist,lightsensor,mut,nd,pres,referer,ser,serie,te,technik,ue,uint,visability,wan,wanna,withing
- --ignore-words-list=additionals,alle,alot,ba,bre,bund,datas,dof,dur,ether,farenheit,falsy,fo,haa,hass,hist,iam,iff,iif,incomfort,ines,ist,lightsensor,mut,nam,nd,pres,pullrequests,referer,resset,rime,ser,serie,sur,te,technik,ue,uint,unsecure,visability,wan,wanna,withing,zar
- --skip="./.*,*.csv,*.json"
- --quiet-level=2
exclude_types: [csv, json]
- repo: https://github.com/PYCQA/flake8.git
rev: 5.0.4
exclude: ^tests/fixtures/|homeassistant/generated/
- repo: https://github.com/PyCQA/flake8
rev: 6.0.0
hooks:
- id: flake8
additional_dependencies:
- flake8-docstrings==1.5.0
- pydocstyle==5.0.2
- pycodestyle==2.10.0
- pyflakes==3.0.1
- flake8-docstrings==1.6.0
- pydocstyle==6.1.1
- flake8-comprehensions==3.10.1
- flake8-noqa==1.3.0
- mccabe==0.7.0
files: ^(homeassistant|script|tests)/.+\.py$
- repo: https://github.com/PyCQA/bandit
rev: 1.6.2
rev: 1.7.4
hooks:
- id: bandit
args:
- --quiet
- --format=custom
- --configfile=tests/bandit.yaml
files: ^(homeassistant|script|tests)/.+\.py$
- repo: https://github.com/pre-commit/mirrors-isort
rev: v4.3.21
- repo: https://github.com/PyCQA/isort
rev: 5.11.4
hooks:
- id: isort
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.4.0
rev: v3.2.0
hooks:
- id: check-executables-have-shebangs
stages: [manual]
- id: check-json
- repo: https://github.com/pre-commit/mirrors-mypy
rev: v0.790
exclude: (.vscode|.devcontainer)
- id: no-commit-to-branch
args:
- --branch=dev
- --branch=master
- --branch=rc
- repo: https://github.com/adrienverge/yamllint.git
rev: v1.28.0
hooks:
- id: mypy
- id: yamllint
- repo: https://github.com/pre-commit/mirrors-prettier
rev: v2.7.1
hooks:
- id: prettier
- repo: https://github.com/cdce8p/python-typing-update
rev: v0.5.0
hooks:
# Run `python-typing-update` hook manually from time to time
# to update python typing syntax.
# Will require manual work, before submitting changes!
# pre-commit run --hook-stage manual python-typing-update --all-files
- id: python-typing-update
stages: [manual]
args:
- --pretty
- --show-error-codes
- --show-error-context
- --py39-plus
- --force
- --keep-updates
files: ^(homeassistant|tests|script)/.+\.py$
1 change: 1 addition & 0 deletions custom_components/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"""ViCare integration."""
82 changes: 64 additions & 18 deletions custom_components/vicare/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,12 @@

from homeassistant.config_entries import ConfigEntry
from homeassistant.const import CONF_CLIENT_ID, CONF_PASSWORD, CONF_USERNAME
from homeassistant.core import HomeAssistant
from homeassistant.core import HomeAssistant, callback
from homeassistant.helpers import entity_registry as er
from homeassistant.helpers.storage import STORAGE_DIR

from .const import (
CONF_HEATING_TYPE,
DEFAULT_SCAN_INTERVAL,
DOMAIN,
HEATING_TYPE_TO_CREATOR_METHOD,
PLATFORMS,
VICARE_API,
VICARE_DEVICE_CONFIG,
HeatingType,
)
from .const import DEFAULT_SCAN_INTERVAL, DOMAIN, PLATFORMS, VICARE_DEVICE_CONFIG
from .helpers import get_unique_device_id

_LOGGER = logging.getLogger(__name__)

Expand All @@ -42,6 +35,60 @@ class ViCareRequiredKeysMixinWithSet:
value_setter: Callable[[Device], bool]


async def async_migrate_entry(hass: HomeAssistant, config_entry: ConfigEntry) -> bool:
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)

_LOGGER.info("Migration to version %s successful", config_entry.version)

return True


async def _async_migrate_entries(
hass: HomeAssistant, config_entry: ConfigEntry
) -> bool:
"""Migrate old entry."""
_LOGGER.debug("Migrating from version %s", config_entry.version)
entity_registry = er.async_get(hass)

if config_entry.version == 1:
devices = hass.data[DOMAIN][config_entry.entry_id][VICARE_DEVICE_CONFIG]
if devices is None or len(devices) == 0:
return True

@callback
def update_unique_id(entry: er.RegistryEntry) -> dict[str, str] | None:
new_unique_id = entry.unique_id.replace(
f"{devices[0].getConfig().serial}-",
f"{get_unique_device_id(devices[0])}-",
)
_LOGGER.debug(
"Migrating entity '%s' unique_id from '%s' to '%s'",
entry.entity_id,
entry.unique_id,
new_unique_id,
)
if existing_entity_id := entity_registry.async_get_entity_id(
entry.domain, entry.platform, new_unique_id
):
_LOGGER.warning(
"Cannot migrate to unique_id '%s', already exists for '%s'",
new_unique_id,
existing_entity_id,
)
return None
return {
"new_unique_id": new_unique_id,
}

await er.async_migrate_entries(hass, config_entry.entry_id, update_unique_id)
config_entry.version = 2

return True

return True


async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
"""Set up from config entry."""
_LOGGER.debug("Setting up ViCare component")
Expand All @@ -51,6 +98,8 @@ async def async_setup_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:

await hass.async_add_executor_job(setup_vicare_api, hass, entry)

await _async_migrate_entries(hass, entry)

await hass.config_entries.async_forward_entry_setups(entry, PLATFORMS)

return True
Expand Down Expand Up @@ -78,13 +127,10 @@ def setup_vicare_api(hass, entry):
"Found device: %s (online: %s)", device.getModel(), str(device.isOnline())
)

# Currently we only support a single device
device = vicare_api.devices[0]
hass.data[DOMAIN][entry.entry_id][VICARE_DEVICE_CONFIG] = device
hass.data[DOMAIN][entry.entry_id][VICARE_API] = getattr(
device,
HEATING_TYPE_TO_CREATOR_METHOD[HeatingType(entry.data[CONF_HEATING_TYPE])],
)()
# Readjust scan interval: each device has its own API endpoint
vicare_api.setCacheDuration(DEFAULT_SCAN_INTERVAL * len(vicare_api.devices))

hass.data[DOMAIN][entry.entry_id][VICARE_DEVICE_CONFIG] = vicare_api.devices


async def async_unload_entry(hass: HomeAssistant, entry: ConfigEntry) -> bool:
Expand Down
Loading

0 comments on commit b37912c

Please sign in to comment.