Add defaults to all fields in model.py

See also https://github.com/home-assistant/home-assistant/issues/28157
This commit is contained in:
Otto Winter 2019-10-24 13:59:06 +02:00
parent 40cb3f8ca9
commit 10724ea3e5
No known key found for this signature in database
GPG Key ID: DB66C0BE6013F97E

View File

@ -3,47 +3,53 @@ from typing import List, Dict
import attr import attr
# All fields in here should have defaults set
# Home Assistant depends on these fields being constructible
# with args from a previous version of Home Assistant.
# The default value should *always* be the Protobuf default value
# for a field (False, 0, empty string, enum with value 0, ...)
@attr.s(cmp=True) @attr.s(cmp=True)
class APIVersion: class APIVersion:
major = attr.ib(type=int) major = attr.ib(type=int, default=0)
minor = attr.ib(type=int) minor = attr.ib(type=int, default=0)
@attr.s @attr.s
class DeviceInfo: class DeviceInfo:
uses_password = attr.ib(type=bool) uses_password = attr.ib(type=bool, default=False)
name = attr.ib(type=str) name = attr.ib(type=str, default='')
mac_address = attr.ib(type=str) mac_address = attr.ib(type=str, default='')
compilation_time = attr.ib(type=str) compilation_time = attr.ib(type=str, default='')
model = attr.ib(type=str) model = attr.ib(type=str, default='')
has_deep_sleep = attr.ib(type=bool) has_deep_sleep = attr.ib(type=bool, default=False)
esphome_version = attr.ib(type=str, default='') esphome_version = attr.ib(type=str, default='')
@attr.s @attr.s
class EntityInfo: class EntityInfo:
object_id = attr.ib(type=str) object_id = attr.ib(type=str, default='')
key = attr.ib(type=int) key = attr.ib(type=int, default=0)
name = attr.ib(type=str) name = attr.ib(type=str, default='')
unique_id = attr.ib(type=str) unique_id = attr.ib(type=str, default='')
@attr.s @attr.s
class EntityState: class EntityState:
key = attr.ib(type=int) key = attr.ib(type=int, default=0)
# ==================== BINARY SENSOR ==================== # ==================== BINARY SENSOR ====================
@attr.s @attr.s
class BinarySensorInfo(EntityInfo): class BinarySensorInfo(EntityInfo):
device_class = attr.ib(type=str) device_class = attr.ib(type=str, default='')
is_status_binary_sensor = attr.ib(type=bool) is_status_binary_sensor = attr.ib(type=bool, default=False)
@attr.s @attr.s
class BinarySensorState(EntityState): class BinarySensorState(EntityState):
state = attr.ib(type=bool) state = attr.ib(type=bool, default=False)
# ==================== COVER ==================== # ==================== COVER ====================
@ -74,10 +80,12 @@ class CoverOperation(enum.IntEnum):
@attr.s @attr.s
class CoverState(EntityState): class CoverState(EntityState):
legacy_state = attr.ib(type=LegacyCoverState, converter=LegacyCoverState) legacy_state = attr.ib(type=LegacyCoverState, converter=LegacyCoverState,
position = attr.ib(type=float) default=LegacyCoverState.OPEN)
tilt = attr.ib(type=float) position = attr.ib(type=float, default=0.0)
current_operation = attr.ib(type=CoverOperation, converter=CoverOperation) tilt = attr.ib(type=float, default=0.0)
current_operation = attr.ib(type=CoverOperation, converter=CoverOperation,
default=CoverOperation.IDLE)
def is_closed(self, api_version: APIVersion): def is_closed(self, api_version: APIVersion):
if api_version >= APIVersion(1, 1): if api_version >= APIVersion(1, 1):
@ -88,8 +96,8 @@ class CoverState(EntityState):
# ==================== FAN ==================== # ==================== FAN ====================
@attr.s @attr.s
class FanInfo(EntityInfo): class FanInfo(EntityInfo):
supports_oscillation = attr.ib(type=bool) supports_oscillation = attr.ib(type=bool, default=False)
supports_speed = attr.ib(type=bool) supports_speed = attr.ib(type=bool, default=False)
class FanSpeed(enum.IntEnum): class FanSpeed(enum.IntEnum):
@ -100,70 +108,70 @@ class FanSpeed(enum.IntEnum):
@attr.s @attr.s
class FanState(EntityState): class FanState(EntityState):
state = attr.ib(type=bool) state = attr.ib(type=bool, default=False)
oscillating = attr.ib(type=bool) oscillating = attr.ib(type=bool, default=False)
speed = attr.ib(type=FanSpeed, converter=FanSpeed) speed = attr.ib(type=FanSpeed, converter=FanSpeed, default=FanSpeed.LOW)
# ==================== LIGHT ==================== # ==================== LIGHT ====================
@attr.s @attr.s
class LightInfo(EntityInfo): class LightInfo(EntityInfo):
supports_brightness = attr.ib(type=bool) supports_brightness = attr.ib(type=bool, default=False)
supports_rgb = attr.ib(type=bool) supports_rgb = attr.ib(type=bool, default=False)
supports_white_value = attr.ib(type=bool) supports_white_value = attr.ib(type=bool, default=False)
supports_color_temperature = attr.ib(type=bool) supports_color_temperature = attr.ib(type=bool, default=False)
min_mireds = attr.ib(type=float) min_mireds = attr.ib(type=float, default=0.0)
max_mireds = attr.ib(type=float) max_mireds = attr.ib(type=float, default=0.0)
effects = attr.ib(type=List[str], converter=list) effects = attr.ib(type=List[str], converter=list, factory=list)
@attr.s @attr.s
class LightState(EntityState): class LightState(EntityState):
state = attr.ib(type=bool) state = attr.ib(type=bool, default=False)
brightness = attr.ib(type=float) brightness = attr.ib(type=float, default=0.0)
red = attr.ib(type=float) red = attr.ib(type=float, default=0.0)
green = attr.ib(type=float) green = attr.ib(type=float, default=0.0)
blue = attr.ib(type=float) blue = attr.ib(type=float, default=0.0)
white = attr.ib(type=float) white = attr.ib(type=float, default=0.0)
color_temperature = attr.ib(type=float) color_temperature = attr.ib(type=float, default=0.0)
effect = attr.ib(type=str) effect = attr.ib(type=str, default='')
# ==================== SENSOR ==================== # ==================== SENSOR ====================
@attr.s @attr.s
class SensorInfo(EntityInfo): class SensorInfo(EntityInfo):
icon = attr.ib(type=str) icon = attr.ib(type=str, default='')
unit_of_measurement = attr.ib(type=str) unit_of_measurement = attr.ib(type=str, default='')
accuracy_decimals = attr.ib(type=int) accuracy_decimals = attr.ib(type=int, default=0)
force_update = attr.ib(type=bool) force_update = attr.ib(type=bool, default=False)
@attr.s @attr.s
class SensorState(EntityState): class SensorState(EntityState):
state = attr.ib(type=float) state = attr.ib(type=float, default=0.0)
# ==================== SWITCH ==================== # ==================== SWITCH ====================
@attr.s @attr.s
class SwitchInfo(EntityInfo): class SwitchInfo(EntityInfo):
icon = attr.ib(type=str) icon = attr.ib(type=str, default='')
assumed_state = attr.ib(type=bool, default=False) assumed_state = attr.ib(type=bool, default=False)
@attr.s @attr.s
class SwitchState(EntityState): class SwitchState(EntityState):
state = attr.ib(type=bool) state = attr.ib(type=bool, default=False)
# ==================== TEXT SENSOR ==================== # ==================== TEXT SENSOR ====================
@attr.s @attr.s
class TextSensorInfo(EntityInfo): class TextSensorInfo(EntityInfo):
icon = attr.ib(type=str) icon = attr.ib(type=str, default='')
@attr.s @attr.s
class TextSensorState(EntityState): class TextSensorState(EntityState):
state = attr.ib(type=str) state = attr.ib(type=str, default='')
# ==================== CAMERA ==================== # ==================== CAMERA ====================
@ -174,7 +182,7 @@ class CameraInfo(EntityInfo):
@attr.s @attr.s
class CameraState(EntityState): class CameraState(EntityState):
image = attr.ib(type=bytes) image = attr.ib(type=bytes, factory=bytes)
# ==================== CLIMATE ==================== # ==================== CLIMATE ====================
@ -197,25 +205,26 @@ def _convert_climate_modes(value):
@attr.s @attr.s
class ClimateInfo(EntityInfo): class ClimateInfo(EntityInfo):
supports_current_temperature = attr.ib(type=bool) supports_current_temperature = attr.ib(type=bool, default=False)
supports_two_point_target_temperature = attr.ib(type=bool) supports_two_point_target_temperature = attr.ib(type=bool, default=False)
supported_modes = attr.ib(type=List[ClimateMode], converter=_convert_climate_modes) supported_modes = attr.ib(type=List[ClimateMode], converter=_convert_climate_modes,
visual_min_temperature = attr.ib(type=float) factory=list)
visual_max_temperature = attr.ib(type=float) visual_min_temperature = attr.ib(type=float, default=0.0)
visual_temperature_step = attr.ib(type=float) visual_max_temperature = attr.ib(type=float, default=0.0)
supports_away = attr.ib(type=bool) visual_temperature_step = attr.ib(type=float, default=0.0)
supports_action = attr.ib(type=bool) supports_away = attr.ib(type=bool, default=False)
supports_action = attr.ib(type=bool, default=False)
@attr.s @attr.s
class ClimateState(EntityState): class ClimateState(EntityState):
mode = attr.ib(type=ClimateMode, converter=ClimateMode) mode = attr.ib(type=ClimateMode, converter=ClimateMode, default=CimateMode.OFF)
action = attr.ib(type=ClimateAction, converter=ClimateAction) action = attr.ib(type=ClimateAction, converter=ClimateAction, default=ClimateAction.OFF)
current_temperature = attr.ib(type=float) current_temperature = attr.ib(type=float, default=0.0)
target_temperature = attr.ib(type=float) target_temperature = attr.ib(type=float, default=0.0)
target_temperature_low = attr.ib(type=float) target_temperature_low = attr.ib(type=float, default=0.0)
target_temperature_high = attr.ib(type=float) target_temperature_high = attr.ib(type=float, default=0.0)
away = attr.ib(type=bool) away = attr.ib(type=bool, default=False)
COMPONENT_TYPE_TO_INFO = { COMPONENT_TYPE_TO_INFO = {
@ -238,11 +247,14 @@ def _convert_homeassistant_service_map(value):
@attr.s @attr.s
class HomeassistantServiceCall: class HomeassistantServiceCall:
service = attr.ib(type=str) service = attr.ib(type=str, default='')
is_event = attr.ib(type=bool) is_event = attr.ib(type=bool, default=False)
data = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map) data = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map,
data_template = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map) factory=dict)
variables = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map) data_template = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map,
factory=dict)
variables = attr.ib(type=Dict[str, str], converter=_convert_homeassistant_service_map,
factory=dict)
class UserServiceArgType(enum.IntEnum): class UserServiceArgType(enum.IntEnum):
@ -262,15 +274,16 @@ def _attr_obj_from_dict(cls, **kwargs):
@attr.s @attr.s
class UserServiceArg: class UserServiceArg:
name = attr.ib(type=str) name = attr.ib(type=str, default='')
type_ = attr.ib(type=UserServiceArgType, converter=UserServiceArgType) type_ = attr.ib(type=UserServiceArgType, converter=UserServiceArgType,
default=UserServiceArgType.BOOL)
@attr.s @attr.s
class UserService: class UserService:
name = attr.ib(type=str) name = attr.ib(type=str, default='')
key = attr.ib(type=int) key = attr.ib(type=int, default=0)
args = attr.ib(type=List[UserServiceArg], converter=list) args = attr.ib(type=List[UserServiceArg], converter=list, factory=list)
@staticmethod @staticmethod
def from_dict(dict_): def from_dict(dict_):