Skip to content

Commit

Permalink
Bump pydantic version to >2.0.0
Browse files Browse the repository at this point in the history
I used the bump-pydantic initially but had to make some additional
changes afterwards. Passes all tests locally so far.

I also inlcuded the requirements.txt in the .gitignore file since
it is only created for the purpose of `make requirements.txt`.

Signed-off-by: Max Mäusezahl <maxmaeusezahl@googlemail.com>
  • Loading branch information
mmaeusezahl committed Mar 16, 2024
1 parent 77b94bb commit 4910719
Show file tree
Hide file tree
Showing 4 changed files with 182 additions and 111 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,6 @@ dmypy.json

# Mega-linter reports dir
megalinter-reports

# Only used for the build system
requirements.txt
11 changes: 5 additions & 6 deletions ha_mqtt_discoverable/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@

import paho.mqtt.client as mqtt
from paho.mqtt.client import MQTTMessageInfo
from pydantic import BaseModel, root_validator
from pydantic.generics import GenericModel
from pydantic import BaseModel, model_validator

# Read version from the package metadata
__version__ = metadata.version(__package__)
Expand Down Expand Up @@ -490,7 +489,7 @@ class DeviceInfo(BaseModel):
Assistant. Examples of such devices are hubs, or parent devices of a sub-device.
This is used to show device topology in Home Assistant."""

@root_validator
@model_validator(mode="before")
def must_have_identifiers_or_connection(cls, values):
"""Check that either `identifiers` or `connections` is set"""
identifiers, connections = values.get("identifiers"), values.get("connections")
Expand Down Expand Up @@ -530,7 +529,7 @@ class EntityInfo(BaseModel):
"""Set this to enable editing sensor from the HA ui and to integrate with a
device"""

@root_validator
@model_validator(mode="before")
def device_need_unique_id(cls, values):
"""Check that `unique_id` is set if `device` is provided,\
otherwise Home Assistant will not link the sensor to the device"""
Expand All @@ -543,7 +542,7 @@ def device_need_unique_id(cls, values):
EntityType = TypeVar("EntityType", bound=EntityInfo)


class Settings(GenericModel, Generic[EntityType]):
class Settings(BaseModel, Generic[EntityType]):
class MQTT(BaseModel):
"""Connection settings for the MQTT broker"""

Expand Down Expand Up @@ -793,7 +792,7 @@ def generate_config(self) -> dict[str, Any]:
automagically ingest the new sensor.
"""
# Automatically generate a dict using pydantic
config = self._entity.dict(exclude_none=True, by_alias=True)
config = self._entity.model_dump(exclude_none=True, by_alias=True)
# Add the MQTT topics to be discovered by HA
topics = {
"state_topic": self.state_topic,
Expand Down
Loading

0 comments on commit 4910719

Please sign in to comment.